I was studying graphics and tried to use PaintComponent
to draw some shapes, following is the code. I am trying for an hour but still its not working really can't get reason. What is the resolution to this simple problem?
public class MyPainting extends JPanel
{
public void PaintComponent (Graphics g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(100, 100, 10, 20);
}
public static void main (String [] args)
{
MyPainting p = new MyPainting();
JFrame f= new JFrame();
f.setSize(300,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.setVisible(true);
}
}
When I run program there is empty JFrame
, I did try it g.drawString, ImageIcon
but every time nothing is visible.
The method PaintComponent
is not defined in any of the super classes of JPanel
. You want paintComponent
@Override
public void paintComponent (Graphics g)
and add the @Override
annotation to allow compiler check for the correct method.