import java.awt.*; import java.lang.Math; import java.util.Random; public class swarm extends java.applet.Applet implements Runnable { Thread runner; Image offscreenImg; Graphics offscreen; Canvas canvas; Label Nlabel, esizelabel, speedlabel, scalelabel, noiselabel, epslabel; TextField Ntf, esizetf, speedtf, scaletf, noisetf, epstf; Choice areaGeometry; int areageom = 1; int maxN = 100; int numcolors = 64; Color colortab[] = new Color[numcolors]; int colorindex[] = new int[maxN]; int sleeptime = 25; // Milliseconds double speed = .01; double scale = .045; double noise = 1; double eps = 4; int elemSize = 4; int width, height; int canvas_width; int canvas_height; int N = 40; double xp[] = new double[maxN], yp[] = new double[maxN], vx[] = new double[maxN], vy[] = new double[maxN], vqx[] = new double[maxN], vqy[] = new double[maxN]; public void init() { int i; Random r = new Random(); double v; for (i=0; i 1.-xBorderWidth && vx[i] > 0) vx[i] *= -1; if (yp[i] < yBorderWidth && vy[i] < 0) vy[i] *= -1; if (yp[i] > 1.-yBorderWidth && vy[i] > 0) vy[i] *= -1; } } else if (areageom == 2) // Ellipse { double x, y, radius, r2, delta, r1, vr, vphi; delta = .5*(xBorderWidth + yBorderWidth); r1 = .5-delta; for (i=0; i= r1 ) { vr = x*vx[i] + y*vy[i]; if (vr > 0) // would move through border { vx[i] -= 8*vr*x; vy[i] -= 8*vr*y; } } } } else { // undefined geometry // .... } // compute new colors for (i=0; inumcolors) colorindex[i] = numcolors; } repaint(); try { Thread.sleep( sleeptime ); } catch (InterruptedException e) {} } } public boolean action(Event evt, Object arg) { if (evt.target instanceof TextField) { Double dval = new Double(0.); boolean numberFormatError; double newValue; numberFormatError = false; try { dval = Double.valueOf(Ntf.getText()); } catch (NumberFormatException e) { numberFormatError = true; } if ( numberFormatError == false) { newValue = dval.doubleValue(); if (newValue >=1. && newValue <= 100.) N = (int)newValue; } numberFormatError = false; try { dval = Double.valueOf(esizetf.getText()); } catch (NumberFormatException e) { numberFormatError = true; } if ( numberFormatError == false) { newValue = dval.doubleValue(); if (newValue >=1. && newValue <= 100.) elemSize = (int)newValue; } numberFormatError = false; try { dval = Double.valueOf(speedtf.getText()); } catch (NumberFormatException e) { numberFormatError = true; } if ( numberFormatError == false) { newValue = dval.doubleValue(); if (newValue >=0 && newValue <= 1) speed = newValue; } numberFormatError = false; try { dval = Double.valueOf(scaletf.getText()); } catch (NumberFormatException e) { numberFormatError = true; } if ( numberFormatError == false) { newValue = dval.doubleValue(); if (newValue >=0 && newValue <= 100) scale = newValue; } numberFormatError = false; try { dval = Double.valueOf(noisetf.getText()); } catch (NumberFormatException e) { numberFormatError = true; } if ( numberFormatError == false) { newValue = dval.doubleValue(); if (newValue >=0 && newValue <= 100) noise = newValue; } numberFormatError = false; try { dval = Double.valueOf(epstf.getText()); } catch (NumberFormatException e) { numberFormatError = true; } if ( numberFormatError == false) { newValue = dval.doubleValue(); if (newValue >=0 && newValue <= 100) eps = newValue; } return true; } else if (evt.target instanceof Button) { int i; Random r = new Random(); double v; // initial to random positions and directions for (i=0; i