import java.awt.*; import java.awt.event.*; import java.applet.*; public class ThreadApplet extends Applet implements Runnable { /** * When this class is ran as an application, how wide should it be. */ public static final int IDEAL_WIDTH = 200; /** * When this class is ran as an application, how tall should it be. */ public static final int IDEAL_HEIGHT = 200; private Thread thread; public void init() { thread = new Thread(this); thread.start(); } public void paint(Graphics g) { } public void run() { Graphics g = getGraphics(); for (int i = 0; i <= 1000; i++) { g.setColor(Color.BLACK); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.WHITE); g.drawString("Counting: " + i, 10, 10); try { Thread.sleep(1000); } catch (InterruptedException e) { return; } } } public static void main(String args[]) { Applet applet = new ThreadApplet(); Frame frame = new Frame(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.add(applet); frame.setSize(IDEAL_WIDTH, IDEAL_HEIGHT); frame.show(); } }