home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 2.8 KB | 137 lines |
- import java.applet.Applet;
- import java.awt.*;
-
- class Scroll {
-
- int xstart, ystart;
- int width, height;
- String text;
- int deltaX, deltaY;
- int xpos, ypos;
- Color color;
-
- /**
- * Text scrolls in different directions.
- * @param xpos initial x position
- * @param ypos initial y position
- * @param deltax velocity in x direction
- * @param deltay velocity in y direction
- * @param width bounding point for window panel width
- * @param height bounding point for window panel height
- * @param text text that is scrolled on the window
- * @param color color of the text
- */
- public Scroll (int x, int y, int dx, int dy, int w,int h, String t, Color c) {
-
- xstart = x;
- ystart = y;
- width = w;
- height = h;
- text = t;
- deltaX = dx;
- deltaY = dy;
- color = c;
- xpos = xstart;
- ypos = ystart;
- }
-
- /**
- * Called from update() in response to repaint()
- * @param g - destination graphics object
- */
- void paint (Graphics g) {
-
- g.setColor (color);
- g.drawString (text, xpos, ypos);
- xpos += deltaX;
- ypos += deltaY;
-
- FontMetrics fm = g.getFontMetrics ();
- int textw = fm.stringWidth (text);
- int texth = fm.getHeight ();
- if (deltaX < 0 && xpos < -textw) xpos = xstart;
- if (deltaX > 0 && xpos > width) xpos = xstart;
- if (deltaY < 0 && ypos < 0) ypos = ystart;
- if (deltaY > 0 && ypos > height+texth) ypos = ystart;
- }
- } // Class Scroll
-
-
- /*
- * The applet/application class
- */
- public class ScrollApp extends Applet implements Runnable{
-
- /*
- * Width and height of the bounding panel
- */
- int width, height;
-
- /*
- * Instance of a left-scrolling Scroll object
- */
- Scroll left;
- String input_text;
- Font font = new Font("Helvetica",1,24);
- Thread thread;
-
- /*
- * Called when the applet is loaded
- * Create instances of FIFO, Source, Sink, and Thread.
- */
- public void init () {
-
- input_text=getParameter("text");
- Dimension d = getSize ();
-
- width = d.width;
- height = d.height;
-
- left = new Scroll (400, 50, -5, 0, width, height,
- input_text, Color.red);
- } // init()
-
- /*
- * Start the graphics update thread.
- */
- public void start() {
-
- thread = new Thread(this);
- thread.start();
- } // start()
-
- /*
- * The graphics update thread
- * Call repaint every 100 ms.
- */
- public void run() {
-
- while (true) {
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) { }
- repaint();
- }
- } // run()
-
- /*
- * Stop the graphics update thread.
- */
- public void stop() {
-
- if (thread != null)
- thread = null;
- } // stop()
-
- /**
- * Called from update() in response to repaint()
- * @param g - destination graphics object
- */
- public void paint (Graphics g) {
-
- g.setFont(font);
- left.paint (g);
- } // paint()
- } // class ScrollApp
-
-