接收的是图像的像素数据的话,MemoryImageSource来产生图像就行了. int w = 100; int h = 100; int pix[] = new int[w * h]; int index = 0; for (int y = 0; y < h; y++) { int red = (y * 255) / (h - 1); for (int x = 0; x < w; x++) { int blue = (x * 255) / (w - 1); pix[index++] = (255 << 24) | (red << 16) | blue; } }图像像素数据存放在数组pix中 Image img = createImage(new MemoryImageSource(w, h, pix, 0, w));
import java.awt.*; import javax.swing.*; import java.awt.image.*;public class DrawPixelImage extends JPanel { protected int width = 400; protected int height = 400; protected int pixel[]; protected MemoryImageSource source; protected Image image;
public DrawPixelImage() { pixel = new int[height * width];
for (int index = 0, y = 0; y < height; y++) { int red = (y * 255) / (height - 1); for (int x = 0; x < width; x++) { int blue = (x * 255) / (width - 1); pixel[index++] = (255 << 24) | (red << 16) | blue; } } source = new MemoryImageSource(width, height, pixel, 0, width); image = this.createImage(source); }
public int getWidth() {return width;} public int getHeight() {return height;}
int w = 100;
int h = 100;
int pix[] = new int[w * h];
int index = 0;
for (int y = 0; y < h; y++) {
int red = (y * 255) / (h - 1);
for (int x = 0; x < w; x++) {
int blue = (x * 255) / (w - 1);
pix[index++] = (255 << 24) | (red << 16) | blue;
}
}图像像素数据存放在数组pix中
Image img = createImage(new MemoryImageSource(w, h, pix, 0, w));
import javax.swing.*;
import java.awt.image.*;public class DrawPixelImage extends JPanel {
protected int width = 400;
protected int height = 400;
protected int pixel[];
protected MemoryImageSource source;
protected Image image;
public DrawPixelImage() {
pixel = new int[height * width];
for (int index = 0, y = 0; y < height; y++) {
int red = (y * 255) / (height - 1);
for (int x = 0; x < width; x++) {
int blue = (x * 255) / (width - 1);
pixel[index++] = (255 << 24) | (red << 16) | blue;
}
}
source = new MemoryImageSource(width, height, pixel, 0, width);
image = this.createImage(source);
}
public int getWidth() {return width;}
public int getHeight() {return height;}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
} private static void createUIAndShow() {
JFrame frame = new JFrame("Memory Image Producer");
DrawPixelImage panel = new DrawPixelImage();
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(panel.getWidth(), panel.getHeight());
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
DrawPixelImage.createUIAndShow();
}
});
}
}
谢谢你,给分了