Home » Java » Displaying an image on JFrame window

Displaying an image on JFrame window

Posted by: admin July 23, 2018 Leave a comment

Questions:

need help with trying to display an image on a JFrame.
been using the code below, and only the window displays, and no images appear. i have also attached how my project is organized.


public class images extends JPanel {
    public void paint(Graphics g){
        Graphics g2 = (Graphics2D) g;
        Image img1 = Toolkit.getDefaultToolkit().getImage("sam.png");
        g2.drawImage(img1, 10, 10, this);
        g2.finalize();
    }

    public static void main(String[] a){
        JFrame frame = new JFrame("help");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(30, 30, 300, 300);
        frame.getContentPane().add(new images());
        frame.setVisible(true);
    }
}
Answers: