// TickerTape Applet, right out of "Java Programming Language Handbook" import java.applet.*; import java.awt.*; // TickerTape Class public class TickerTape extends Applet implements Runnable{ // Declare Variables String inputText; String animSpeedString; Color color = new Color(255, 255, 255); int xpos; int fontLength; int fontHeight; int animSpeed; Font font; Thread ttapeThread = null; Image im; Graphics osGraphics; boolean suspended = false; // Initialize Applet public void init(){ inputText = getParameter("TEXT"); animSpeedString = getParameter("SPEED"); animSpeed = Integer.parseInt(animSpeedString); im=createImage(size().width, size().height); osGraphics = im.getGraphics(); xpos = size().width; fontHeight = 4 * size().height / 5; font = new Font("Helvetica", 1, fontHeight); } // Override Applet Class' paint method public void paint(Graphics g){ paintText(osGraphics); g.drawImage(im, 0, 0, null); } // Draw background and text on buffer image public void paintText(Graphics g){ g.setColor(Color.black); g.fillRect(0, 0, size().width, size().height); g.clipRect(0, 0, size().width, size().height); g.setFont(font); g.setColor(color); FontMetrics fmetrics = g.getFontMetrics(); fontLength = fmetrics.stringWidth(inputText); fontHeight = fmetrics.getHeight(); g.drawString(inputText, xpos, size().height - fontHeight / 4); } // Start Applet as thread public void start(){ if(ttapeThread == null){ ttapeThread = new Thread(this); ttapeThread.start(); } } // Animate coordinates for drawing text public void setcoord(){ xpos = xpos - animSpeed; if(xpos <- fontLength){ xpos = size().width; } } // Change coordinates and repaint public void run(){ while(ttapeThread != null){ try { Thread.sleep(50); } catch (InterruptedException e){} setcoord(); repaint(); } } // Re-paint when buffer is updated public void update(Graphics g) { paint(g); } // Handle mouse clicks public boolean handleEvent(Event evt) { if (evt.id == Event.MOUSE_DOWN) { if (suspended) { ttapeThread.resume(); } else { ttapeThread.suspend(); } suspended = !suspended; } return true; } // Stop thread then clean up before close public void stop(){ if(ttapeThread != null) ttapeThread.stop(); ttapeThread = null; } } // End TickerTape