views:

18

answers:

1

Code :

import javax.swing.*;
import java.awt.*;

public class firstGUI extends JPanel {

    public static void main(String[] args) {
        JFrame frame =  new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);
        frame.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        Image image = new ImageIcon("dist.jpg").getImage();
        g.drawImage(image,0,0, this);
    }
}

Compiles perfectly, but when I run it, it just shows a form. No picture(or any other operation in paintComponent) shows up. Is there something I'm missing?

+3  A: 

Your paintComponent method is an instance method of your firstGUI class (a JPanel). The problem is that you are not creating an instance of firstGUI and adding it to the frame.

The following replacement main method instantiates firstGUI and adds it to the contentPane of the frame:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.getContentPane().add(new firstGUI());
    frame.setVisible(true);
}
Phil Ross
MoonStruckHorrors