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

  1. import java.applet.Applet;
  2. import java.awt.*;
  3.  
  4. class Scroll {
  5.  
  6. int xstart, ystart;
  7. int width, height;
  8. String text;
  9. int deltaX, deltaY;
  10. int xpos, ypos;
  11. Color color;
  12.  
  13. /**
  14.  * Text scrolls in different directions.
  15.  * @param xpos      initial x position
  16.  * @param ypos      initial y position
  17.  * @param deltax    velocity in x direction
  18.  * @param deltay    velocity in y direction
  19.  * @param width     bounding point for window panel width
  20.  * @param height    bounding point for window panel height
  21.  * @param text      text that is scrolled on the window
  22.  * @param color     color of the text
  23.  */
  24. public Scroll (int x, int y, int dx, int dy, int w,int h, String t, Color c) {
  25.  
  26.     xstart = x;
  27.     ystart = y;
  28.     width = w;
  29.     height = h;
  30.     text = t;
  31.     deltaX = dx;
  32.     deltaY = dy;
  33.     color = c;
  34.     xpos = xstart;
  35.     ypos = ystart;
  36. }
  37.  
  38. /**
  39.  * Called from update() in response to repaint()
  40.  * @param g - destination graphics object
  41.  */
  42. void paint (Graphics g) {
  43.  
  44.     g.setColor (color);
  45.     g.drawString (text, xpos, ypos);
  46.     xpos += deltaX;
  47.     ypos += deltaY;
  48.  
  49.     FontMetrics fm = g.getFontMetrics ();
  50.     int textw = fm.stringWidth (text);
  51.     int texth = fm.getHeight ();
  52.     if (deltaX < 0 && xpos < -textw) xpos = xstart;
  53.     if (deltaX > 0 && xpos > width) xpos = xstart;
  54.     if (deltaY < 0 && ypos < 0) ypos = ystart;
  55.     if (deltaY > 0 && ypos > height+texth) ypos = ystart;
  56. }
  57. } // Class Scroll
  58.  
  59.  
  60. /*
  61.  * The applet/application class
  62.  */
  63. public class ScrollApp extends Applet implements Runnable{
  64.  
  65. /*
  66.  * Width and height of the bounding panel
  67.  */
  68. int width, height;
  69.  
  70. /*
  71.  * Instance of a left-scrolling Scroll object
  72.  */
  73. Scroll left;
  74. String input_text;
  75. Font font = new Font("Helvetica",1,24);
  76. Thread thread;
  77.  
  78. /*
  79.  * Called when the applet is loaded
  80.  * Create instances of FIFO, Source, Sink, and Thread.
  81.  */
  82. public void init () {
  83.  
  84.     input_text=getParameter("text");
  85.     Dimension d = getSize ();
  86.  
  87.     width = d.width;
  88.     height = d.height;
  89.  
  90.     left = new Scroll (400, 50, -5, 0, width, height,
  91.         input_text, Color.red);
  92. } // init()
  93.  
  94. /*
  95.  * Start the graphics update thread.
  96.  */
  97. public void start() {
  98.  
  99.     thread = new Thread(this);
  100.     thread.start();
  101. } // start()
  102.  
  103. /*
  104.  * The graphics update thread
  105.  * Call repaint every 100 ms. 
  106.  */
  107. public void run() {
  108.  
  109.     while (true) {
  110.         try {
  111.             Thread.sleep(100);
  112.         } catch (InterruptedException e) { }
  113.         repaint();
  114.     }
  115. } // run()
  116.  
  117. /*
  118.  * Stop the graphics update thread.
  119.  */
  120. public void stop() {
  121.  
  122.     if (thread != null)
  123.     thread = null;
  124. } // stop()
  125.  
  126. /**
  127.  * Called from update() in response to repaint()
  128.  * @param g - destination graphics object
  129.  */
  130. public void paint (Graphics g) {
  131.  
  132.     g.setFont(font);
  133.     left.paint (g);
  134. } // paint()
  135. } // class ScrollApp
  136.  
  137.