【发布时间】:2011-09-03 15:53:30
【问题描述】:
我打算在窗口内显示一个 28x28 像素的图像。像素具有“0”值,所以我希望它显示一个带有 28x28 的黑色方块的窗口。但是没有显示图像。也许数组的数据(我不确定像素值是否必须是 0 到 255 范围内的 int)必须是其他数据才能显示图像。谢谢!
公开课 ASD {
public static Image getImageFromArray(int[] pixels, int width, int height) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
WritableRaster raster = (WritableRaster) image.getData();
System.out.println(pixels.length + " " + width + " " + height);
raster.setPixels(0,0,width,height,pixels);
return image;
}
public static void main(String[] args) throws IOException {
JFrame jf = new JFrame();
JLabel jl = new JLabel();
int[] arrayimage = new int[784];
for (int i = 0; i < 28; i++)
{ for (int j = 0; j < 28; j++)
arrayimage[i*28+j] = 0;
}
ImageIcon ii = new ImageIcon(getImageFromArray(arrayimage,28,28));
jl.setIcon(ii);
jf.add(jl);
jf.pack();
jf.setVisible(true);
}
【问题讨论】: