0
我编写了一个程序,用于从命令行读取图像,并且想要读取每个像素以绘制相应颜色的矩形,以便从矩形“重新创建”图像。使用矩形绘制图像
但是,尽管矩形具有正确的大小,但每个像素似乎都是黑色的。至少,我在输出面板上看到的是一张黑色图片,其尺寸与输入图片相同。
class AppDrawPanel extends JPanel {
private BufferedImage bi;
/* ... */
public void loadAPPImage(String s) throws IOException{
bi = ImageIO.read(new File(s));
}
@Override
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
int w = bi.getWidth();
int h = bi.getHeight();
for(int x = 0; x < w; x++){
for (int z = 0; z < h; z++){
Color c = new Color(bi.getRGB(x, z));
super.setForeground(c);
g2.fillRect(x, z, 3, 3);
}
}
}
}
和主要功能:
public static void main(String[] args) throws IOException{
/* ... */
AppDrawPanel draw = new AppDrawPanel();
draw.loadAPPImage(args[0]);
frame.add(draw);
/* ... */
}
其中/* ... */
表示无关与绘制矩形或读取图像代码。
1)为了更好地提供帮助,请发布[MCVE](http://stackoverflow.com/help/mcve)(最小完整和可验证示例)。 2)获取图像的一种方法是通过热链接到[本答案](http://stackoverflow.com/a/19209651/418556)中看到的图像。 3)鉴于这是一个问答网站,最好从一个明确的问题,如“如何画盒子,使它们不是黑色的?或“错误是什么?”。你的问题是什么? –
'super.setForeground(c);'应该只是'g2.setColor(c)'。 – Marco13
@ Marco13谢谢你,它的作品:) @安德鲁汤普森谢谢你的建议:) – TheWaveLad