I'm trying to display a simple image using JFrame and JComponent. I've read through some of the existing questions and answers but it's no use. The following are my codes of my subclasses to JComponent and JFrame:
import java.awt.*;
import javax.swing.JComponent;
public class FaceComponent extends JComponent {
public FaceComponent() {
}
public void painComponent(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.YELLOW);
g.fillOval(60, 60, 200, 200);
g.setColor(Color.MAGENTA);
g.fillOval(90, 100, 40, 20);
g.fillOval(180, 100, 40, 20);
g.setColor(Color.PINK);
g.drawLine(120, 160, 140, 180);
g.drawLine(140, 180, 160, 160);
g.drawLine(120, 160, 140, 200);
g.drawLine(140, 200, 160, 160);
g.setColor(Color.BLACK);
g.setFont(new Font("SansSerif", Font.ITALIC, 24));
g.drawString("Whatsup", 30, 200);
}
}
Here's my FaceWindow with JFrame imported:
import javax.swing.JFrame;
public class FaceWindow {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("lab09ex1");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setVisible(true);
FaceComponent fc = new FaceComponent();
frame.add(fc);
}
}
But it ends out with an empty grey display screen instead of what I'm input in my program. What's the matter? Sorry to be not specific in this question but it's the only way to show you my current situation.