Results 1 to 8 of 8
- 10-28-2011, 11:34 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
Problem with Thread.sleep() and repaint()!!!
Hi, I'm working on my own version of the Snake game and I have a problem. I want that when "the Snake" collides with itself, change its color to red, wait 1 or 2 seconds and reset the game. For this I used Thread.sleep, but it does not work as I want. First it waits x time, then changes the color, and almost instantaneously restarts the game.
Here's the code:
- Init (): what it does is reset the values of variables, and so on. and then repaint ();Java Code:public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.gray); g.fillRect(0, 0, 500, 500); try { Thread.sleep(t); } catch (InterruptedException p) { p.printStackTrace(); } if (k != 0 && !over) { switch (k) { case 1: xa = 10; ya = 0; break; case 2: xa = -10; ya = 0; break; case 3: xa = 0; ya = 10; break; case 4: xa = 0; ya = -10; break; } puntos.poll(); puntos.addLast(new Point(puntos.getLast().x + xa, puntos.getLast().y + ya)); food(g); for (z = puntos.size() - 2; z > 0; z--) { if (puntos.getLast().x == puntos.get(z).x && puntos.getLast().y == puntos.get(z).y) { over = true; System.out.println("PERDISTE"); } } } if (over) { System.out.println("OVER TRUE"); for (i = 0; i < puntos.size(); i++) { g.setColor(Color.red); g.fillRect(puntos.get(i).x, puntos.get(i).y, 10, 10); g.setColor(Color.black); g.drawRect(puntos.get(i).x, puntos.get(i).y, 10, 10); } try { Thread.sleep(1500); init(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (!over) { System.out.println("OVER FALSE"); for (i = 0; i < puntos.size(); i++) { g.setColor(Color.green); g.fillRect(puntos.get(i).x, puntos.get(i).y, 10, 10); g.setColor(new Color(67, 135, 22)); g.fillRect(puntos.getLast().x, puntos.getLast().y, 10, 10); g.setColor(Color.black); g.drawRect(puntos.get(i).x, puntos.get(i).y, 10, 10); } paintfood(g); repaint(); } }
-food () and paintfood () are for the food of the Snake.
- "points" is the list containing the positions of each square of Snake
I think the problem is in how I used Thread.sleep () and where I put it, I hope you can help!
Thank you!
- 10-28-2011, 11:44 PM #2
Re: Problem with Thread.sleep() and repaint()!!!
Do NOT call Thread.sleep() while on the EDT. Use a Swing Timer to accomplish this instead.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 10-28-2011, 11:46 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Re: Problem with Thread.sleep() and repaint()!!!
a) Never call Thread.sleep in the paintComponent method - this is called to from the EDT and will result in your component not updating until the sleep is complete b) Never call repaint from within paintComponent, as repaint basically queue's up another call paintComponent. If you want to do animation, then use a swing Timer or a separate thread (if the latter, make sure calls to Swing components get placed on the EDT).
- 10-29-2011, 12:47 AM #4
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
Re: Problem with Thread.sleep() and repaint()!!!
Fine, but how do I use a Timer? I've been looking on the web, but I can't find anything I can understand. By the way, I'm a begginer and all my knowledge is taken from tutorials, videos, etc., so I tried to use everything I remembered. I'd never heard about a Swing Timer!
Thanks again!
- 10-29-2011, 01:12 AM #5
Re: Problem with Thread.sleep() and repaint()!!!
First result for googling "java swing timer": How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
-
Re: Problem with Thread.sleep() and repaint()!!!
- 10-30-2011, 09:25 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 9
- Rep Power
- 0
Re: Problem with Thread.sleep() and repaint()!!!
I finally worked it out. Thanks for your help!!
But I have another question: How can I put a JLabel on the JPanel? It would show the score? I tried adding the JLabel to the JPanel, but I can't see it!
Thanks again!
PS: Do I have to start a new thread for this question?
-
Re: Problem with Thread.sleep() and repaint()!!!
Similar Threads
-
Thread.sleep, Event, Repaint(), problems
By erversteeg in forum New To JavaReplies: 1Last Post: 12-04-2010, 09:56 PM -
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
how to reduce the thread sleep time and wake up the thread
By baktha.thalapathy in forum Threads and SynchronizationReplies: 2Last Post: 06-24-2010, 07:36 PM -
Do I have to put Thread to sleep?
By atch in forum New To JavaReplies: 0Last Post: 03-08-2010, 10:21 AM -
How to use the sleep and thread?
By jiuhu in forum Java AppletsReplies: 4Last Post: 08-07-2007, 02:56 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks