/*----------------------------------------------------------------------+ | Title: Kaleido.java | | Java beta applet | | | | Author: David E. Joyce | | Department of Mathematics and Computer Science | | Clark University | | Worcester, MA 01610-1477 | | U.S.A. | | | | http://aleph0.clarku.edu/~djoyce/home.html | | djoyce@clarku.edu | | | | Date: version 1.0: August, 1996. mouseEnter; background cols | | version 0.0: November, 1995. First Java version | | Original program written in Pascal | | for a Tektronix 4105 terminal, November, 1983. | +----------------------------------------------------------------------*/ import java.awt.*; import java.applet.Applet; import java.util.StringTokenizer; public class Kaleido extends Applet implements Runnable { Thread tumbler; boolean suspended; int timeout; Color background = Color.lightGray; Color lens = Color.black; int kaleidoSides,kaleidoSidesBy2; double sectorAngle, maxtan, maxlen2; int gMidx, gMidy; int radius; Polygon outline; int nFigures = 10; int figure = 0; int n[] = new int[nFigures]; double a[][] = new double[nFigures][12]; double b[][] = new double[nFigures][12]; Color c[] = new Color[nFigures]; public String getAppletInfo() { return "Kaleido. Copyright 1995, David Joyce, Clark University. Version 1.0"; } public String[][] getParameterInfo() { String[][] pinfo = { {"sides", "int", "sides of the kaleidoscope"}, {"timeout", "int", "milliseconds between figures"}, {"background", "color", "applet background color"}, {"lens", "color", "kaleidoscope lens color"}, }; return pinfo; } Color randomColor() { float hue = (float)(Math.random()*1.0); if (Math.random()<0.2) return new Color(Color.HSBtoRGB(hue, (float)(1.0-Math.random()*Math.random()), // saturation (float)(1.0-Math.random()*Math.random()) // brightness )); else if (Math.random()<0.2) return new Color(Color.HSBtoRGB(hue,1.0f,(float)(0.3+0.7*Math.random()))); else return new Color(Color.HSBtoRGB(hue,(float)(Math.random()),1.0f)); } static String colorName[] = { "black", "blue", "cyan", "darkGray", "gray", "green","lightGray", "magenta", "orange", "pink", "red", "white", "yellow"}; static Color constColor[] = { Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow}; Color parseColor (String str) { if (str==null || "none".equals(str)) return null; if ("random".equals(str)) return randomColor(); if ("background".equals(str)) return background; if ("brighter".equals(str)) return background.brighter(); if ("darker".equals(str)) return background.darker(); for (int i=0; i"); try { return new Color(Integer.parseInt(str,16));} catch (NumberFormatException exc) {} StringTokenizer t = new StringTokenizer(str,","); if (!t.hasMoreTokens()) return null; try { float hue = (float)(Integer.parseInt(t.nextToken())/360.0); if (!t.hasMoreTokens()) return null; float sat = (float)(Integer.parseInt(t.nextToken())/100.0); if (!t.hasMoreTokens()) return null; float bri = (float)(Integer.parseInt(t.nextToken())/100.0); return new Color(Color.HSBtoRGB(hue,sat,bri)); } catch (NumberFormatException exc) { return null; } } public void init() { String param = getParameter("background"); background = parseColor(param); if (background == null) background = Color.lightGray; param = getParameter("lens"); lens = parseColor(param); if (lens == null) lens = Color.black; param = getParameter("timeout"); timeout = 1000; if (param != null) { try { timeout = Integer.parseInt(param);} catch (NumberFormatException exc) {} } param = getParameter("sides"); if (param != null) { try { kaleidoSides = Integer.parseInt(param);} catch (NumberFormatException exc) {} } if (kaleidoSides<4) kaleidoSides = 4; kaleidoSidesBy2 = (kaleidoSides+1)/2; kaleidoSides = kaleidoSidesBy2*2; // kaleidoSides must be even sectorAngle = 2.0 * Math.PI / kaleidoSides; maxtan = (kaleidoSides<=4)? 10000.0 : Math.tan(sectorAngle + 0.05); maxlen2 = Math.cos(sectorAngle/2.0) ; maxlen2 = maxlen2*maxlen2 ; gMidx = size().width/2; gMidy = size().height/2; radius = Math.min(gMidx,gMidy); // determine the outline of the Kaleidoscope outline = new Polygon(); for (int i=1; i<=kaleidoSides; ++i) { double theta = 2.0*Math.PI*i/kaleidoSides + Math.PI/2.0; outline.addPoint (gMidx+(int)(Math.cos(theta)*radius), gMidy+(int)(Math.sin(theta)*radius)); } } public void start() { if (tumbler == null) { tumbler = new Thread(this); tumbler.start(); } } public void stop() { if (tumbler != null) { tumbler.stop(); tumbler = null; } } public void run() { while(true) { try {Thread.currentThread().sleep(timeout+(int)(10.0*Math.random()));} catch (InterruptedException e){} tumble(); } } synchronized void tumble() { repaint(); } public void drawFigure(Graphics g, int n, double a[], double b[], Color c) { double phi,f11,f12,f21,f22,a_prime,b_prime; int i,j; Polygon nextPoly; g.setColor(c); for (i=0; i0) drawFigure(g, n[figure], a[figure], b[figure], lens); // select a random polygon n[figure] = 3 + (int)(Math.random()*(2.0+30.0/kaleidoSides)); double x,y; int j; c[figure] = randomColor(); for (j=0; j x*maxtan || x*x+y*y>maxlen2); a[figure][j] = -y; b[figure][j] = x; } drawFigure(g, n[figure], a[figure], b[figure], c[figure]); figure = (figure+1)%nFigures; } public void paint(Graphics g) { Dimension d = size(); System.out.println("In paint: background="+background); g.setColor(background); g.fillRect(0,0,d.width,d.height); g.setColor(lens); g.fillPolygon(outline); } public boolean mouseEnter(Event evt, int x, int y) { if (suspended) { tumbler.resume(); suspended = false; } return true; } public boolean mouseExit(Event evt, int x, int y) { if (!suspended) { tumbler.suspend(); suspended = true; } return true; } }