Results 1 to 5 of 5
- 09-29-2009, 02:11 PM #1
Member
- Join Date
- Sep 2009
- Location
- Germany- Bayern
- Posts
- 2
- Rep Power
- 0
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:
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.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(); }
anro
- 09-29-2009, 02:41 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 09-29-2009, 05:12 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Here is an example that uses thousands of animated balls:
forums.sun.com/thread.jspa?threadID=5339511&start=1
- 09-29-2009, 11:50 PM #4
Member
- Join Date
- Sep 2009
- Location
- Germany- Bayern
- Posts
- 2
- Rep Power
- 0
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
- 09-30-2009, 09:49 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Problem in JSP rendering
By srkumarj2ee@gmail.com in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-27-2009, 02:02 AM -
JTextPane Font's with AffineTransform Rendering
By StormyWaters in forum AWT / SwingReplies: 7Last Post: 09-18-2009, 09:56 PM -
manual BMP rendering colour issues
By Goseph in forum AWT / SwingReplies: 0Last Post: 02-24-2009, 08:26 PM -
Help with JTable rendering
By daniel2008 in forum AWT / SwingReplies: 8Last Post: 01-18-2009, 03:51 AM -
JLabel Rendering
By random4534 in forum New To JavaReplies: 3Last Post: 12-16-2008, 08:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks