I'm still new to java graphics, I wrote this little piece of code, but it doesn't show anything on the screen. can someone please tell me what's wrong. I'm not getting any errors, but it just doesn't show anything. Thanks
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.*;
public class GraphicsExample extends JPanel {
public void paintComponenet(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.red);
g.drawRect(10, 10, 200, 200);
for(int i=0; i<20; i++){
g.setColor(new Color((int) (Math.random() *255), (int)(Math.random()*255),(int) (Math.random()*255)));
g.fillOval(20+(int)(Math.random()*180), 20+(int)(Math.random()*180), 5, 5);
}
g.setColor(Color.red);
g.drawString("Hello there", 20, 20);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.add(new GraphicsExample());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,600);
f.show();
}
}
paintComponenet
is missed spelt, it should be paintComponent
Start by using the @Override
annotation when you "think" you're overriding methods...
@Override
public void paintComponenet(Graphics g) {
This will cause a compiler error if the compiler can't find a corresponding method in the parent classes, which will highlight these types of errors...
@Override
public void paintComponent(Graphics g) {