/* Copyright 1998, John C. Lahr */ import java.awt.*; import java.applet.*; public class spiral extends Applet implements Runnable{ Dimension size; // Screen dimensions of applet boolean please_stop; Image buffer; protected Image john; protected MediaTracker tracker; Graphics bufferGraphics; Thread runner; double ncycles = 12.; double ncircles = 6.; double diameter = 200.; double bgdelt = diameter/ncircles; double smdelt = bgdelt/ncycles; double diamst; double a = Math.pow(diameter, 1./(double)(ncircles-1)); double b = 1.0/Math.pow(a, (1./ncycles)); double t, lastt = 1.0; double diam = 1.; int j = 0; int section = 1; int frame_delay = 10; int duration = 1500; int nloops = duration/frame_delay; Button repeat_button; Button speedup_button; double xcenter; double ycenter; public void init() { repeat_button = new Button("Repeat at same speed"); this.add(repeat_button); speedup_button = new Button("Speed up"); this.add(speedup_button); tracker = new MediaTracker(this); String file = "john.gif"; john = this.getImage(this.getDocumentBase(), file); tracker.addImage(john, 1); size = this.size(); buffer = this.createImage(size.width, size.height); bufferGraphics = buffer.getGraphics(); xcenter = size().width*0.5; ycenter = size().height*0.5; } public void start() { if (runner == null) { please_stop = false; runner = new Thread(this); runner.start(); } } public void stop() { please_stop = true; } public void paint(Graphics g) { /* System.out.println("begin paint"); */ if (section == 1) { /* System.out.println("section 1"); */ bufferGraphics.setColor(Color.black); // fill with black bufferGraphics.fillRect(0, 0, size().width, size().height); bufferGraphics.setColor(Color.white); bufferGraphics.drawString("Focus your view on the center of the circles", 185, 450); bufferGraphics.drawString("Speed = " + nloops, 185,465); g.drawImage(buffer, 0, 0, this); } if (section == 2) { /* System.out.print("section 2: nloops = "); System.out.println(nloops); */ bufferGraphics.setColor(Color.white); // white circle bufferGraphics.fillOval((int)(xcenter-diameter*0.5), (int)(ycenter-diameter*0.5), (int)diameter, (int)diameter); bufferGraphics.setColor(Color.black); diam = diamst; for (j = 0; j < ncircles; j++) { bufferGraphics.drawOval((int)(xcenter-diam*0.5), (int)(ycenter-diam*0.5), (int)diam, (int)diam); diam = diam - bgdelt; } g.drawImage(buffer, 0, 0, this); } if (section == 3) { /* System.out.println("section 3"); */ bufferGraphics.setColor(Color.black); // fill with black bufferGraphics.fillRect(0, 0, size().width, size().height); bufferGraphics.drawImage(john, 140, 145, this); g.drawImage(buffer, 0, 0, this); } } public void run() { /* System.out.println("begin run"); */ section = 1; System.out.println("Begin run routine"); repaint(0, 0, size().width, size().height); try {Thread.sleep(20);} // Pause xx ms catch (InterruptedException e) {;} section = 2; for (int i = 0; i < nloops; i++) { diamst = diameter; for (int kk = 0; kk < ncycles; kk++) { repaint( (int) (xcenter-0.5*diameter), (int) (ycenter-0.5*diameter), (int) diameter, (int) diameter); try {Thread.sleep(frame_delay);} // Pause xx ms catch (InterruptedException e) {;} diamst = diamst - smdelt; } } section = 3; repaint(); runner = null; } public void update(Graphics g) { paint(g); } /** Repeat at same speed **/ public boolean action(Event e, Object arg) { if (e.target == repeat_button) { /* System.out.println("pressed repeat button"); */ start(); return true; } if (e.target == speedup_button) { /* System.out.println("pressed speedup button"); */ frame_delay = frame_delay - 2; if (frame_delay <= 2) frame_delay = 10; nloops = duration/frame_delay; start(); return true; } return false; } }