/* SpiralApplet.java - draws spiral patterns */ import java.awt.*; import java.util.Random; class Spiral { //class variables float angstep; float radstep; int numsteps; int maxColor; Dimension size; Color colors[]; //constructor public Spiral() { maxColor = 6; colors = new Color[7]; colors[0] = new Color(255, 0, 0); colors[1] = new Color(255, 255, 0); colors[2] = new Color(0, 255, 0); colors[3] = new Color(255, 0, 255); colors[4] = new Color(0, 0, 255); colors[5] = new Color(255, 128, 0); colors[6] = new Color(0, 255, 255); } //set the pattern's parameters public void setParms() { maxColor = 6; float drift = 0; Random r = new Random(); int type = 2 + (int)(r.nextFloat() * (float)6); float mid = (float)6.283 / (float)type; switch (type) { case 2: type = 9 + (int)(r.nextFloat() * (float)20); mid = (float)6.283 * (float)type / (float)(type * 2 + 1); drift = (float).004; radstep = (float)2 + r.nextFloat() * (float)4 * (float)type / (float)20; numsteps = 200 + type * 35; break; case 3: drift = (float).05; radstep = (float)1 + r.nextFloat() * (float)4; numsteps = (int)((float)size.width / radstep); break; case 4: drift = (float).04; radstep = (float)1 + r.nextFloat() * (float)2.5; numsteps = (int)((float)size.width / radstep); break; case 5: drift = (float).02; radstep = (float)1 + r.nextFloat() * (float)1.4; numsteps = (int)((float)size.width / radstep); break; case 6: drift = (float).017; radstep = (float)1 + r.nextFloat() * (float).8; numsteps = (int)((float)size.width / radstep); break; case 7: drift = (float).016; radstep = (float).9 + r.nextFloat() * (float).5; numsteps = (int)((float)size.width / radstep); maxColor = 5; break; case 8: drift = (float).015; radstep = (float).8 + r.nextFloat() * (float).4; numsteps = (int)((float)size.width / radstep); break; } angstep = mid + (r.nextFloat() - (float).5) * drift; } //receive window size from applet public void setSize(Dimension size) { this.size = size; } //draw pattern public void paint(Graphics g) { int xold, yold; int xcent = size.width / 2; int ycent = size.height / 2; int xnew = xcent; int ynew = ycent; float rad = 0; float ang = 0; int c = 0; for (int i=0; i maxColor) c = 0; g.drawLine(xold, yold, xnew, ynew); } } } public class SpiralApplet extends java.applet.Applet implements Runnable { //instance variables Thread t = null; private Spiral sp; private long bedtime; private Button quitButton, autoButton, newButton, fastButton, slowButton; private boolean auto, painting; //initialise applet public void init() { this.setBackground(Color.black); this.setLayout(new BorderLayout()); Panel p = new Panel(); p.setBackground(Color.lightGray); p.setLayout(new FlowLayout()); p.add(autoButton = new Button("auto/man")); p.add(newButton = new Button("new pattern")); p.add(fastButton = new Button("faster")); p.add(slowButton = new Button("slower")); this.add("North", p); auto = true; painting = false; setButtons(); bedtime = (long)2001; } public void run() { t.setPriority(Thread.NORM_PRIORITY); while (t != null) { if (auto) newPattern(); try { Thread.sleep(bedtime); } catch (InterruptedException e) {} } } public void start() { if (t == null) { t = new Thread(this); t.start(); } } public void stop() { if (t != null) { t.stop(); t = null; } } private void newPattern() { if (sp == null) sp = new Spiral(); if (!painting) { painting = true; sp.setSize(size()); sp.setParms(); //workaround for netscape gold ?????? if (this.getBackground() != Color.black) this.setBackground(Color.black); repaint(); } } private void setButtons() { if (auto) { newButton.disable(); fastButton.enable(); slowButton.enable(); } else { newButton.enable(); fastButton.disable(); slowButton.disable(); } } public void paint(Graphics g) { painting = true; if (sp != null) { sp.setSize(size()); sp.paint(g); } painting = false; } public boolean action(Event e, Object o) { if (e.target == autoButton) { auto = !auto; setButtons(); return true; } else if ((e.target == newButton) && (!auto)) { newPattern(); return true; } else if (e.target == fastButton) { if (bedtime > (long)1000) bedtime -= (long)1000; return true; } else if (e.target == slowButton) { bedtime += (long)1000; return true; } return super.action(e, o); } }