home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 3.1 KB | 150 lines |
- import java.applet.Applet;
- import java.awt.*;
-
- /**
- * a class that handles scrolling text
- */
- class Scroll {
-
- /*
- * x and y coordinates of starting point
- */
- int xstart, ystart;
-
- /*
- * width and height of bounding panel
- */
- int width, height;
-
- /*
- * text to be scrolled
- */
- String text;
-
- /*
- * x and y velocities, respectively
- */
- int deltaX, deltaY;
-
- /*
- * current x and y position of the text
- */
- int xpos, ypos;
-
- /*
- * the color of the text
- */
- Color color;
-
- /**
- * class constructor just saves arguments
- * @param x, y - starting coordinates
- * @param dx, dy - x and y velocities
- * @param w, h - width and height of bounding panel
- * @param t - the text string
- * @param c - 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;
- }
-
- /*
- * draw the text at the current position
- * advance the position and reinitialize outside bounding panel
- * @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;
- }
- }
-
- /**
- * the applet/application proper
- */
- public class ScrollApp extends Applet {
-
- /*
- * width and height of the bounding panel
- */
- int width, height;
-
- /*
- * instances of Scroll for demonstration
- */
- Scroll left, right, up, down, diag;
-
- /*
- * called when the applet is loaded
- * create new instances of Scroll
- */
- public void init () {
-
-
- width = 410;
- height = 230;
-
- left = new Scroll (400, 50, -5, 0, width, height,
- "Moving left", Color.red);
- right = new Scroll (0, 150, 5, 0, width, height,
- "Moving right", Color.green);
- up = new Scroll (100, 200, 0, -5, width, height,
- "Moving up", Color.blue);
- down = new Scroll (200, 0, 0, 5, width, height,
- "Moving down", Color.cyan);
- diag = new Scroll (0, 0, 7, 3, width, height,
- "Moving diagonally", Color.magenta);
- }
-
- /*
- * invoke the paint method of each scrolling text instance
- * force a repaint 50ms later
- */
- public void paint (Graphics g) {
-
- left.paint (g);
- right.paint (g);
- up.paint (g);
- down.paint (g);
- diag.paint (g);
- repaint (50);
- }
-
- /*
- * Application entry point
- * @param args - command-line arguments
- */
- public static void main (String args[]) {
-
- Frame f = new Frame ("Scrolling text");
- ScrollApp scrollApp = new ScrollApp ();
-
- f.setSize (410, 230);
- f.add ("Center", scrollApp);
- f.show ();
- scrollApp.init ();
- }
- }
-
-