oval leaves the trail

2019-06-23 23:55发布

问题:

I am trying to make a simple ball animation, that starts from 1 corner and goes to another corner of the panel. I have written a program for that.

When I run the program the oval or ball leaves the trail. What I mean to say is that it leaves it's 'color trail' when the program runs. In my program timer fires an event every 100 milliseconds.

The following is the logic responsible for running the code :

void function() {
  // in this there is a action listener timed accordingly to fire event of 
  // doing x++ every 100th miliseconds
}

public void paintComponent(final Graphics g) {
 g.setColor(Color.black);
 g.drawOval(x,y,width,height);
 g.fillOval(x,y,width,height);
}

Screen shot of the output :

回答1:

Try

public void paintComponent(final Graphics g) {
 super.paintComponent(g);

 g.setColor(Color.black);
 g.drawOval(x,y,width,height);
 g.fillOval(x,y,width,height);
}