repainting more efficiently
I'm making a tower defence game. This involves "monsters" (circles in my case) moving over a path, laid out in a grid of squares, to an end point.
The field consists of 30X30 instances of the square class. The path that the monsters take is in an ArrayList<Point> called route. When the Timer in the monster instance gives an ActionEvent, the move() methode is called. That does among others:
So the xLocation and the yLocation move to the next point in the road. Then comes that part the troubles me. The monster his location has changed, but now he needs to be repainted. The first thing that came to mind is calling a methode in the class that paints the JPanel that just says "repaint()". That way the entire field gets repainted and the monster is moved to the next pixel in the road
However, this means that with 10 monsters on the field, all moving at 20 pixels per second, that the entire field of 900 square instances needs to be repainted 200 times per second.So I started thinking about a more complicated solution. Making the painted methode by means of if-statements repaint the square's the monster (partly) covers and repaint any monster that was on those square's including the monster that moved.
However this seemed a rather complicated. Especially if the program is more complicated than mine. So my question is, is there an easier way to display moving circles over a background?