home *** CD-ROM | disk | FTP | other *** search
/ Chip Hitware 6 A / CHIP_HITWARE6_A.iso / internet / beams / splash12.exe / data1.cab / Plugins / _Imaging / Zag / Zag.java < prev    next >
Encoding:
Text File  |  1998-12-18  |  4.3 KB  |  123 lines

  1. /
  2. //Zag.java a cool animation, testing flickerless animation routines
  3. //Copyright: Robin Hayes Silicon Graphics Inc. 1996
  4. //
  5. import java.awt.*;
  6. import java.applet.*;
  7. import java.util.*;
  8. import java.net.*;
  9.  
  10. public class Zag extends Applet implements Runnable {
  11.         int Maxi = 30;
  12.         int maxSpeed = 5;
  13.         float hue;
  14.         int xc[];
  15.         int yc[];
  16.         int xd[];
  17.         int yd[];
  18.         Image backplane;
  19.         int xsz, ysz;
  20.  
  21.         public String getAppletInfo() {
  22.                 return "Zag.java written by Robin Hayes, SGI 1996";
  23.         }
  24.         public void init() {
  25.                 String pv;
  26.                 int x,y;
  27.                 int i;
  28.  
  29.                 xsz = 500;
  30.                 ysz = 500;
  31.                 pv = getParameter("numpoints");
  32.                 if(pv != null) {
  33.                         Maxi = Integer.parseInt(pv);
  34.                 }
  35.                 pv = getParameter("xsize");
  36.                 if(pv != null) {
  37.                         xsz = Integer.parseInt(pv);
  38.                 }
  39.                 pv = getParameter("ysize");
  40.                 if(pv != null) {
  41.                         ysz = Integer.parseInt(pv);
  42.                 }
  43.                 xc = new int[Maxi];
  44.                 yc = new int[Maxi];
  45.                 xd = new int[Maxi];
  46.                 yd = new int[Maxi];
  47.                 this.resize(xsz,ysz);
  48.                 backplane = this.createImage(xsz,ysz);
  49.                 hue = (float)(Math.random());
  50.                 for(i=0;i<Maxi;i++)
  51.                 {
  52.                         x = (int)(Math.random() * xsz);
  53.                         y = (int)(Math.random() * ysz);
  54.                         if((int)(Math.random() + .5) == 1)
  55.                         {
  56.                                 xd[i] = -1 * (int)(Math.random() * maxSpeed);
  57.                         }
  58.                         else
  59.                         {
  60.                                 xd[i] = 1 * (int)(Math.random() * maxSpeed);
  61.                         }
  62.                         if((int)(Math.random() + .5) == 1)
  63.                         {
  64.                                 yd[i] = -1 * (int)(Math.random() * maxSpeed);
  65.                         }
  66.                         else
  67.                         {
  68.                                 yd[i] = 1 * (int)(Math.random() * maxSpeed);
  69.                         }
  70.                         xc[i] = x;
  71.                         yc[i] = y;
  72.                 }
  73.         }
  74.         public void paint(Graphics g) {
  75.                 Graphics bp = backplane.getGraphics();
  76.                 bp.setColor(Color.white);
  77.                 bp.fillRect(0,0,xsz,ysz);
  78.                 bp.setXORMode(java.awt.Color.getHSBColor(hue,(float)1.0,(float)1.0));
  79.                 bp.fillPolygon(xc,yc,Maxi);
  80.                 g.drawImage(backplane,0,0,this);
  81.         }
  82.         private Thread animator_thread = null;
  83.         public void start() {
  84.                 if(animator_thread == null) {
  85.                         animator_thread = new Thread(this);
  86.                         animator_thread.start();
  87.                 }
  88.         }
  89.         public void stop() {
  90.                 if((animator_thread != null) && animator_thread.isAlive())
  91.                         animator_thread.stop();
  92.                 animator_thread = null;
  93.         }
  94.  
  95.         public void run() {
  96.                 int i;
  97.  
  98.                 while(true) {
  99.                         hue += .01;
  100.                         if(hue > 1.0)
  101.                         {
  102.                                 hue -= 1.0;
  103.                         }
  104.                         for(i=0;i<Maxi;i++) {
  105.                                 xc[i] += xd[i];
  106.                                 if(xc[i] >= xsz || xc[i] < 0)
  107.                                 {
  108.                                         xd[i] *= -1;
  109.                                         xc[i] += xd[i];
  110.                                 }
  111.                                 yc[i] += yd[i];
  112.                                 if(yc[i] >= ysz || yc[i] < 0)
  113.                                 {
  114.                                         yd[i] *= -1;
  115.                                         yc[i] += yd[i];
  116.                                 }
  117.                         }
  118.                         this.paint(this.getGraphics());
  119.                         try Thread.sleep(50); catch (InterruptedException e);
  120.                 }
  121.         }
  122. }
  123.