User Tools

Site Tools


lang:judo:english:tutorial-en-016

[ Intro | Begin | if/else | Loops | Arrays | Graphics | Animation | Mouse | Game | Real | Methods | Class | Class 2 | Applet | MouseClick | Thread | Button ]


Simple Java Animation Using Threads

    import java.awt.*;
    import java.applet.*;
    public class TestAnim1 extends Applet implements Runnable {
	// Declare a Thread object
	Thread count;
	int x;
	int y;
 
	// initialize applet
	public void init()
	{
	    x = 0;
	    y = 20;
	    setBackground(Color.yellow);
 
	    // init thread
	    count = new Thread(this);
 
	    // start the thread
	    count.start();
	}
 
	// Thread code, overrides run method of Runnable
	public void run()
	{
	    while (true) {
	        try {
		    // delay
	            count.sleep(100);
	            x++;
 
	            // call paint method
	            repaint();
	        }
		catch(Exception e) {} // do nothing
	    }
	}
 
	// paint method
	public void paint(Graphics g) {
	    g.setColor(Color.green);
	    g.fillOval(x, y, 30, 30);
	}
    }
lang/judo/english/tutorial-en-016.txt ยท Last modified: 2007/02/08 09:56 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki