import java.awt.*; import java.applet.*; /* By Futrelle for COM1370 7/6/01 All that's required for timing is to implement the Runnable interface with the methods start() and run() and an instance variable of type Thread. It's that simple! In this example, a pair of lines is moved in mirror image fashion, 20 times per second for 5 seconds (100 steps). */ public class MoveLine extends Applet implements Runnable { Thread runner; int step; public void init() { setBackground(Color.white); } // The following is queued by repaint() to be called. public void paint(Graphics g) { g.setColor(Color.magenta); // color for line g.drawLine(100 + 2*step, 200 + 3*step, 200 - step, 50 + 2*step); // interchange x and y to give mirror image line g.drawLine( 200 + 3*step, 100 + 2*step, 50 + 2*step, 200 - step); g.setColor(Color.black); // color for text g.drawString("R. Futrelle COM1370 Sm2001 Timed Lines Demo, 7/6/01", 10, 490); g.drawLine(0,0,500,500); } // paint() public void start() { runner = new Thread(this); runner.start(); } // start() public void run() { for (step = 0; step < 100 ;step++) { try { Thread.sleep(50); } catch (InterruptedException e) { } repaint(); } } }