1 回答
TA贡献1828条经验 获得超3个赞
从概念上讲,这个想法很简单。 BufferedImage可能是更好的选择之一,因为它提供了一个您也可以编写的可变缓冲区。它可以显示并输出到磁盘。
您需要解决的问题是将“颜色”转换int为 API 使用的“打包”值。
虽然这实际上是一个相当常见的解决方案,但我看到太多人在实现算法时犯了简单的错误,所以我改用了可用的Color类。它没有那么“高效”,但我不认为您正在尝试生成一个需要每秒运行数百帧的解决方案;)
Random rnd = new Random();
int[][] pixels = new int[128][128];
for (int y = 0; y < 128; y++) {
for (int x = 0; x < 128; x++) {
pixels[y][x] = rnd.nextInt(255);
}
}
BufferedImage img = new BufferedImage(128, 128, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < 128; y++) {
for (int x = 0; x < 128; x++) {
Color color = new Color(pixels[y][x], pixels[y][x], pixels[y][x]);
img.setRGB(x, y, color.getRGB());
}
}
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)));
try {
ImageIO.write(img, "png", new File("SuperDuppa.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
添加回答
举报
