[ [[tutorial-en-001 | Intro]] | [[tutorial-en-002 | Begin]] | [[tutorial-en-003 | if/else]] | [[tutorial-en-004 | Loops]] | [[tutorial-en-005 | Arrays]] | [[tutorial-en-006 | Graphics]] | [[tutorial-en-007 | Animation]] | [[tutorial-en-008 | Mouse]] | [[tutorial-en-009 | Game]] | [[tutorial-en-010 | Real]] | [[tutorial-en-011 | Methods]] | [[tutorial-en-012 | Class]] | [[tutorial-en-013 | Class 2]] | [[tutorial-en-014 | Applet]] | [[tutorial-en-015 | MouseClick]] | [[tutorial-en-016 | Thread]] | [[tutorial-en-017 | 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); } }