I want to create FinalImage with using 2 images (after load from file) and show it. So I created class:
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);
}
}
}
But when I repaint() this class I always get null from finalImage. I used deepCopy but this method don't change anything. I also check what toString method is giving and everything is okej (its give me normal width and height for finalImage)
Somebody know why null is always giving in paintComponent method?
Thanks for help :)
Your comment shows your problem. You've two ImagPanel objects, one displayed and with null images and the other not displayed and with non-null images.
Solution: create only one object. Give it a setImages(Image img1, Image img2)
method and call it when you need to set images.
Also as per my comment change your super method call.