/** * ScrollText.java - Scrolling text widget (simple version) * * This class presents a text message that scrolls through rectangular * area from right to left. * * @author Philip Meese * @version 1.0, 3/16/96 Java Tutor, Second Column * @copyright Copyright 1996 Philip Meese, All Rights Reserved */ import java.awt.*; public class ScrollText extends Canvas implements Runnable{ private Thread _thread; private String _message; private int _speed=50; // medium speed default private Font _font; private FontMetrics _fontMetrics; private int _msgWidth; private Rectangle _rect; private int _msgX, _msgY; private boolean stat; /** * Constructor which sets message, width and height of display area * * @arg message_ The message to be displayed * @arg width_ The initial width of the display area * @arg height_ The initial height of the display area */ public ScrollText(String message_, int width_, int height){ _message=message_; _font=new Font("TimesRoman", Font.BOLD, 56); _rect=new Rectangle(0, 0, width_, height); reshape(0, 0, width_, height); _msgX=width_; // offscreen, right _thread=new Thread(this); } /** * Sets the scrolling speed * * @arg speed_ A value between 1 and 100 (slowest to fastest) */ public void speed(int speed_){ if(speed_ > 0 && speed_ < 100) _speed=speed_; } public void status (boolean t){ stat = t; if (stat) repaint(); System.out.println(stat); } /** * Your standard paint function. Called by your standard update() * * @arg g_ The Graphics object to paint on */ public void paint(Graphics g_){ _rect=bounds(); // the space we live in g_.setFont(_font); _fontMetrics=g_.getFontMetrics(_font); _msgY=_fontMetrics.getAscent(); _msgWidth=_fontMetrics.stringWidth(_message); if (!stat) { paintBackground(g_, Color.blue); paintMessage(g_, Color.yellow); System.out.println("stat false"); } else { paintBackground(g_, Color.white); printMessage(g_, Color.blue); System.out.println("stat true"); } } /** * A painter's helper: Paints the message at it's current position * * @arg g_ The Graphics object to paint on * @arg c_ The color to use for painting */ private void paintMessage(Graphics g_, Color c_){ g_.setColor(c_); g_.drawString(_message, _msgX, _msgY); } private void printMessage(Graphics g_, Color c_){ Font _fonta; _fonta=new Font("Helvetica", Font.PLAIN, 14); g_.setFont(_fonta); g_.setColor(c_); g_.drawString(_message, 10, 40); } /** * Another painter's helper: Fills in background color * * @arg g_ The Graphics object to paint on * @arg c_ The color to use for painting */ private void paintBackground(Graphics g_, Color c_){ g_.setColor(c_); g_.fillRect(_rect.x, _rect.y, _rect.width, _rect.height); } /** * This function makes us "runnable" and is called as a result * of calling _thread.start() */ public void run(){ while(true){ // forever is a long time try{ _thread.sleep(100 - _speed); _msgX=_msgX -= 1; //_msgX = _msgX -1; Fnctional Equivalent ?? //System.out.println(_msgX); if(_msgX <= -1 * _msgWidth) _msgX = _rect.width; repaint(); } catch(InterruptedException e) { break; } } } /** * Starts scrolling action by starting our timing thread */ public void start(){ _thread.start(); } /** * Stops scrolling */ public void suspend(){ _thread.suspend(); } /** * Resumes scrolling after a suspension */ public void resume(){ _thread.resume(); } } // end class ScrollText