/* * Liny.java - A cute bouncing lines demo that runs on HotJava * * This was a quick hack from C code, no comments yet... * It would appear that I need to redraw if the applet is * scrolled around on the screen, but I'm not sure yet. * * $Id$ * $Log$ * */ import java.awt.*; import java.applet.*; /* Beginning of liny class */ public class Liny extends java.applet.Applet implements Runnable { Thread animate=null; int lx[][] = new int[40][257]; int ly[][] = new int[40][257]; int dx[] = new int[40]; int dy[] = new int[40]; int xx[] = new int[40]; int yy[] = new int[40]; Color pal[] = new Color[257]; int z, numlins, traces; int maxx, maxy, minx, miny; int i, ii, iii, gravity, randon; int q; float a1, a2; int rand() { return (int) (Math.random() * 30000); } Color HSItoRGB(double h, double s, double i) { double rv, gv, bv; double PI, t; Color col; int r, g, b; PI=3.1415926; t = 2 * PI * h; rv=1.0 + s * Math.sin(t - 2.0 * PI / 3.0); gv=1.0 + s * Math.sin(t); bv=1.0 + s * Math.sin(t + 2.0 * PI / 3.0); t=255.9999999 * i / 2; r = (int) (rv * t); g = (int) (gv * t); b = (int) (bv * t); col = new Color(r, g, b); return col; } public void init() { int red, green, blue; double hue; Dimension mydim; setSize(500, 500); mydim = getSize(); setBackground(Color.black); numlins=3; traces=130; gravity=0; randon=0; if (getParameter("lines")!=null) { Integer lines=new Integer(getParameter("lines")); numlins=lines.intValue(); } if (getParameter("traces")!=null) { Integer ntraces=new Integer(getParameter("traces")); traces=ntraces.intValue(); } if (getParameter("gravity")!=null) { Integer grav=new Integer(getParameter("gravity")); gravity=grav.intValue(); } if (getParameter("random")!=null) { Integer randn=new Integer(getParameter("random")); randon=randn.intValue(); } if (traces>255) traces=255; if (numlins>40) numlins=39; maxx=mydim.width - 21; maxy=mydim.height - 21; minx=20; miny=20; for (i=0; i<=numlins+1; i++) { xx[i]=rand() % maxx; yy[i]=rand() % maxy; dx[i]=rand() % 20 - 10; dy[i]=rand() % 20 - 10; } for (i=0; i<=numlins; i++) { for (z=0; z<=traces; z++) { lx[i][z]=xx[i]; ly[i][z]=yy[i]; } dx[i] = rand() % 10 - 2; dy[i] = rand() % 10 - 2; } q=1; for (i=1; i<(traces + 1); i++) { hue=(i/(traces * 1.0)); pal[i] = HSItoRGB(hue, 0.95, 1.0); } pal[0]=Color.black; } public void run() { while (true) { try { Thread.sleep(8); } catch(InterruptedException e) {} for (i=1; i<=numlins; i++) { xx[i]=xx[i]+dx[i]; yy[i]=yy[i]+dy[i]; if (xx[i]>maxx) dx[i]= -(Math.abs(dx[i])); if (xx[i]maxy) dy[i]= -(Math.abs(dy[i])); if (yy[i] 97) { i=rand() % numlins + 1; dx[i]=dx[i] + rand() % 5 - 2; dy[i]=dy[i] + rand() % 5 - 2; } } for (i=1; i<=numlins; i++) { if (dx[i] < -15) dx[i] = -15; if (dx[i] > 15) dx[i] = 15; if (dy[i] < -15) dy[i] = -15; if (dy[i] > 15) dy[i] = 15; } } /* infinite loop */ } public void start() { if (animate == null) { animate = new Thread(this); animate.start(); } } public void stop() { if (animate != null) { animate.stop(); animate=null; } } public synchronized void paint(Graphics g) { int num; Dimension mydim = getSize(); g.setColor(pal[(q % traces) + 1]); for (i=1; i