Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Problem with Thread.sleep() and repaint()!!!

  1. #1
    chrishans is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default 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:
    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();
    		}
    
    
    	}
    - Init (): what it does is reset the values ​​of variables, and so on. and then 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!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,858
    Rep Power
    8

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

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

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

  4. #4
    chrishans is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

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

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,858
    Rep Power
    8

    Default Re: Problem with Thread.sleep() and repaint()!!!

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with Thread.sleep() and repaint()!!!

    Quote Originally Posted by chrishans View Post
    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!
    Rather than tell us you don't understand Swing Timers, which is an unanswerable statement, not a question, better to tell us what you've read in the tutorials and ask specific questions about the tutorial content that you don't understand.
    doWhile likes this.

  7. #7
    chrishans is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

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

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with Thread.sleep() and repaint()!!!

    Quote Originally Posted by chrishans View Post
    PS: Do I have to start a new thread for this question?
    Yes, it probably it would be best if you did, as this is a completely new problem, one that will require that you show us your new code.

    Congrats on solving the original problem!

Similar Threads

  1. Thread.sleep, Event, Repaint(), problems
    By erversteeg in forum New To Java
    Replies: 1
    Last Post: 12-04-2010, 09:56 PM
  2. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  3. how to reduce the thread sleep time and wake up the thread
    By baktha.thalapathy in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-24-2010, 07:36 PM
  4. Do I have to put Thread to sleep?
    By atch in forum New To Java
    Replies: 0
    Last Post: 03-08-2010, 10:21 AM
  5. How to use the sleep and thread?
    By jiuhu in forum Java Applets
    Replies: 4
    Last Post: 08-07-2007, 02:56 AM

Tags for this Thread

Posting Permissions

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