Picture in a JFrame problem
I have inserted a picture in a JFrame. The problem is that the picture isn't appearing. Can someone pls tell me whats missing in my code? Thanks a lot.
This is my code:
Code:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.util.Calendar;
import java.awt.image.*;
import javax.imageio.*;
public class GQ extends JFrame implements ActionListener {
private static final int FRAME_WIDTH = 1024;
private static final int FRAME_HEIGHT = 768;
private static final int FRAME_X_ORIGIN = 0;
private static final int FRAME_Y_ORIGIN = 0;
public static void main (String[] args) {
GQ frame = new GQ();
frame.setVisible(true);
}
public GQ() {
Container contentPane;
contentPane = getContentPane();
contentPane.setBackground(Color.green);
contentPane.setLayout(null);
image = new JPanel();
contentPane.add(image);
}
class test4 extends JPanel {
BufferedImage image;
int w,h;
public test4() {
try {
image = ImageIO.read(new File("geo2.jpg"));
w = image.getWidth();
h = image.getHeight();
} catch (IOException ioe) {
System.out.println(ioe);
System.exit(0);
}
}
public Dimension getPreferredSize() {
return new Dimension(w,h);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image,0,0,this);
}
public void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test4 t = new test4();
t.setLayout(new BorderLayout());
f.add(t);
f.pack();
f.setVisible(true);
}
}
Thanks a lot.