home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch03 / ScrollApp.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  3.1 KB  |  150 lines

  1. import java.applet.Applet;
  2. import java.awt.*;
  3.  
  4. /**
  5.  * a class that handles scrolling text
  6.  */
  7. class Scroll {
  8.  
  9. /*
  10.  * x and y coordinates of starting point
  11.  */
  12. int xstart, ystart;
  13.  
  14. /*
  15.  * width and height of bounding panel
  16.  */
  17. int width, height;
  18.  
  19. /*
  20.  * text to be scrolled
  21.  */
  22. String text; 
  23.  
  24. /*
  25.  * x and y velocities, respectively
  26.  */
  27. int deltaX, deltaY;
  28.  
  29. /*
  30.  * current x and y position of the text
  31.  */
  32. int xpos, ypos;
  33.  
  34. /*
  35.  * the color of the text
  36.  */
  37. Color color;
  38.  
  39. /**
  40.  * class constructor just saves arguments
  41.  * @param x, y - starting coordinates
  42.  * @param dx, dy - x and y velocities
  43.  * @param w, h - width and height of bounding panel
  44.  * @param t - the text string
  45.  * @param c - color of the text
  46.  */
  47. public Scroll (int x, int y, int dx, int dy, int w, int h, String t, Color c) {
  48.  
  49.       xstart = x;
  50.       ystart = y;
  51.       width = w;
  52.       height = h;
  53.       text = t;
  54.       deltaX = dx;
  55.       deltaY = dy;
  56.       color = c;
  57.       xpos = xstart;
  58.       ypos = ystart;
  59. }
  60.  
  61. /*
  62.  * draw the text at the current position
  63.  * advance the position and reinitialize outside bounding panel
  64.  * @param g - destination graphics object
  65.  */
  66. void paint (Graphics g) {
  67.  
  68.       g.setColor (color);
  69.       g.drawString (text, xpos, ypos);
  70.       xpos += deltaX;
  71.       ypos += deltaY;
  72.  
  73.       FontMetrics fm = g.getFontMetrics ();
  74.       int textw = fm.stringWidth (text);
  75.       int texth = fm.getHeight ();
  76.       if (deltaX < 0 && xpos < -textw) xpos = xstart;
  77.       if (deltaX > 0 && xpos > width) xpos = xstart;
  78.       if (deltaY < 0 && ypos < 0) ypos = ystart;
  79.       if (deltaY > 0 && ypos > height+texth) ypos = ystart;
  80. }
  81. }
  82.  
  83. /**
  84.  * the applet/application proper
  85.  */
  86. public class ScrollApp extends Applet {
  87.  
  88. /*
  89.  * width and height of the bounding panel
  90.  */
  91. int width, height;
  92.  
  93. /*
  94.  * instances of Scroll for demonstration
  95.  */
  96. Scroll left, right, up, down, diag;
  97.  
  98. /*
  99.  * called when the applet is loaded
  100.  * create new instances of Scroll
  101.  */      
  102. public void init () {
  103.  
  104.  
  105.       width = 410;
  106.       height = 230;
  107.  
  108.       left = new Scroll (400, 50, -5, 0, width, height,
  109.             "Moving left", Color.red);
  110.       right = new Scroll (0, 150, 5, 0, width, height,
  111.             "Moving right", Color.green);
  112.       up = new Scroll (100, 200, 0, -5, width, height,
  113.             "Moving up", Color.blue);
  114.       down = new Scroll (200, 0, 0, 5, width, height,
  115.             "Moving down", Color.cyan);
  116.       diag = new Scroll (0, 0, 7, 3, width, height,
  117.             "Moving diagonally", Color.magenta); 
  118. }
  119.  
  120. /*
  121.  * invoke the paint method of each scrolling text instance
  122.  * force a repaint 50ms later
  123.  */
  124. public void paint (Graphics g) {
  125.  
  126.       left.paint (g);
  127.       right.paint (g);
  128.       up.paint (g);
  129.       down.paint (g);
  130.       diag.paint (g);
  131.       repaint (50);
  132. }
  133.  
  134. /*
  135.  * Application entry point
  136.  * @param args - command-line arguments
  137.  */
  138. public static void main (String args[]) {
  139.  
  140.       Frame f = new Frame ("Scrolling text");
  141.       ScrollApp scrollApp = new ScrollApp ();
  142.       
  143.       f.setSize (410, 230);
  144.       f.add ("Center", scrollApp);
  145.       f.show ();
  146.       scrollApp.init ();
  147. }
  148. }
  149.  
  150.