import javax.swing.JPanel;
import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Graphics2D;
class View extends JPanel {
Image icon1,icon2;
public View(){
ImageIcon i1=new ImageIcon(this.getClass().getResource("tile1.png"));//image I drew
ImageIcon i2=new ImageIcon(this.getClass().getResource("tile2.png"));//image I downloaded
icon1=i1.getImage();
icon2=i2.getImage();
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2=(Graphics2D)g;
g2.drawImage(icon1,10,10,null);//this image is not displayed
g2.drawImage(icon2,200,200,null);//this image is displayed
}
}
In the above code, I'm simply trying to load some images and display it in the window.
Now, I drew an image using MSPaint and saved it in bmp/png (icon1). And I downloaded another image off the internet in gif/png (icon2). The images are present in the same directory as the code.
BUT when I run the program to display the images, I see that only icon2 is being displayed. icon1 is not displayed.
Why is this so?
The drawImage() method returns a true value on success. However, for the icon1 it returns false and for icon2 it returns true.