home *** CD-ROM | disk | FTP | other *** search
/ Discovering Windows 98 / WinExpert9.iso / Tips / JavaSource.zip / Ticker.java < prev    next >
Text File  |  1997-11-03  |  2KB  |  65 lines

  1. import java.awt.*;
  2.  
  3. public class Ticker extends java.applet.Applet implements Runnable {
  4.     String message;
  5.     Font f;
  6.     FontMetrics fm;
  7.     int position;
  8.     Thread runner;
  9.         Graphics offscreenG;
  10.         Image offscreenImg;
  11.  
  12.  
  13.     public void init () {
  14.         message = getParameter("tickertext");
  15.         f = new Font("Arial", Font.BOLD, 16);
  16.         fm = getFontMetrics(f);
  17.         position = 0;
  18.                 offscreenImg = createImage (size().width, size().height);
  19.                 offscreenG = offscreenImg.getGraphics();
  20.         }
  21.  
  22.     public void start () {
  23.                 if (runner == null) {
  24.                         runner = new Thread(this);
  25.                         runner.start();
  26.                         }
  27.                 }
  28.  
  29.         public void stop () {
  30.                 if (runner != null) {
  31.                         runner.stop();
  32.                         runner = null;
  33.                         }
  34.                 }
  35.  
  36.         public void run () {
  37.                 while (true) {
  38.             position++;
  39.             repaint();
  40.             if (position > size().width + fm.stringWidth(message)) {
  41.                 position = 0;
  42.                 }
  43.             try {
  44.                 runner.sleep(25);
  45.                 }
  46.             catch (InterruptedException e) {
  47.                 }
  48.                         }
  49.         }
  50.  
  51.     public void update(Graphics g) {
  52.                 g.clipRect(0, 0, size().width, size().height);
  53.                 paint(g);
  54.         }
  55.                         
  56.     public void paint(Graphics g) {
  57.         offscreenG.setFont(f);
  58.         offscreenG.setColor(Color.black);
  59.         offscreenG.fillRect(0, 0, size().width, size().height);
  60.         offscreenG.setColor(Color.white);
  61.         offscreenG.drawString(message, size().width - position, 20);
  62.  
  63.                 g.drawImage(offscreenImg, 0, 0, this);
  64.         }
  65.     }