import java.applet.*; import java.awt.*; /* Threads allow multiple events to occur simultaneously, for example: in a game of pong, two paddles can move at the same time as a bouncing ball */ public class MovingText extends Applet implements Runnable { // notice the "implements runnable", this is needed for using threads public Font myFont; Thread t; // create a field that will be used for an instance of a thread public void init() { myFont=new Font("Helvetica", Font.PLAIN, 20); setFont(myFont); setBackground(Color.black); setForeground(Color.red); t=new Thread(this); // instantiate a new thread object t.start(); // start our thread running } public void run() { while(true) { // we will loop until the web page is closed in the browser repaint(); // repaint the screen, it will call the paint function again try{ t.sleep(500); // pause before we redraw }catch(InterruptedException e){ ; } // the try catch statement is used to trap errors // we need to trap an error if a problem happens with our thread } } public void paint(Graphics g) { int x=(int)((Math.random()*1000)%size().width); int y=(int)((Math.random()*1000)%size().height); Color randomColor; int red=(int)((Math.random()*1000)%256); int green=(int)((Math.random()*1000)%256); int blue=(int)((Math.random()*1000)%256); randomColor = new Color(red, green, blue); g.setColor(randomColor); g.drawString("Carman", x, y); } }