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.

Try some testing. Load the same image in the new ImageIcon() calls and see if there is a problem with the java code vs the images.

I notice that this problem happens when I am using bmp (or I rename a bmp file to some other format). So if I draw the image and save it as png or something else, then this problem does not arise.

I guess then Image does not support bmp format.

I didnt know that. I thought windows would automatically convert the file

This article has been dead for over six months. Start a new discussion instead.