home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuy2wen / cybcerone / utils / scrollingpanel.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.0 KB  |  146 lines

  1. // ScrollingPanel.java
  2. // 24.03.96
  3. //
  4. // text that scrolls
  5.  
  6. package cybcerone.utils;
  7.  
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10.  
  11. /**
  12.  * Text scrolls horizontallz.
  13.  */
  14. public class ScrollingPanel extends TextPanel implements Runnable {
  15.   protected int step = 2;
  16.   protected int delay = 33;
  17.   protected int space = scale (100);
  18.   
  19.   protected Thread scroller;
  20.   protected int x;
  21.   protected int textWidth;
  22.   protected boolean scrolling;
  23.  
  24.   protected int offScreen;    /* -1 * textWidth    */
  25.   protected int fullWidth;    /* textWidth + space */
  26.   
  27.   // for double buffering
  28.   private Image offscreenImg;
  29.   private Graphics offscreenG;
  30.   
  31.   public ScrollingPanel (String id, String statusText, Appletlike app) {
  32.     super (id, statusText, app);
  33.   }
  34.   
  35.   public void setSpeed (int step, int delay) {
  36.     this.step = scale (step);
  37.     this.delay = scale (delay);
  38.   }
  39.  
  40.   /** the space between the end and the beginning of scrolling text */
  41.   public void setSpace (int space) {
  42.     this.space = scale (space);
  43.     setFullWidth ();
  44.   }
  45.  
  46.   public void setText (String text) {
  47.     stop ();
  48.     super.setText (text);
  49.  
  50.     if (text != null) {
  51.       textWidth = metrics.stringWidth (text);
  52.       if (textWidth < size().width) {
  53.     x = 0;
  54.     scrolling = false;
  55.       } else {
  56.     x = size().width / 2;
  57.     setOffScreen ();
  58.     setFullWidth ();
  59.     scrolling = true;
  60.     start ();
  61.       }
  62.     }
  63.   }
  64.  
  65.   protected void setOffScreen () {
  66.     offScreen = -1 * textWidth;
  67.   }
  68.  
  69.   protected void setFullWidth () {
  70.     fullWidth = textWidth + space;
  71.   }
  72.   
  73.   public void setText (ScrollingPanel otherPanel) {
  74.     this.text = otherPanel.text;
  75.     this.x = otherPanel.x;
  76.     this.scrolling = otherPanel.scrolling;
  77.     this.textWidth = otherPanel.textWidth;
  78.     setNext (otherPanel.getNext ());
  79.  
  80.     if (scrolling) 
  81.       start ();
  82.     else {
  83.       stop ();
  84.       repaint ();
  85.     }
  86.   }
  87.  
  88.   public void start () {
  89.     if (scroller == null && scrolling && isShowing ()) {
  90.       scroller = new Thread (this);
  91.       scroller.start ();
  92.     }
  93.   }
  94.  
  95.   public void stop () {
  96.     if (scroller != null)
  97.       scroller.stop ();
  98.     scroller = null;
  99.   }
  100.  
  101.   public void run () {
  102.     long now;
  103.     long updateTime = 0;
  104.  
  105.     while (scroller != null) {
  106.       if ((updateTime += delay) < (now = System.currentTimeMillis ()))
  107.     updateTime = now;
  108.  
  109.       step ();
  110.       repaint ();
  111.  
  112.       try {
  113.     scroller.sleep (updateTime - System.currentTimeMillis ());
  114.       } catch (InterruptedException e) {
  115.       }
  116.     }
  117.   }
  118.  
  119.   /** take one step */
  120.   protected void step () {
  121.     x -= step;
  122.     if (x < offScreen)
  123.       x += fullWidth;
  124.   }
  125.  
  126.   protected void paintText (Graphics g) {
  127.     if (text != null) {
  128.       g.drawString (text, x, y);
  129.       if (scrolling && x < size().width - fullWidth) {
  130.     g.drawString (text, x + textWidth + space, y);
  131.       }
  132.     }
  133.   }
  134.  
  135.   public void update (Graphics g) {
  136.     // for double buffering;
  137.     if (offscreenG == null) {
  138.       offscreenImg = createImage (size().width, size().height);
  139.       offscreenG = offscreenImg.getGraphics ();
  140.     }
  141.  
  142.     paint (offscreenG);
  143.     g.drawImage (offscreenImg, 0, 0, this);
  144.   }
  145. }
  146.