Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to convert thread.sleep() into a timer in an Applet

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to convert thread.sleep() into a timer in an Applet

    Since I have used the thread.sleep() method, my graphics is updating way to slowly. How would I convert this into a timer?

    The point of the applet is just to move the black dot around the screen based on the arrow keys that are pressed. (I am eventually going to make this into a snake game, but am taking it one step at a time.)

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.Timer;
     
    import java.applet.*;
    import java.util.*;
    import static java.lang.System.*;
     
    public class SnakeSimple extends Applet implements KeyListener{
     
    	int x = 0;
    	int y = 0;
    	byte direction;
    	final byte UP = 1;
    	final byte RIGHT = 2;
    	final byte DOWN = 3;
    	final byte LEFT = 4;
     
    	public void init() {
    	    x = 100;
    	    y = 100;
    	    addKeyListener(this); 
    	}
    	public void paint(Graphics g) {
    		g.setColor(Color.black);
    		g.fillOval(x, y, 10, 10);
    		run();
    	}
    	public void run(){
    		try {
    			Thread.sleep(100);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    		if(direction==UP){
    			y-=10;
    		}
    		if(direction==DOWN){
    			y+=10;
    		}
    		if(direction==LEFT){
    			x-=10;
    		}
    		if(direction==RIGHT){
    			x+=10;
    		}
     
    		repaint();
     
     
    	}
    	@Override
    	public void keyPressed(KeyEvent e) {
    		if(e.getKeyCode()==37){
    			direction = LEFT;
    		}
    		if(e.getKeyCode()==38){
    			direction = UP;
    		}
    		if(e.getKeyCode()==39){
    			direction = RIGHT;
    		}
    		if(e.getKeyCode()==40){
    			direction = DOWN;
    		}
    		repaint();
    	}
    	@Override
    	public void keyReleased(KeyEvent e) {
     
    	}
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
    }


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: How to convert thread.sleep() into a timer in an Applet

    In the init() method, create a Swing Timer with the delay you want and with an ActionListener that calls the run() method. Delete the Thread.sleep from the run method. After the Timer is created, call its start() method. Don't call the run() method from paint().

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to convert thread.sleep() into a timer in an Applet

    On top of what dlorde said, the reason things are running so slowly is that you're blocking the EDT by sleeping from your paint method, which is called by the EDT. The EDT controls things like events and painting, so blocking that thread will cause those things to slow down. If you have no idea what I'm talking about, google is your friend. Hint: EDT stands for "Event Dispatching Thread".

    A Swing Timer (and switching to JApplet) is the way to go. But just to demonstrate the point, if you put that run() method in a forever loop in another Thread, I think you'd see the effect you're looking for.

    PS- I do want to congratulate you on your "one thing at a time" approach. That's definitely the way to go.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Giving Thread.sleep( ); a decimal value
    By Knox in forum Threads
    Replies: 2
    Last Post: April 9th, 2011, 08:55 PM
  2. [SOLVED] Thread.sleep() in while loop not working
    By mds1256 in forum Threads
    Replies: 4
    Last Post: January 13th, 2011, 06:02 AM
  3. Thread Sleep, Timer, Button Question
    By tabutcher in forum Java Theory & Questions
    Replies: 1
    Last Post: May 1st, 2010, 02:54 AM
  4. Thread/timer problem
    By korbal in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2010, 05:59 PM
  5. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM

Tags for this Thread