Results 1 to 3 of 3
Thread: repainting more efficiently
- 08-22-2010, 05:44 PM #1
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
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:
steps++;
xLocation=(int) route.get(steps).getX();
yLocation=(int) route.get(steps).getY();
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?Last edited by imorio; 08-22-2010 at 05:47 PM.
- 08-24-2010, 04:22 AM #2
you can invoke inner component methods to paint
...Java Code:It is a common thing as for graphics JPanel panel=new JPanel() { public void paintComponent(Graphics g) { System.out.println("panel repainted"); } };
Java Code:panel.repaint();
- 08-24-2010, 04:24 AM #3
Similar Threads
-
question about painting efficiently
By gib65 in forum AWT / SwingReplies: 16Last Post: 07-04-2010, 08:18 PM -
How can I do this more efficiently ?
By chucklesotoole in forum New To JavaReplies: 5Last Post: 06-04-2010, 02:28 PM -
Repainting From Another Class
By habester in forum New To JavaReplies: 1Last Post: 11-13-2009, 02:29 AM -
[SOLVED] Repainting in a thread
By Kephix in forum Threads and SynchronizationReplies: 2Last Post: 04-19-2009, 06:13 AM -
repainting lines separately
By rstepler in forum Java 2DReplies: 8Last Post: 07-07-2008, 02:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks