home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 4.2 KB | 185 lines |
- // ScrollingTextPanel.java
- // 17.03.96
- //
- // text that scrolls
-
- package cybcerone.utils;
-
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Point;
- import java.awt.Dimension;
- import java.awt.Event;
-
- public class ScrollingTextPanel extends IdPanel implements Runnable {
- private String text;
- private int step;
- private int delay;
- private Font textFont;
- private Color textColor;
- private Color highlightColor;
-
- private Thread scroller;
- private int x;
- private int y;
- private int textWidth;
-
- /* are we scrolling this text? */
- private boolean scrolling;
-
- // for double buffering
- private Image offscreenImg;
- private Graphics offscreenG;
-
- public ScrollingTextPanel (String id, String statusText, Appletlike app) {
- this (id, statusText, "", app);
- }
-
- public ScrollingTextPanel (String id, String statusText,
- String text, Appletlike app) {
- this (id, statusText, text, 3, 50,
- Color.white, new Font ("Dialog", Font.PLAIN, 18), Color.black, app);
- }
-
- /**
- * text to scroll, number of pixels to step, time between steps
- */
- public ScrollingTextPanel (String id, String statusText,
- String text, int step, int delay,
- Color textColor, Font textFont, Color bgColor,
- Appletlike app) {
- super (id, statusText, app);
- this.text = text;
- this.step = step;
- this.delay = delay;
- this.textFont = textFont;
- this.textColor = textColor;
-
- setBackground (bgColor);
- setForeground (textColor);
- }
-
- public void setText (String text) {
- this.text = text;
-
- FontMetrics metrics = getFontMetrics (textFont);
- int textHeight = metrics.getAscent () + metrics.getDescent ();
-
- if (text != null)
- textWidth = metrics.stringWidth (text);
- else
- textWidth = 0;
-
- y = (size().height + textHeight) / 2;
-
- if (textWidth < size().width) {
- x = 0;
- scrolling = false;
- stop ();
- repaint ();
- } else {
- x = size().width / 2;
- scrolling = true;
- start ();
- }
- }
-
- public void setText (ScrollingTextPanel otherPanel) {
- this.text = otherPanel.text;
- this.x = otherPanel.x;
- this.scrolling = otherPanel.scrolling;
- this.textWidth = otherPanel.textWidth;
- setNext (otherPanel.getNext ());
-
- if (scrolling)
- start ();
- else {
- stop ();
- repaint ();
- }
- }
-
- public void setTextColor (Color textColor) {
- this.textColor = textColor;
- setForeground (textColor);
- }
-
- public void setHighlight (Color highlightColor) {
- this.highlightColor = highlightColor;
- }
-
- public void setBackground (String bgFile, int priority) {
- super.setBackground (bgFile, priority);
- }
-
- public void start () {
- if (scroller == null && scrolling) {
- scroller = new Thread (this);
- scroller.start ();
- }
- }
-
- public void stop () {
- if (scroller != null) {
- scroller.stop ();
- scroller = null;
- }
- }
-
- public void run () {
- while (scroller != null) {
- repaint ();
- try {
- scroller.sleep (delay);
- } catch (InterruptedException e) {
- }
- x -= step;
- if (x < (-1 * textWidth)) x += textWidth + 100;
- }
- }
-
- public boolean mouseEnter (Event evt, int x, int y) {
- if (highlightColor != null) {
- setForeground (highlightColor);
- repaint ();
- }
- return super.mouseEnter (evt, x, y);
- }
-
- public boolean mouseExit (Event evt, int x, int y) {
- if (textColor != null) {
- setForeground (textColor);
- repaint ();
- }
- return super.mouseEnter (evt, x, y);
- }
-
- public void paint (Graphics g) {
- super.paint (g);
- g.setFont (textFont);
- g.setColor (getForeground ());
- if (text != null) {
- g.drawString (text, x, y);
- if (scrolling && x < size().width - textWidth - 100) {
- g.drawString (text, x + textWidth + 100, y);
- }
- }
- }
-
- public void update (Graphics g) {
- // for double buffering;
- if (offscreenG == null) {
- offscreenImg = createImage (size().width, size().height);
- offscreenG = offscreenImg.getGraphics ();
- }
-
- offscreenG.setColor (getBackground ());
- offscreenG.fillRect (0, 0, size().width, size().height);
- paint (offscreenG);
- g.drawImage (offscreenImg, 0, 0, this);
- }
- }
-