我想使用 2 个图像(从文件加载后)创建 FinalImage 并显示它。所以我创建了类:
public class ImagePanel extends JPanel{
private static final long serialVersionUID = 1L;
private BufferedImage firstImage;
private BufferedImage secondImage;
private BufferedImage finalImage;
public ImagePanel(BufferedImage first, BufferedImage second){
if(first != null && second != null){
this.firstImage = deepCopy(first);
this.secondImage = deepCopy(second);
finalImage = new BufferedImage(firstImage.getWidth()*2, firstImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = finalImage.getGraphics();
g.drawImage(firstImage, 0, 0, null);
g.drawImage(secondImage, firstImage.getWidth(), 0, null);
System.out.println("FinalImage"+finalImage.toString());
}
}
private BufferedImage deepCopy(BufferedImage bi) {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
protected void paintComponent(Graphics g) {
super.paintComponents(g);
if(finalImage != null){
g.drawImage(finalImage, 20, 20, null);
}
}
}
但是当我重绘()这个类时,我总是从 finalImage 中得到空值。我使用了 deepCopy,但这种方法不会改变任何东西。我还检查了 toString 方法给出了什么,一切正常(它给了我 finalImage 的正常宽度和高度)
有人知道为什么 null 总是在 paintComponent 方法中给出?
感谢帮助 :)