/* PatternApplet.java - draws 'spirograph' patterns */ import java.awt.*; class OuterGear { //instance variables Dimension centre; int rad; int angle; //constructor public OuterGear(Dimension centre, int rad) { this.centre = centre; this.rad = rad; angle = 0; } } class InnerGear { //class variables static private float sines[] = new float[360]; static private float cosines[] = new float[360]; //instance variables OuterGear outer; int rad, penrad; int angle; int numTurns, maxNumTurns; float ratio, radDiff; Color c; boolean donePaint = false; boolean firstPaint = true; //initalise class variables static { double x, dx; dx = 6.283 / 360; x = 0.0; for (int i=0; i<360; i++) { sines[i] = (float)Math.sin(x); cosines[i] = (float)Math.cos(x); x += dx; } } //constructor public InnerGear(int rad, int penrad, OuterGear outer, Color c) { if (rad > outer.rad) { System.out.println("error - inner rad > outer rad"); System.exit(0); } if (penrad > rad) { System.out.println("error - pen rad > gear rad"); System.exit(0); } this.rad = rad; this.penrad = penrad; this.outer = outer; this.c = c; ratio = (float)rad / (float)outer.rad; radDiff = (float)(outer.rad - rad); angle = 0; numTurns = 0; maxNumTurns = getMaxNumTurns(); } //return number of turns required to complete the pattern public int getMaxNumTurns() { int i = 1; int j = 1; int radA, radB; while(true) { radA = i * rad; radB = j * outer.rad; if (radA == radB) break; if (radA > radB) { j++; } else { i++; j = 1; } } return i; } //work out pen position given current angle public Dimension getPenPos(int angle) { Dimension d = new Dimension(); outer.angle = (int)((float)(angle + numTurns * 360) * ratio); if (outer.angle > 359) outer.angle -= (outer.angle / 360) * 360; d.width = outer.centre.width + (int)(radDiff * cosines[outer.angle] + (float)penrad * cosines[angle]); d.height = outer.centre.height + (int)(radDiff * sines[outer.angle] - (float)penrad * sines[angle]); return d; } public Dimension paintTrail(Graphics g) { g.setColor(c); Dimension oldP, newP; Dimension d = new Dimension(); oldP = getPenPos(angle); for (int i=0; i<60; i++) { angle++; if (angle == 360) { angle = 0; numTurns++; if (numTurns == maxNumTurns) donePaint = true; } newP = getPenPos(angle); g.drawLine(oldP.width, oldP.height, newP.width, newP.height); oldP = newP; if (i == 30) d = oldP; } if (firstPaint) firstPaint = false; return d; } } public class PatternApplet extends java.applet.Applet implements Runnable { //class variables Thread t = null; int index; int numOuts, numIns; int outR[]; int xOff[]; int yOff[]; int inR[]; int penR[]; int inOut[]; int col[]; OuterGear outer; InnerGear inner; Image vImage; Graphics vg; Dimension centre, clipper; int clip; //initialise applet public void init() { setBackground(Color.black); centre = getCentre(); getGears(); index = 0; } public void getGears() { String p = getParameter("numOuts"); numOuts = (p != null) ? Integer.valueOf(p).intValue() : 1; p = getParameter("numIns"); numIns = (p != null) ? Integer.valueOf(p).intValue() : 1; outR = new int[numOuts]; xOff = new int[numOuts]; yOff = new int[numOuts]; inR = new int[numIns]; penR = new int[numIns]; inOut = new int[numIns]; col = new int[numIns]; p = getParameter("outR0"); outR[0] = (p != null) ? Integer.valueOf(p).intValue() : 200; p = getParameter("xOff0"); xOff[0] = (p != null) ? Integer.valueOf(p).intValue() : 0; p = getParameter("yOff0"); yOff[0] = (p != null) ? Integer.valueOf(p).intValue() : 0; p = getParameter("inR0"); inR[0] = (p != null) ? Integer.valueOf(p).intValue() : 65; p = getParameter("penR0"); penR[0] = (p != null) ? Integer.valueOf(p).intValue() : 65; p = getParameter("inOut0"); inOut[0] = (p != null) ? Integer.valueOf(p).intValue() : 0; p = getParameter("col0"); col[0] = (p != null) ? Integer.valueOf(p).intValue() : 0; String parm; if (numIns > 1) { for (int i=0; i 1) { for (int i=0; i