Whatever you are writing in the paintComponent
method will be painted each time that the method is called. Based on the description so far, the usual way of achieving what you want to achieve is to determine whether something should be painted or not:
class TheClass extends JComponent
{
private boolean paintTheFirstThing = true;
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
if (paintTheFirstThing)
{
g.setColor(Color.BLUE);
g.drawRect(50, 50, 50, 50);
g.drawOval(60, 60, 60, 60);
}
else
{
g.drawOval(10, 10, 10, 10)
}
}
void setPaintTheFirstThing(boolean p)
{
this.paintTheFirstThing = p;
repaint();
}
}
(This is only a sketch, showing the basic idea. Of course, when you want to paint many different things, you'll not create lots of boolean flags for them. The key point is that in your paintComponent
method, you have to describe what should be painted at certain point in time)