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

  1. // ScrollingTextPanel.java
  2. // 17.03.96
  3. //
  4. // text that scrolls
  5.  
  6. package cybcerone.utils;
  7.  
  8. import java.awt.Color;
  9. import java.awt.Font;
  10. import java.awt.FontMetrics;
  11. import java.awt.Graphics;
  12. import java.awt.Image;
  13. import java.awt.Point;
  14. import java.awt.Dimension;
  15. import java.awt.Event;
  16.  
  17. public class ScrollingTextPanel extends IdPanel implements Runnable {
  18.   private String text;
  19.   private int step;
  20.   private int delay;
  21.   private Font textFont;
  22.   private Color textColor;
  23.   private Color highlightColor;
  24.  
  25.   private Thread scroller;
  26.   private int x;
  27.   private int y;
  28.   private int textWidth;
  29.  
  30.   /* are we scrolling this text? */
  31.   private boolean scrolling;
  32.  
  33.   // for double buffering
  34.   private Image offscreenImg;
  35.   private Graphics offscreenG;
  36.  
  37.   public ScrollingTextPanel (String id, String statusText, Appletlike app) {
  38.     this (id, statusText, "", app);
  39.   }
  40.  
  41.   public ScrollingTextPanel (String id, String statusText,
  42.                  String text, Appletlike app) {
  43.     this (id, statusText, text, 3, 50, 
  44.       Color.white, new Font ("Dialog", Font.PLAIN, 18), Color.black, app);
  45.   }
  46.   
  47.   /**
  48.    * text to scroll, number of pixels to step, time between steps 
  49.    */
  50.   public ScrollingTextPanel (String id, String statusText,
  51.                  String text, int step, int delay,
  52.                  Color textColor, Font textFont, Color bgColor,
  53.                  Appletlike app) {
  54.     super (id, statusText, app);
  55.     this.text = text;
  56.     this.step = step;
  57.     this.delay = delay;
  58.     this.textFont = textFont;
  59.     this.textColor = textColor;
  60.  
  61.     setBackground (bgColor);
  62.     setForeground (textColor);
  63.   }
  64.  
  65.   public void setText (String text) {
  66.     this.text = text;
  67.  
  68.     FontMetrics metrics = getFontMetrics (textFont);
  69.     int textHeight = metrics.getAscent () + metrics.getDescent ();
  70.  
  71.     if (text != null)
  72.       textWidth = metrics.stringWidth (text);
  73.     else
  74.       textWidth = 0;
  75.     
  76.     y = (size().height + textHeight) / 2;
  77.     
  78.     if (textWidth < size().width) {
  79.       x = 0;
  80.       scrolling = false;
  81.       stop ();
  82.       repaint ();
  83.     } else {
  84.       x = size().width / 2;
  85.       scrolling = true;
  86.       start ();
  87.     }
  88.   }
  89.   
  90.   public void setText (ScrollingTextPanel otherPanel) {
  91.     this.text = otherPanel.text;
  92.     this.x = otherPanel.x;
  93.     this.scrolling = otherPanel.scrolling;
  94.     this.textWidth = otherPanel.textWidth;
  95.     setNext (otherPanel.getNext ());
  96.  
  97.     if (scrolling) 
  98.       start ();
  99.     else {
  100.       stop ();
  101.       repaint ();
  102.     }
  103.   }
  104.  
  105.   public void setTextColor (Color textColor) {
  106.     this.textColor = textColor;
  107.     setForeground (textColor);
  108.   }
  109.  
  110.   public void setHighlight (Color highlightColor) {
  111.     this.highlightColor = highlightColor;
  112.   }
  113.   
  114.   public void setBackground (String bgFile, int priority) {
  115.     super.setBackground (bgFile, priority);
  116.   }
  117.   
  118.   public void start () {
  119.     if (scroller == null && scrolling) {
  120.       scroller = new Thread (this);
  121.       scroller.start ();
  122.     }
  123.   }
  124.  
  125.   public void stop () {
  126.     if (scroller != null) {
  127.       scroller.stop ();
  128.       scroller = null;
  129.     }
  130.   }
  131.  
  132.   public void run () {
  133.     while (scroller != null) {
  134.       repaint ();
  135.       try {
  136.     scroller.sleep (delay);
  137.       } catch (InterruptedException e) {
  138.       }
  139.       x -= step;
  140.       if (x < (-1 * textWidth)) x += textWidth + 100;
  141.     }
  142.   }
  143.  
  144.   public boolean mouseEnter (Event evt, int x, int y) {
  145.     if (highlightColor != null) {
  146.       setForeground (highlightColor);
  147.       repaint ();
  148.     }
  149.     return super.mouseEnter (evt, x, y);
  150.   }
  151.  
  152.   public boolean mouseExit (Event evt, int x, int y) {
  153.     if (textColor != null) {
  154.       setForeground (textColor);
  155.       repaint ();
  156.     }
  157.     return super.mouseEnter (evt, x, y);
  158.   }
  159.  
  160.   public void paint (Graphics g) {
  161.     super.paint (g);
  162.     g.setFont (textFont);
  163.     g.setColor (getForeground ());
  164.     if (text != null) {
  165.       g.drawString (text, x, y);
  166.       if (scrolling && x < size().width - textWidth - 100) {
  167.     g.drawString (text, x + textWidth + 100, y);
  168.       }
  169.     }
  170.   }
  171.  
  172.   public void update (Graphics g) {
  173.     // for double buffering;
  174.     if (offscreenG == null) {
  175.       offscreenImg = createImage (size().width, size().height);
  176.       offscreenG = offscreenImg.getGraphics ();
  177.     }
  178.  
  179.     offscreenG.setColor (getBackground ());
  180.     offscreenG.fillRect (0, 0, size().width, size().height);
  181.     paint (offscreenG);
  182.     g.drawImage (offscreenImg, 0, 0, this);
  183.   }
  184. }
  185.