You should draw in a JPanel
:
JPanel panel = new JPanel()
{
@Override
protected void paintComponent(Graphics g)
{
// TODO Auto-generated method stub
super.paintComponent(g);
g.drawOval(10, 10, 100, 100);
}
};
Don't forget to add the JPanel
to the JFrame
:
add(panel);
Code:
public DrawOn()
{
JPanel panel = new JPanel()
{
@Override
protected void paintComponent(Graphics g)
{
// TODO Auto-generated method stub
super.paintComponent(g);
g.drawOval(10, 10, 100, 100);
}
};
add(panel);
setPreferredSize(new Dimension(200, 200));
setVisible(true);
pack();
}
Note: You could make a class that extends JPanel
instead of using an anonymous class so your code is clearer.