// Funky applet to draw some oscillating sine waves, remove one // works because the dots are random enough that removing a wave // at the peak isn't easy to notice. import java.applet.*; import java.awt.*; import java.util.*; import SinBox; public class Disappear extends DoubleBufferApplet implements Runnable { Vector sinboxes; Vector activeSinboxes; Thread animationThread; int frameDelay = 100; SinBox selectedBox; long clickTime; public void init() { this.setBackground(Color.white); this.setForeground(Color.black); update(getGraphics()); // force window create String frameDelayString = getParameter("frameDelay"); try { if (frameDelayString != null) frameDelay = Integer.parseInt(frameDelayString); } catch (Exception e) {} sinboxes = new Vector(3); sinboxes.addElement(new SinBox(imageBufferSize.width/2, imageBufferSize.height/4, 0.3, 0.05)); sinboxes.addElement(new SinBox(imageBufferSize.width/2, imageBufferSize.height/2, 0.7, 0.04)); sinboxes.addElement(new SinBox(imageBufferSize.width/2, 3*imageBufferSize.height/4, 0.15, 0.06)); activeSinboxes = new Vector(3); activeSinboxes.addElement(sinboxes.elementAt(1)); } public void run() { while (Thread.currentThread() == animationThread) { clearImageBuffer(); Graphics g = imageBuffer.getGraphics(); // update active curves Enumeration e = activeSinboxes.elements(); while (e.hasMoreElements()) { SinBox sb = (SinBox)e.nextElement(); sb.updateCurve(); } // render all curves e = sinboxes.elements(); while (e.hasMoreElements()) { SinBox sb = (SinBox)e.nextElement(); sb.render(g); } repaint(); // test to see if we can remove one if (sinboxes.size() == 3) { boolean canRemove = true; e = sinboxes.elements(); while (e.hasMoreElements() && canRemove) { SinBox sb = (SinBox)e.nextElement(); if (!sb.nearMaximum()) canRemove = false; } if (canRemove) { SinBox middleBox = (SinBox)sinboxes.elementAt(1); sinboxes.removeElement((Object)middleBox); activeSinboxes.removeElement(middleBox); System.out.println("Poof"); } } // sleep for next update.. try { Thread.sleep(frameDelay); } catch (InterruptedException ex) { System.out.println("Sleep Interrupted??"); } } } // thread toggle public boolean keyDown(Event e, int key) { if ((char)key == ' ') { if (animationThread == null) { animationThread = new Thread(this); animationThread.start(); } else { animationThread.stop(); animationThread = null; } } return true; } public void start() { super.start(); animationThread = new Thread(this); animationThread.start(); } public void stop() { animationThread = null; super.stop(); } public boolean mouseDown(Event ev, int mouseX, int mouseY) { selectedBox = null; clickTime = System.currentTimeMillis(); // figure out which wave was clicked Enumeration e = sinboxes.elements(); while (e.hasMoreElements() && selectedBox == null) { SinBox sb = (SinBox)e.nextElement(); if (sb.box.inside(mouseX, mouseY)) { selectedBox = sb; } } // toggle its active status if (selectedBox != null) { if (activeSinboxes.contains(selectedBox)) activeSinboxes.removeElement(selectedBox); else activeSinboxes.addElement(selectedBox); } return true; } public void paint(Graphics og) { Graphics g = imageBuffer.getGraphics(); Enumeration e = sinboxes.elements(); while (e.hasMoreElements()) { SinBox sb = (SinBox)e.nextElement(); sb.render(g); } super.paint(og); } }