home *** CD-ROM | disk | FTP | other *** search
- /
- //Zag.java a cool animation, testing flickerless animation routines
- //Copyright: Robin Hayes Silicon Graphics Inc. 1996
- //
- import java.awt.*;
- import java.applet.*;
- import java.util.*;
- import java.net.*;
-
- public class Zag extends Applet implements Runnable {
- int Maxi = 30;
- int maxSpeed = 5;
- float hue;
- int xc[];
- int yc[];
- int xd[];
- int yd[];
- Image backplane;
- int xsz, ysz;
-
- public String getAppletInfo() {
- return "Zag.java written by Robin Hayes, SGI 1996";
- }
- public void init() {
- String pv;
- int x,y;
- int i;
-
- xsz = 500;
- ysz = 500;
- pv = getParameter("numpoints");
- if(pv != null) {
- Maxi = Integer.parseInt(pv);
- }
- pv = getParameter("xsize");
- if(pv != null) {
- xsz = Integer.parseInt(pv);
- }
- pv = getParameter("ysize");
- if(pv != null) {
- ysz = Integer.parseInt(pv);
- }
- xc = new int[Maxi];
- yc = new int[Maxi];
- xd = new int[Maxi];
- yd = new int[Maxi];
- this.resize(xsz,ysz);
- backplane = this.createImage(xsz,ysz);
- hue = (float)(Math.random());
- for(i=0;i<Maxi;i++)
- {
- x = (int)(Math.random() * xsz);
- y = (int)(Math.random() * ysz);
- if((int)(Math.random() + .5) == 1)
- {
- xd[i] = -1 * (int)(Math.random() * maxSpeed);
- }
- else
- {
- xd[i] = 1 * (int)(Math.random() * maxSpeed);
- }
- if((int)(Math.random() + .5) == 1)
- {
- yd[i] = -1 * (int)(Math.random() * maxSpeed);
- }
- else
- {
- yd[i] = 1 * (int)(Math.random() * maxSpeed);
- }
- xc[i] = x;
- yc[i] = y;
- }
- }
- public void paint(Graphics g) {
- Graphics bp = backplane.getGraphics();
- bp.setColor(Color.white);
- bp.fillRect(0,0,xsz,ysz);
- bp.setXORMode(java.awt.Color.getHSBColor(hue,(float)1.0,(float)1.0));
- bp.fillPolygon(xc,yc,Maxi);
- g.drawImage(backplane,0,0,this);
- }
- private Thread animator_thread = null;
- public void start() {
- if(animator_thread == null) {
- animator_thread = new Thread(this);
- animator_thread.start();
- }
- }
- public void stop() {
- if((animator_thread != null) && animator_thread.isAlive())
- animator_thread.stop();
- animator_thread = null;
- }
-
- public void run() {
- int i;
-
- while(true) {
- hue += .01;
- if(hue > 1.0)
- {
- hue -= 1.0;
- }
- for(i=0;i<Maxi;i++) {
- xc[i] += xd[i];
- if(xc[i] >= xsz || xc[i] < 0)
- {
- xd[i] *= -1;
- xc[i] += xd[i];
- }
- yc[i] += yd[i];
- if(yc[i] >= ysz || yc[i] < 0)
- {
- yd[i] *= -1;
- yc[i] += yd[i];
- }
- }
- this.paint(this.getGraphics());
- try Thread.sleep(50); catch (InterruptedException e);
- }
- }
- }
-