下面列出了怎么用java.awt.image.MemoryImageSource的API类实例代码及写法,或者点击链接到github查看源代码。
private void Setup() {
balls = new Image[nBalls];
byte red[] = new byte[256];
red[0] = (byte) bgGrey;
byte green[] = new byte[256];
green[0] = (byte) bgGrey;
byte blue[] = new byte[256];
blue[0] = (byte) bgGrey;
for (int r = 0; r < nBalls; r++) {
float b = (float) (r + 1) / nBalls;
for (int i = maxr; i >= 1; --i) {
float d = (float) i / maxr;
red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
}
IndexColorModel model = new IndexColorModel(8, maxr + 1,
red, green, blue, 0);
balls[r] = applet.createImage(
new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2));
}
}
private static Image createImage() {
int w = 100;
int h = 100;
int[] pix = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int red = 127;
int green = 127;
int blue = y > h / 2 ? 127 : 0;
int alpha = 255;
if (x < w / 4 && y < h / 4) {
alpha = 0;
red = 0;
}
pix[index++] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
}
return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, pix, 0, w));
}
private void Setup() {
balls = new Image[nBalls];
byte red[] = new byte[256];
red[0] = (byte) bgGrey;
byte green[] = new byte[256];
green[0] = (byte) bgGrey;
byte blue[] = new byte[256];
blue[0] = (byte) bgGrey;
for (int r = 0; r < nBalls; r++) {
float b = (float) (r + 1) / nBalls;
for (int i = maxr; i >= 1; --i) {
float d = (float) i / maxr;
red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
}
IndexColorModel model = new IndexColorModel(8, maxr + 1,
red, green, blue, 0);
balls[r] = applet.createImage(
new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2));
}
}
private static Image createImage() {
int w = 100;
int h = 100;
int[] pix = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int red = 127;
int green = 127;
int blue = y > h / 2 ? 127 : 0;
int alpha = 255;
if (x < w / 4 && y < h / 4) {
alpha = 0;
red = 0;
}
pix[index++] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
}
return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, pix, 0, w));
}
private void Setup() {
balls = new Image[nBalls];
byte red[] = new byte[256];
red[0] = (byte) bgGrey;
byte green[] = new byte[256];
green[0] = (byte) bgGrey;
byte blue[] = new byte[256];
blue[0] = (byte) bgGrey;
for (int r = 0; r < nBalls; r++) {
float b = (float) (r + 1) / nBalls;
for (int i = maxr; i >= 1; --i) {
float d = (float) i / maxr;
red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
}
IndexColorModel model = new IndexColorModel(8, maxr + 1,
red, green, blue, 0);
balls[r] = applet.createImage(
new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2));
}
}
private void Setup() {
balls = new Image[nBalls];
byte red[] = new byte[256];
red[0] = (byte) bgGrey;
byte green[] = new byte[256];
green[0] = (byte) bgGrey;
byte blue[] = new byte[256];
blue[0] = (byte) bgGrey;
for (int r = 0; r < nBalls; r++) {
float b = (float) (r + 1) / nBalls;
for (int i = maxr; i >= 1; --i) {
float d = (float) i / maxr;
red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
}
IndexColorModel model = new IndexColorModel(8, maxr + 1,
red, green, blue, 0);
balls[r] = applet.createImage(
new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2));
}
}
private static Image createImage() {
int w = 100;
int h = 100;
int[] pix = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int red = 127;
int green = 127;
int blue = y > h / 2 ? 127 : 0;
int alpha = 255;
if (x < w / 4 && y < h / 4) {
alpha = 0;
red = 0;
}
pix[index++] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
}
return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, pix, 0, w));
}
private void Setup() {
balls = new Image[nBalls];
byte red[] = new byte[256];
red[0] = (byte) bgGrey;
byte green[] = new byte[256];
green[0] = (byte) bgGrey;
byte blue[] = new byte[256];
blue[0] = (byte) bgGrey;
for (int r = 0; r < nBalls; r++) {
float b = (float) (r + 1) / nBalls;
for (int i = maxr; i >= 1; --i) {
float d = (float) i / maxr;
red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
}
IndexColorModel model = new IndexColorModel(8, maxr + 1,
red, green, blue, 0);
balls[r] = applet.createImage(
new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2));
}
}
private static Image createImage() {
int w = 100;
int h = 100;
int[] pix = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int red = 127;
int green = 127;
int blue = y > h / 2 ? 127 : 0;
int alpha = 255;
if (x < w / 4 && y < h / 4) {
alpha = 0;
red = 0;
}
pix[index++] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
}
return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, pix, 0, w));
}
private void Setup() {
balls = new Image[nBalls];
byte red[] = new byte[256];
red[0] = (byte) bgGrey;
byte green[] = new byte[256];
green[0] = (byte) bgGrey;
byte blue[] = new byte[256];
blue[0] = (byte) bgGrey;
for (int r = 0; r < nBalls; r++) {
float b = (float) (r + 1) / nBalls;
for (int i = maxr; i >= 1; --i) {
float d = (float) i / maxr;
red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
}
IndexColorModel model = new IndexColorModel(8, maxr + 1,
red, green, blue, 0);
balls[r] = applet.createImage(
new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2));
}
}
private static Image createImage() {
int w = 100;
int h = 100;
int[] pix = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int red = 127;
int green = 127;
int blue = y > h / 2 ? 127 : 0;
int alpha = 255;
if (x < w / 4 && y < h / 4) {
alpha = 0;
red = 0;
}
pix[index++] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
}
return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, pix, 0, w));
}
private void Setup() {
balls = new Image[nBalls];
byte red[] = new byte[256];
red[0] = (byte) bgGrey;
byte green[] = new byte[256];
green[0] = (byte) bgGrey;
byte blue[] = new byte[256];
blue[0] = (byte) bgGrey;
for (int r = 0; r < nBalls; r++) {
float b = (float) (r + 1) / nBalls;
for (int i = maxr; i >= 1; --i) {
float d = (float) i / maxr;
red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
}
IndexColorModel model = new IndexColorModel(8, maxr + 1,
red, green, blue, 0);
balls[r] = applet.createImage(
new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2));
}
}
private static Image createImage() {
int w = 100;
int h = 100;
int[] pix = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int red = 127;
int green = 127;
int blue = y > h / 2 ? 127 : 0;
int alpha = 255;
if (x < w / 4 && y < h / 4) {
alpha = 0;
red = 0;
}
pix[index++] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
}
return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, pix, 0, w));
}
/**
* Creates a <CODE>java.awt.Image</CODE>. A successful call to the method
* <CODE>generate()</CODE> before calling this method is required.
*
* @param foreground the color of the bars
* @param background the color of the background
* @return the image
*/
public java.awt.Image createAwtImage(Color foreground, Color background) {
if (image == null) {
return null;
}
int f = foreground.getRGB();
int g = background.getRGB();
Canvas canvas = new Canvas();
int w = width + 2 * ws;
int h = height + 2 * ws;
int pix[] = new int[w * h];
int stride = (w + 7) / 8;
int ptr = 0;
for (int k = 0; k < h; ++k) {
int p = k * stride;
for (int j = 0; j < w; ++j) {
int b = image[p + j / 8] & 0xff;
b <<= j % 8;
pix[ptr++] = (b & 0x80) == 0 ? g : f;
}
}
java.awt.Image img = canvas.createImage(new MemoryImageSource(w, h, pix, 0, w));
return img;
}
/**
* Creates a <CODE>java.awt.Image</CODE>. A successful call to the method <CODE>generate()</CODE>
* before calling this method is required.
* @param foreground the color of the bars
* @param background the color of the background
* @return the image
*/
public java.awt.Image createAwtImage(Color foreground, Color background) {
if (image == null)
return null;
int f = foreground.getRGB();
int g = background.getRGB();
Canvas canvas = new Canvas();
int w = width + 2 * ws;
int h = height + 2 * ws;
int pix[] = new int[w * h];
int stride = (w + 7) / 8;
int ptr = 0;
for (int k = 0; k < h; ++k) {
int p = k * stride;
for (int j = 0; j < w; ++j) {
int b = image[p + (j / 8)] & 0xff;
b <<= j % 8;
pix[ptr++] = (b & 0x80) == 0 ? g : f;
}
}
java.awt.Image img = canvas.createImage(new MemoryImageSource(w, h, pix, 0, w));
return img;
}
private void Setup() {
balls = new Image[nBalls];
byte red[] = new byte[256];
red[0] = (byte) bgGrey;
byte green[] = new byte[256];
green[0] = (byte) bgGrey;
byte blue[] = new byte[256];
blue[0] = (byte) bgGrey;
for (int r = 0; r < nBalls; r++) {
float b = (float) (r + 1) / nBalls;
for (int i = maxr; i >= 1; --i) {
float d = (float) i / maxr;
red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
}
IndexColorModel model = new IndexColorModel(8, maxr + 1,
red, green, blue, 0);
balls[r] = applet.createImage(
new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2));
}
}
private static Image createImage() {
int w = 100;
int h = 100;
int[] pix = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int red = 127;
int green = 127;
int blue = y > h / 2 ? 127 : 0;
int alpha = 255;
if (x < w / 4 && y < h / 4) {
alpha = 0;
red = 0;
}
pix[index++] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
}
return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, pix, 0, w));
}
/**
* Constructs IntegerImage with the given ColorModel and data.
*
* @param colorModel
* @param data
*/
public ByteImage(ColorModel colorModel, byte[][] data) {
nrow = data.length;
ncol = data[0].length;
imagePixels = new byte[nrow * ncol];
for (int i = 0; i < nrow; i++) {
byte[] row = data[i];
System.arraycopy(row, 0, imagePixels, i * ncol, ncol);
}
imageSource = new MemoryImageSource(ncol, nrow,colorModel, imagePixels, 0, ncol);
imageSource.setAnimated(true);
image = Toolkit.getDefaultToolkit().createImage(imageSource);
dirtyImage=false;
xmin = 0;
xmax = ncol;
ymin = nrow;
ymax = 0; // zero is on top
}
private void Setup() {
balls = new Image[nBalls];
byte red[] = new byte[256];
red[0] = (byte) bgGrey;
byte green[] = new byte[256];
green[0] = (byte) bgGrey;
byte blue[] = new byte[256];
blue[0] = (byte) bgGrey;
for (int r = 0; r < nBalls; r++) {
float b = (float) (r + 1) / nBalls;
for (int i = maxr; i >= 1; --i) {
float d = (float) i / maxr;
red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
}
IndexColorModel model = new IndexColorModel(8, maxr + 1,
red, green, blue, 0);
balls[r] = applet.createImage(
new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2));
}
}
private void Setup() {
balls = new Image[nBalls];
byte red[] = new byte[256];
red[0] = (byte) bgGrey;
byte green[] = new byte[256];
green[0] = (byte) bgGrey;
byte blue[] = new byte[256];
blue[0] = (byte) bgGrey;
for (int r = 0; r < nBalls; r++) {
float b = (float) (r + 1) / nBalls;
for (int i = maxr; i >= 1; --i) {
float d = (float) i / maxr;
red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
}
IndexColorModel model = new IndexColorModel(8, maxr + 1,
red, green, blue, 0);
balls[r] = applet.createImage(
new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2));
}
}
private void Setup() {
balls = new Image[nBalls];
byte red[] = new byte[256];
red[0] = (byte) bgGrey;
byte green[] = new byte[256];
green[0] = (byte) bgGrey;
byte blue[] = new byte[256];
blue[0] = (byte) bgGrey;
for (int r = 0; r < nBalls; r++) {
float b = (float) (r + 1) / nBalls;
for (int i = maxr; i >= 1; --i) {
float d = (float) i / maxr;
red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
}
IndexColorModel model = new IndexColorModel(8, maxr + 1,
red, green, blue, 0);
balls[r] = applet.createImage(
new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2));
}
}
private static Image createImage() {
int w = 100;
int h = 100;
int[] pix = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int red = 127;
int green = 127;
int blue = y > h / 2 ? 127 : 0;
int alpha = 255;
if (x < w / 4 && y < h / 4) {
alpha = 0;
red = 0;
}
pix[index++] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
}
return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, pix, 0, w));
}
/**
* Converts a byte array to an image that has previously been converted with imageToCompressedByteArray(Image image).
* The image is not recognizable as image from standard tools.
*
* @param data The image.
* @return The image.
*/
public static Image compressedByteArrayToImage(byte[] data) {
try {
// unzip data
ByteArrayInputStream byteStream = new ByteArrayInputStream(data);
GZIPInputStream zippedStream = new GZIPInputStream(byteStream);
ObjectInputStream objectStream = new ObjectInputStream(zippedStream);
int width = objectStream.readShort();
int height = objectStream.readShort();
int[] imageSource = (int[])objectStream.readObject();
objectStream.close();
// create image
MemoryImageSource mis = new MemoryImageSource(width, height, imageSource, 0, width);
return Toolkit.getDefaultToolkit().createImage(mis);
}
catch (Exception e) {
return null;
}
}
private void Setup() {
balls = new Image[nBalls];
byte red[] = new byte[256];
red[0] = (byte) bgGrey;
byte green[] = new byte[256];
green[0] = (byte) bgGrey;
byte blue[] = new byte[256];
blue[0] = (byte) bgGrey;
for (int r = 0; r < nBalls; r++) {
float b = (float) (r + 1) / nBalls;
for (int i = maxr; i >= 1; --i) {
float d = (float) i / maxr;
red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
}
IndexColorModel model = new IndexColorModel(8, maxr + 1,
red, green, blue, 0);
balls[r] = applet.createImage(
new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2));
}
}
/**
* 将黑色颜色部分透明化
*
* @param img
* @return
*/
final static public Image transparencyBlackColor(final Image img) {
int width = img.getWidth(null);
int height = img.getHeight(null);
PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, true);
try {
pg.grabPixels();
} catch (InterruptedException e) {
e.printStackTrace();
}
int pixels[] = (int[]) pg.getPixels();
int length = pixels.length;
for (int i = 0; i < length; i++) {
if (pixels[i] == 0) {
pixels[i] = 0xffffff;
}
}
return toolKit.createImage(new MemoryImageSource(width, height, pixels, 0, width));
}
final static public Image transparencyBlackColor(final Image img, final Color c) {
int width = img.getWidth(null);
int height = img.getHeight(null);
PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, true);
try {
pg.grabPixels();
} catch (InterruptedException e) {
e.printStackTrace();
}
int pixels[] = (int[]) pg.getPixels();
int length = pixels.length;
for (int i = 0; i < length; i++) {
int pixel = pixels[i];
int[] rgbs = LColor.getRGBs(pixel);
if (rgbs[0] >= 252 && rgbs[1] >= 252 && rgbs[1] >= 252) {
pixels[i] = 0xffffff;
}
}
return toolKit.createImage(new MemoryImageSource(width, height, pixels, 0, width));
}
public static Image decodeImage (String[] rows)
{
final int width = rows[0].length();
final int height = rows.length;
int[] pix = new int[width * height];
int index = 0;
for (String row : rows) {
for (int x = 0; x < width; x++) {
pix[index++] = decodeARGB (row.charAt (x));
}
}
// Create the proper image icon
Toolkit tk = Toolkit.getDefaultToolkit ();
return tk.createImage
(new MemoryImageSource (width, height, pix, 0, width));
}
/**
* Calculates and returns the image. Halts the calculation and returns
* null if the Applet is stopped during the calculation.
*/
private Image calculateImage() {
Thread me = Thread.currentThread();
int width = canvas.getSize().width;
int height = canvas.getSize().height;
int xvals[] = new int[2];
int yvals[] = new int[2];
int xmethod = XControls.getParams(xvals);
int ymethod = YControls.getParams(yvals);
int pixels[] = new int[width * height];
int c[] = new int[4]; //temporarily holds R,G,B,A information
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
c[0] = c[1] = c[2] = 0;
c[3] = 255;
if (xmethod < ymethod) {
applyMethod(c, xmethod, i, width, xvals);
applyMethod(c, ymethod, j, height, yvals);
} else {
applyMethod(c, ymethod, j, height, yvals);
applyMethod(c, xmethod, i, width, xvals);
}
pixels[index++] = ((c[3] << 24) | (c[0] << 16) | (c[1] << 8)
| c[2]);
}
// Poll once per row to see if we've been told to stop.
if (runner != me) {
return null;
}
}
return createImage(new MemoryImageSource(width, height,
ColorModel.getRGBdefault(), pixels, 0, width));
}
/**
* Calculates and returns the image. Halts the calculation and returns
* null if the Applet is stopped during the calculation.
*/
private Image calculateImage() {
Thread me = Thread.currentThread();
int width = canvas.getSize().width;
int height = canvas.getSize().height;
int xvals[] = new int[2];
int yvals[] = new int[2];
int xmethod = XControls.getParams(xvals);
int ymethod = YControls.getParams(yvals);
int pixels[] = new int[width * height];
int c[] = new int[4]; //temporarily holds R,G,B,A information
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
c[0] = c[1] = c[2] = 0;
c[3] = 255;
if (xmethod < ymethod) {
applyMethod(c, xmethod, i, width, xvals);
applyMethod(c, ymethod, j, height, yvals);
} else {
applyMethod(c, ymethod, j, height, yvals);
applyMethod(c, xmethod, i, width, xvals);
}
pixels[index++] = ((c[3] << 24) | (c[0] << 16) | (c[1] << 8)
| c[2]);
}
// Poll once per row to see if we've been told to stop.
if (runner != me) {
return null;
}
}
return createImage(new MemoryImageSource(width, height,
ColorModel.getRGBdefault(), pixels, 0, width));
}
/**
* Calculates and returns the image. Halts the calculation and returns
* null if the Applet is stopped during the calculation.
*/
private Image calculateImage() {
Thread me = Thread.currentThread();
int width = canvas.getSize().width;
int height = canvas.getSize().height;
int xvals[] = new int[2];
int yvals[] = new int[2];
int xmethod = XControls.getParams(xvals);
int ymethod = YControls.getParams(yvals);
int pixels[] = new int[width * height];
int c[] = new int[4]; //temporarily holds R,G,B,A information
int index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
c[0] = c[1] = c[2] = 0;
c[3] = 255;
if (xmethod < ymethod) {
applyMethod(c, xmethod, i, width, xvals);
applyMethod(c, ymethod, j, height, yvals);
} else {
applyMethod(c, ymethod, j, height, yvals);
applyMethod(c, xmethod, i, width, xvals);
}
pixels[index++] = ((c[3] << 24) | (c[0] << 16) | (c[1] << 8)
| c[2]);
}
// Poll once per row to see if we've been told to stop.
if (runner != me) {
return null;
}
}
return createImage(new MemoryImageSource(width, height,
ColorModel.getRGBdefault(), pixels, 0, width));
}