更改像素后 JAVA 中的图像实现

Image actualization in JAVA after pixels are changed

我有这样的问题:我已经在JAVA程序中准备好从图片中保存一些数据并将它们保存到txt文件中。然后程序应该将每 25 行中图片的所有像素都变成黑色,并在显示器上实现图片(已经有黑线)。但是出了点问题,我不知道是什么 - 整个图片从显示中删除,没有显示任何内容。这是代码:

private void saveButtonActionPerformed(java.awt.event.ActionEvent evt)  

        ..........

      BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(), in.getType());
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j=j+25) {
                    out.setRGB(i,j,0);  
                }
            }

            ImageIcon img = new ImageIcon(out);

            imagePanel.removeAll();
            imagePanel.setIcon(img);

        } catch (IOException e) { 
            System.out.print("ERROR");  
        }
    }
}                                          

public static BufferedImage loadImage(File file) {
    try {            
        BufferedImage out = ImageIO.read(file);
        return out;
    } catch (IOException e) {
        return null;
    }
}

我使用的是 NetBeans,但一切正常。

只需在调用imagePanel.setIcon(img);后添加imagePanel.revalidate();。您必须告诉 Java 它必须评估您的更改并在必要时重新绘制组件。

您创建了一个空图像并开始在每第 25 行绘图,实际上您应该从原始图像开始并开始在其中绘图。

 // you create an empty image with same width and height of the original
    //BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(), in.getType());
      BufferedImage out = ImageIO.read(new File("path/to/Original/image")); 
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j=j+25) {
                out.setRGB(i,j,0);  
            }
        }

achabahe and Erwin Bolwidt 是对的 - 缺少图像。 我已经用如此简单的方法解决了它:

 BufferdImage out = in;

我没有刷新内存,所以我可以再次使用它。也许不是最好的解决方案,但有效。