home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / JAVA / NOTES / SOURCE / scrlmsg.jav < prev    next >
Text File  |  1996-12-20  |  6KB  |  151 lines

  1.  
  2. /*
  3.     Applet scrolls a message, while a red oval grows from size zero
  4.     to the full size of the applet.  Two red bars across the applet
  5.     lie above and below the path of the message.  This is an example
  6.     of animation using an off-screen canvas.
  7.     
  8.     David Eck, September 2, 1996
  9. */
  10.  
  11.  
  12. import java.awt.*;
  13. import java.applet.*;
  14.  
  15. public class ScrollingMessage extends Applet
  16.                               implements Runnable {
  17.    
  18.    String message = "Hello World!";   // can be modified by an applet param with name "message"
  19.    
  20.    int message_x, message_y;   // position where message is drawn
  21.    int min_x, max_x;           // largest and smallest values for message_x
  22.    int bar1_y, bar2_y;         // y coordinates for red lines across the applet
  23.    
  24.    int pulse_frames = 30;   // number of frames it takes for red oval to grow to full size
  25.    int scroll_frames = 100; // number of framed it takes for message to scroll across the screen
  26.          
  27.    Thread runner = null;   // Thread for running the animation
  28.    int sleepTime = 100;    // Pause between frames, in milliseconds
  29.    
  30.    Image OSC = null;       // the off-screen image
  31.    Graphics OSC_graphics;  // graphics context for OSC 
  32.    int width = -1;         // width of OSC  (value of -1 indicates it doesn't exist)
  33.    int height = -1;        // height of OSC
  34.    
  35.  
  36.    public void init() {
  37.          // just check for the parameter
  38.       String param = getParameter("message");
  39.       if (param != null)
  40.          message = param;
  41.    }
  42.        
  43.    synchronized public void paint(Graphics g) {
  44.       if (OSC != null)  // if off-screen image exists, copy it to screen
  45.           g.drawImage(OSC,0,0,this);
  46.       else {  // otherwise, fill the applet with the backgroudn color
  47.           g.setColor(getBackground());
  48.           g.fillRect(0,0,size().width,size().height);
  49.       }
  50.    }
  51.        
  52.    public void update(Graphics g) {
  53.          // redefine update() so that it doesn't erase the applet;
  54.          // this will avoid flickering when the applet is redrawn
  55.       paint(g);
  56.    }
  57.          
  58.    public void start() {
  59.       if (runner == null) {  // create and start the thread
  60.          runner = new Thread(this);
  61.          runner.start();
  62.       }
  63.       else if (runner.isAlive()) {  // resume the thread,
  64.          runner.resume();           //    unless it is already dead
  65.       }
  66.    }
  67.          
  68.    public void stop() {
  69.       if (runner.isAlive())  // suspend the thread, 
  70.          runner.suspend();   //     if it is still alive
  71.    }
  72.          
  73.    public void destroy() {
  74.       if (runner.isAlive())  // if the thread is still running,
  75.           runner.stop();     //     then stop it
  76.       runner = null;
  77.    }
  78.    
  79.    synchronized void doResize(int w, int h) {
  80.          // Resize the off-screen canvas to width w, height h
  81.          // (presumably the new size of the applet).  This is
  82.          // called only by drawNextFrame().  The initial values
  83.          // of instance variables width and height force this
  84.          // to be called before any frames are drawn.
  85.        OSC = null;
  86.        width = w;
  87.        height = h;
  88.        OSC = createImage(width,height);  // create off-screen canvas
  89.        OSC_graphics = OSC.getGraphics();
  90.        Font font = getFont();  // font for drawing message, about 1/8 height of applet
  91.        font = new Font("TimesRoman", Font.PLAIN, height/8);
  92.        OSC_graphics.setFont(font);
  93.        FontMetrics fm = getFontMetrics(font);
  94.            // compute drawing parameters that depend on applet and font size
  95.        message_y = (height - fm.getHeight()) / 2 + fm.getAscent() + fm.getLeading() / 2;
  96.        bar1_y = message_y - fm.getAscent() - fm.getLeading();
  97.        bar2_y = message_y + fm.getLeading() + fm.getDescent();
  98.        min_x = -fm.stringWidth(message);
  99.        max_x = width;
  100.    }
  101.          
  102.    
  103.    synchronized void drawNextFrame(int scroll_frame, int pulse_frame) {
  104.            
  105.            // Create one frame in the animation.  scroll_frame gives
  106.            // relative position of scrolling message; pulse_frame
  107.            // gives relative size of growing oval.
  108.            
  109.        int w = size().width;   // check if size of applet has changed
  110.        int h = size().height;
  111.        if (w != width || h != height)
  112.           doResize(w,h);
  113.        
  114.            // compute drawing parameters that depend on which frame this is
  115.        message_x = max_x - ((scroll_frame * (max_x-min_x)) / scroll_frames);
  116.        int a = (width/2) - (pulse_frame*width)/(2*pulse_frames);   // left edge for oval
  117.        int b = (height/2) - (pulse_frame*height)/(2*pulse_frames); // top edge for oval
  118.        
  119.        OSC_graphics.setColor(Color.white);                 // fill the background
  120.        OSC_graphics.fillRect(0,0,width,height);
  121.        OSC_graphics.setColor(Color.red);                   // draw the oval
  122.        OSC_graphics.fillOval(a,b,width-2*a,height-2*b);
  123.        OSC_graphics.drawLine(0,bar1_y,width,bar1_y);       // draw the bars (2 pixels high)
  124.        OSC_graphics.drawLine(0,bar1_y+1,width,bar1_y+1);
  125.        OSC_graphics.drawLine(0,bar2_y,width,bar2_y);
  126.        OSC_graphics.drawLine(0,bar2_y-1,width,bar2_y-1);
  127.        OSC_graphics.setColor(Color.black);                 // draw the message
  128.        OSC_graphics.drawString(message,message_x,message_y);
  129.    
  130.    }
  131.        
  132.    public void run() {
  133.          // subroutine to be executed by the thread
  134.       int scroll_frame = 0;  // frame number for scrolling
  135.       int pulse_frame = 0;   // frame number for growing oval
  136.       while (true) {
  137.          drawNextFrame(scroll_frame,pulse_frame);
  138.          repaint();
  139.          scroll_frame++;
  140.          if (scroll_frame > scroll_frames)
  141.             scroll_frame = 0;
  142.          pulse_frame++;
  143.          if (pulse_frame > pulse_frames)
  144.            pulse_frame = 0;
  145.          try { Thread.sleep(sleepTime); }
  146.          catch (InterruptedException e) { }
  147.       }
  148.    }
  149.          
  150. }  // end of class ScrollingMessage
  151.