import java.awt.*; import java.applet.*; import java.util.*; public class Structure extends Applet implements Runnable { final static int NUM_POINTS = 128; final static int SCALE = 4; final static int NUM_STEPS = 64; final static int SIZE = 256; final static int HALFSIZE = 128; boolean threadSuspended = false; //added by C.Y. Graphics gOff; Image im; Point dots[] = null; double where[] = null; double phase[] = null; double extent[] = null; Color color[] = null; Thread kicker = null; public Structure() { dots = new Point[NUM_POINTS]; phase = new double[NUM_POINTS]; extent = new double[NUM_POINTS]; where = new double[NUM_POINTS]; color = new Color[NUM_STEPS*2]; int i; for(i = 0; i < NUM_STEPS/2; i++) { color[i] = new Color(i * 4 + 128, // red 0, 64 - i*2); color[NUM_STEPS - i -1] = new Color(color[i].getRGB()); } for(i = 0; i < NUM_STEPS/2; i++) { color[NUM_STEPS+i] = new Color(128 - i*4, 0, 64 + i*6); //255 - i * 12); color[NUM_STEPS*2 - i -1] = new Color(color[NUM_STEPS+i].getRGB()); } } public void init() { resize(SIZE+6,SIZE+6); int i; Random r = new Random(); for(i =0; i < NUM_POINTS; i++) { extent[i] = Math.sqrt(Math.sin(i*Math.PI/NUM_POINTS)); dots[i] = new Point(0,0); dots[i].y = i * SIZE / NUM_POINTS; phase[i] = r.nextFloat() * Math.PI * 2; } im = createImage(size().width,size().height); gOff = im.getGraphics(); repaint(); } public void start() { if (kicker == null) { kicker = new Thread(this); kicker.start(); } } public void stop() { if (kicker != null) { kicker.stop(); kicker = null; } } public void run() { while(kicker != null) { try { tick(); repaint(); kicker.sleep(200); } catch (InterruptedException e){}; // fixed by C.Y. } } // ** Added by C.Y. public boolean mouseDown(java.awt.Event evt, int x, int y) { if (threadSuspended) { kicker.resume(); } else { kicker.suspend(); } threadSuspended = !threadSuspended; return true; } // ** public void tick() { double step = Math.PI/NUM_STEPS; double angle = 0; double twopi = 2*Math.PI; int i; for(i = 1; i < NUM_POINTS; i++) { where[i] = Math.cos( phase[i]); dots[i].x =(int) (HALFSIZE *( 1.0+ ( ( where[i]) * extent[i]))); phase[i]+= step; if(phase[i] > twopi) phase[i] -= twopi; } } public void update(Graphics g) { gOff.setColor(Color.black); gOff.fillRect(0,0,size().width,size().height); paint(gOff); g.drawImage(im,0,0,this); } public void paint(Graphics g) { int i; Dimension d = size(); g.setColor(Color.red); g.drawRect(0,0,d.width-1,d.height-2); g.drawRect(1,1,d.width-2,d.height-3); g.drawRect(2,2,d.width-3,d.height-4); //for(i = 1; i< NUM_POINTS; i++) for(i = NUM_POINTS-1; i > 0; i--) { int c = (int)((where[i]+1.0)*32); int size = 6; // modified by C.Y. in this part int newsize= size+ (int) ( extent[i] * 4.0 + (Math.sin(where[i]+(Math.PI/2.0)) )*6.0); if(phase[i] > Math.PI) { c+= NUM_STEPS; size-=2; newsize= size + (int) ( extent[i] * 4.0 ); // modified by C.Y. } g.setColor(color[c]); g.fillOval(dots[i].x,dots[i].y,newsize,newsize); // modified by C.Y. } } }