home *** CD-ROM | disk | FTP | other *** search
/ Freesoft 1997 May / Freesoft_1997-05_cd.bin / recenz / PROGRAM / JAVADRAW / iavadraw301_inst.exe / data.z / Update.java < prev    next >
Text File  |  1997-05-20  |  5KB  |  184 lines

  1. /*
  2.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.applet.Applet;
  19.  
  20. /* 
  21.  * This applet animates graphics that it generates.  This example
  22.  * eliminates flashing by overriding the update() method.  For the
  23.  * graphics this example generates, overriding update() isn't quite
  24.  * good enough -- on some systems, you can still see a crawling effect.
  25.  */
  26.  
  27. public class Update extends Applet implements Runnable {
  28.     int frameNumber = -1;
  29.     int delay;
  30.     Thread animatorThread;
  31.     boolean frozen = false;
  32.  
  33.     int squareSize = 20;
  34.     boolean fillColumnTop = true;
  35.  
  36.     public void init() {
  37.         String str;
  38.         int fps = 10;
  39.  
  40.         //How many milliseconds between frames?
  41.         str = getParameter("fps");
  42.         try {
  43.             if (str != null) {
  44.                 fps = Integer.parseInt(str);
  45.             }
  46.         } catch (Exception e) {}
  47.         delay = (fps > 0) ? (1000 / fps) : 100;
  48.  
  49.         //How many pixels wide is each square?
  50.         str = getParameter("squareWidth");
  51.         try {
  52.             if (str != null) {
  53.                 squareSize = Integer.parseInt(str);
  54.             }
  55.         } catch (Exception e) {}
  56.     }
  57.  
  58.     public void start() {
  59.         if (frozen) {
  60.             //Do nothing.  The user has requested that we
  61.             //stop changing the image.
  62.         } else {
  63.             //Start animating!
  64.             if (animatorThread == null) {
  65.                 animatorThread = new Thread(this);
  66.             }
  67.             animatorThread.start();
  68.         }
  69.     }
  70.  
  71.     public void stop() {
  72.         //Stop the animating thread.
  73.         animatorThread = null;
  74.     }
  75.  
  76.     public boolean mouseDown(Event e, int x, int y) {
  77.         if (frozen) {
  78.             frozen = false;
  79.             start();
  80.         } else {
  81.             frozen = true;
  82.             stop();
  83.         }
  84.         return true;
  85.     }
  86.  
  87.     public void run() {
  88.         //Just to be nice, lower this thread's priority
  89.         //so it can't interfere with other processing going on.
  90.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  91.  
  92.         //Remember the starting time.
  93.         long startTime = System.currentTimeMillis();
  94.  
  95.         //This is the animation loop.
  96.         while (Thread.currentThread() == animatorThread) {
  97.             //Advance the animation frame.
  98.             frameNumber++;
  99.  
  100.             //Display it.
  101.             repaint();
  102.  
  103.             //Delay depending on how far we are behind.
  104.             try {
  105.                 startTime += delay;
  106.                 Thread.sleep(Math.max(0, 
  107.                                       startTime-System.currentTimeMillis()));
  108.             } catch (InterruptedException e) {
  109.                 break;
  110.             }
  111.         }
  112.     }
  113.  
  114.     public void paint(Graphics g) {
  115.         update(g);
  116.     }
  117.  
  118.     public void update(Graphics g) {
  119.         Color bg = getBackground();
  120.         Color fg = getForeground();
  121.         Dimension d = size();
  122.         boolean fillSquare;
  123.         boolean fillNextFrame;
  124.         int rowWidth = 0;
  125.         int x = 0, y = 0;
  126.         int w, h;
  127.         int tmp;
  128.  
  129.         //Set width of first "square". Decide whether to fill it.
  130.         fillSquare = fillColumnTop;
  131.         fillColumnTop = !fillColumnTop;
  132.         tmp = frameNumber % squareSize;
  133.         if (tmp == 0) {
  134.             w = squareSize;
  135.             fillNextFrame = !fillSquare;
  136.         } else {
  137.             w = tmp;
  138.             fillNextFrame = fillSquare;
  139.         }
  140.  
  141.         //Draw from left to right.
  142.         while (x < d.width) {
  143.             int colHeight = 0;
  144.  
  145.             //Draw the column.
  146.             while (y < d.height) {
  147.                 colHeight += squareSize;
  148.  
  149.                 //If we don't have room for a full square, cut if off.
  150.                 if (colHeight > d.height) {
  151.                     h = d.height - y;
  152.                 } else {
  153.                     h = squareSize;
  154.                 }
  155.  
  156.                 //Draw the rectangle.
  157.                 if (fillSquare) {
  158.                     g.fillRect(x, y, w, h);
  159.                     fillSquare = false;
  160.                 } else {
  161.                     g.setColor(bg);
  162.                     g.fillRect(x, y, w, h);
  163.                     g.setColor(fg);
  164.                     fillSquare = true;
  165.                 } 
  166.  
  167.                 y += h;
  168.             } //while y
  169.  
  170.             //Determine x, y, and w for the next go around.
  171.             x += w;
  172.             y = 0;
  173.             w = squareSize;
  174.             rowWidth += w;
  175.             if (rowWidth > d.width) {
  176.                 w = d.width - x;
  177.             }
  178.             fillSquare = fillColumnTop;
  179.             fillColumnTop = !fillColumnTop;
  180.         } //while x
  181.         fillColumnTop = fillNextFrame;
  182.     }
  183. }
  184.