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