/* Spiro T. Applet connects a set of points on a circle with colorful lines author: Dennis Box, dennis@xnet.com date: 11/24/96 this is my first java program ever */ import java.awt.*; import java.applet.*; import java.util.*; public class Spiro extends Applet implements Runnable{ Vector pointList =null; int nPoints; Math m; Color bgColor = null; Thread aThread = null; boolean threadSuspended = false; //------------------------------ /* set up a (vector) pointList of nPoints in a circle */ public void init() { int cx,cy; //cx,cy center X center Y coordinate double r; //radius Dimension d; //dimension of window int i; String arg = getParameter("NUM_POINTS"); if (arg ==null){ nPoints = 20; }else{ Integer I = new Integer(arg); nPoints = I.intValue(); } arg = getParameter("BGCOLOR"); if (arg ==null){ bgColor = Color.black; }else{ Integer I = new Integer(0); I = I.valueOf(arg,16); bgColor = new Color(I.intValue()); } super.init(); pointList = new Vector(); setLayout(null); addNotify(); d = size(); r = ((d.width > d.height) ? d.height : d.width) / 2.0; cx=d.width/2; cy=d.height/2; //create point list for (i=0; i< nPoints; i++){ double ang = (double)(i*2*m.PI / nPoints); Point p = new Point((int)(r * m.cos(ang) + cx), (int)(r * m.sin(ang) + cy) ); pointList.addElement(p); } //{{INIT_CONTROLS setLayout(null); addNotify(); resize(426,270); //}} } //------------------------------ public boolean handleEvent(Event event) { return super.handleEvent(event); } //------------------------------- public void start() { if(aThread == null) { aThread = new Thread(this); aThread.start(); } } //------------------------------ public void stop() { aThread = null; } //----------------------------- public void run() { while (aThread != null) { try {Thread.sleep(5000);} catch (InterruptedException e){} repaint(); } aThread = null; } //-------------------------------- /* draw colorful lines between the points in pointList */ public void paint(Graphics g) { Dimension r = size(); Random n = new Random (); int i,j,k; g.setColor(bgColor); g.fillRect(0,0,r.width,r.height); for(k=1;k<=nPoints/2;k++){ //isn't garbage collection wonderful? Color dColor = new Color(n.nextFloat(), n.nextFloat(), n.nextFloat()); g.setColor(dColor); for (i=0; i< nPoints; i++){ Point p1,p2; j=(i+k) % nPoints; p1=(Point)pointList.elementAt(i); p2=(Point)pointList.elementAt(j); g.drawLine(p1.x,p1.y,p2.x,p2.y); } } } //---------------------------------- public boolean mouseDown(java.awt.Event evt, int x, int y) { if (threadSuspended) { aThread.resume(); } else { aThread.suspend(); } threadSuspended = !threadSuspended; return true; } }