Achint
Achint

Reputation: 817

Image not loading in frame

SO these aer the two different files that Im using:

    public class TestFunc {

    static BufferedImage img;   
    public static void main(String[] args){


        JFrame frame = new JFrame("Display");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);

        ShowImage I = new ShowImage();
        frame.getContentPane().add(I);
        frame.setVisible(true);

    }
}

and the other one:

public final class ShowImage extends JPanel{

Image image;

public ShowImage(){

    super();
    image = Toolkit.getDefaultToolkit().getImage("Sunset.jpg");

}

public void paintComponent(Graphics g){
    g.drawImage(image, 0, 0, this);

}

 }

However, there is nothing being displayed on the frame, and it appears blank. I'm pretty new to this, can't figure it out.

Thanks.

Upvotes: 1

Views: 729

Answers (1)

Shivan Dragon
Shivan Dragon

Reputation: 15219

Try using an ImageIcon inside a JLabel:

JFrame frame = new JFrame("Display");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        frame.setVisible(true);

        JPanel jp = new JPanel();
        frame.add(jp);
        jp.add(new JLabel(new ImageIcon("d:\\temp\\me.JPG")));

        frame.pack();

Upvotes: 2

Related Questions