home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 3.0 KB | 146 lines |
- // ScrollingPanel.java
- // 24.03.96
- //
- // text that scrolls
-
- package cybcerone.utils;
-
- import java.awt.Graphics;
- import java.awt.Image;
-
- /**
- * Text scrolls horizontallz.
- */
- public class ScrollingPanel extends TextPanel implements Runnable {
- protected int step = 2;
- protected int delay = 33;
- protected int space = scale (100);
-
- protected Thread scroller;
- protected int x;
- protected int textWidth;
- protected boolean scrolling;
-
- protected int offScreen; /* -1 * textWidth */
- protected int fullWidth; /* textWidth + space */
-
- // for double buffering
- private Image offscreenImg;
- private Graphics offscreenG;
-
- public ScrollingPanel (String id, String statusText, Appletlike app) {
- super (id, statusText, app);
- }
-
- public void setSpeed (int step, int delay) {
- this.step = scale (step);
- this.delay = scale (delay);
- }
-
- /** the space between the end and the beginning of scrolling text */
- public void setSpace (int space) {
- this.space = scale (space);
- setFullWidth ();
- }
-
- public void setText (String text) {
- stop ();
- super.setText (text);
-
- if (text != null) {
- textWidth = metrics.stringWidth (text);
- if (textWidth < size().width) {
- x = 0;
- scrolling = false;
- } else {
- x = size().width / 2;
- setOffScreen ();
- setFullWidth ();
- scrolling = true;
- start ();
- }
- }
- }
-
- protected void setOffScreen () {
- offScreen = -1 * textWidth;
- }
-
- protected void setFullWidth () {
- fullWidth = textWidth + space;
- }
-
- public void setText (ScrollingPanel 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 start () {
- if (scroller == null && scrolling && isShowing ()) {
- scroller = new Thread (this);
- scroller.start ();
- }
- }
-
- public void stop () {
- if (scroller != null)
- scroller.stop ();
- scroller = null;
- }
-
- public void run () {
- long now;
- long updateTime = 0;
-
- while (scroller != null) {
- if ((updateTime += delay) < (now = System.currentTimeMillis ()))
- updateTime = now;
-
- step ();
- repaint ();
-
- try {
- scroller.sleep (updateTime - System.currentTimeMillis ());
- } catch (InterruptedException e) {
- }
- }
- }
-
- /** take one step */
- protected void step () {
- x -= step;
- if (x < offScreen)
- x += fullWidth;
- }
-
- protected void paintText (Graphics g) {
- if (text != null) {
- g.drawString (text, x, y);
- if (scrolling && x < size().width - fullWidth) {
- g.drawString (text, x + textWidth + space, y);
- }
- }
- }
-
- public void update (Graphics g) {
- // for double buffering;
- if (offscreenG == null) {
- offscreenImg = createImage (size().width, size().height);
- offscreenG = offscreenImg.getGraphics ();
- }
-
- paint (offscreenG);
- g.drawImage (offscreenImg, 0, 0, this);
- }
- }
-