import java.applet.Applet; import java.awt.*; public class ticker6 extends Applet implements Runnable { String myText; Thread myThread; Font myFont; FontMetrics myMetrics; Image myImage; Graphics myOffsceenGraphics; Color myColor; int myTextLength; int XCoord = 0; int mySleepPeriod = 50; int myAdvance = 2; boolean myThreadStopped = false; Button Stop = new Button("Stop"); Button Start = new Button("Start"); public void init() { myText=getParameter("text"); myThread = null; myThreadStopped = false; myFont = new Font("Helvetica",1,24); myMetrics = getFontMetrics(myFont); myTextLength = myMetrics.stringWidth(myText); myColor = Color.blue; myImage = createImage(size().width,size().height); myOffsceenGraphics = myImage.getGraphics(); setLayout(new BorderLayout()); Panel myPanel = new Panel(); myPanel.setLayout(new FlowLayout()); myPanel.add(Start); myPanel.add(Stop); add("South",myPanel); } public void newCoordinates() { if (XCoord < -myTextLength) { XCoord = size().width; } else { XCoord = XCoord - myAdvance; } } public void start() { if (myThread == null) { myThread = new Thread(this,"MyThread"); myThread.start(); } } public void run() { while (myThread != null) { try { myThread.sleep(mySleepPeriod); } catch (InterruptedException e) {} newCoordinates(); repaint(); } } public void updateImage(Graphics g) { g.setColor(Color.black); g.fillRect(0,0,size().width, size().height); g.setColor(myColor); g.setFont(myFont); g.drawString(myText,XCoord,25); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { updateImage(myOffsceenGraphics); g.drawImage(myImage,0,0,null); } public void stop() { myThread.stop(); myThread = null; } public boolean mouseDown(Event e, int x, int y) { if (myThreadStopped) { myThread.resume(); myThreadStopped = false; } else { myThread.suspend(); myThreadStopped = true; } return true; } public boolean action(Event e, Object arg) { if (("Stop".equals(arg)) && (!myThreadStopped)) { myThread.suspend(); myThreadStopped = true; } if (("Start".equals(arg)) && (myThreadStopped)) { myThread.resume(); myThreadStopped = false; } return true; } }