Results 1 to 5 of 5
  1. #1
    anro is offline Member
    Join Date
    Sep 2009
    Location
    Germany- Bayern
    Posts
    2
    Rep Power
    0

    Default Is there a way to speed up these Threads? (Rendering Problems)

    Hi everyone!

    I'm new to your forum and I logged in because I need some help with my threads.

    Im writing a game inside a JFrame using Netbeans' swing. In that game there are several Ants running around.
    To set them alive I invoke Threads on them, that do the run() method. I want them to be rendered realtime.

    My problem is, that the Ants I use are very, very slow. If I use 20 of them the prog is lagging as hell.

    Maybe someone has a idea how to make more than 100 Threads run smoth.
    Here's some pseudocode:
    PHP Code:
    public Ant(Node pos) {
      this.pos = pos;
      this.trace = new Vector<Node>(); trace.add(pos)
    } // constructor
    public void run() {
      //timer uses the move(Node n) method, delay = 500;
    }
    public void move(Node n) {
       pos = n; trace.add(n);
       drawPosition();
    }
    public synchronized void drawPosition() {
     // renders a BufferedImagefile and displays output to panel
     // this is called by every Thread every delay millisecs, so it is
     // almost always active
    }
    public void main() { // not really main but this is what happens with my ants
      Ant a = new Ant(1,1);
      Vector<Thread> threads = new Vector<Thread>();
      Thread t1 = new Thread(a); threads.add(t1); t1.start();
    }
    The Ants need to have a way to save Information on where they already have been and additionally need to have a timer to not run away too fast.

    anro

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Why have them as separate threads?
    I can sort of see the thinking, but I suspect you're sticking a load of overhead on that you don't actually need.
    Try a single thread that simply cycles through the ants and moves them, presumably informing the EDT when a move cycle is complete.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default

    Here is an example that uses thousands of animated balls:

    forums.sun.com/thread.jspa?threadID=5339511&start=1

  4. #4
    anro is offline Member
    Join Date
    Sep 2009
    Location
    Germany- Bayern
    Posts
    2
    Rep Power
    0

    Default

    Thank you for your advice!!:D

    I took a look at the balls example and learned a lot from it.
    And it works!!

    First I kept repainting the Panel for each ant and the problem remained the same.
    Then I just repainted the Panel every delay millisecs and now I am able to let up to 1000 ants live.

    It seems that the panels draw method is very expensive.

    Sincerely yours.
    anro

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Yep.
    As that thread points out you want to minimise the calls to draw.
    Do your drawing behind the scenes (buffering).

Similar Threads

  1. Problem in JSP rendering
    By srkumarj2ee@gmail.com in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 02:02 AM
  2. JTextPane Font's with AffineTransform Rendering
    By StormyWaters in forum AWT / Swing
    Replies: 7
    Last Post: 09-18-2009, 09:56 PM
  3. manual BMP rendering colour issues
    By Goseph in forum AWT / Swing
    Replies: 0
    Last Post: 02-24-2009, 08:26 PM
  4. Help with JTable rendering
    By daniel2008 in forum AWT / Swing
    Replies: 8
    Last Post: 01-18-2009, 03:51 AM
  5. JLabel Rendering
    By random4534 in forum New To Java
    Replies: 3
    Last Post: 12-16-2008, 08:55 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
  •