Results 1 to 3 of 3
  1. #1
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default 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.

  2. #2
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Thumbs up

    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();

  3. #3
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Default

    It is much useful as for painting especially if you place it in a sep. thread

Similar Threads

  1. question about painting efficiently
    By gib65 in forum AWT / Swing
    Replies: 16
    Last Post: 07-04-2010, 08:18 PM
  2. How can I do this more efficiently ?
    By chucklesotoole in forum New To Java
    Replies: 5
    Last Post: 06-04-2010, 02:28 PM
  3. Repainting From Another Class
    By habester in forum New To Java
    Replies: 1
    Last Post: 11-13-2009, 02:29 AM
  4. [SOLVED] Repainting in a thread
    By Kephix in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-19-2009, 06:13 AM
  5. repainting lines separately
    By rstepler in forum Java 2D
    Replies: 8
    Last Post: 07-07-2008, 02:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •