/* Ikeda.java * * Provides a Java-based animation of an Ikeda Attractor. * Based on an algorithm from Clifford A. Pickover * * @author L. Gladney * @version 0.0 December 13, 1999 */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.net.*; public class Ikeda extends Applet implements Runnable, MouseListener, MouseMotionListener { Thread kicker=null; //name of animation thread int delay=10; // default sleep time for animation thread int strtx=5, strty=10; // initial starting point for waves (in x,y) int npts=3000; // default number of pts. to generate int r,gr,b; // define color indices int wavefrontlength=140, wavefrontwidth=5; // wavefront width, length (in pixels) int ptsrcwidth=5, ptsrcspacing=20; // diameter, spacing of point sources int ptsrcdist=50; // travel distance between source creations int stepdown=10; // sets speed of response of animation to time int maxwidth=400; // default dimensions for applet window int maxheight=400; double c1=0.0; double c1addto = 0.01; // bump values for c1 parameter boolean runit=true; // set flag to run as default boolean animgo=true; // set animation flag off - let user start it int xbmp = strtx; // keeps track of animation position x, y int ybmp = strty; int xlag, ylag, xset; // x,y on surface, 1st intersection int xret, yret; // retarded x,y positions for wavefront int initcnt = 0; // keep track of initial calls to paint int initdelay=5; // # cycles to wait before redrawing /* Offscreen graphics context for double buffering */ Graphics offScrGC; /* The offscreen image used in double buffering */ Image offScrImage; public void init() { // setSize(maxwidth, maxheight); // set window size to specified limits maxwidth = getSize().width; maxheight = getSize().height; setBackground(Color.white); r = 255; // set default color gr = 0; b = 0; /* read in the parameters from HTML file */ if (getParameter("delay") != null) { delay = Integer.valueOf(getParameter("delay")).intValue(); } /* Add the mouse and mouse motion listeners */ addMouseMotionListener(this); addMouseListener(this); } // end of init /** operate the main thread here */ public void run() { Random rn = new Random(); // create a random number generator Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while (runit) // begin thread loop { if (animgo) { // check the animation flag c1 += c1addto; if (c1 > 0.4) c1 = 0.0; // reset at the useable limit repaint(); // repaint the window try { Thread.currentThread().sleep(delay); // implement delay to slow things down } catch (Exception e) { }; } // end check on animation flag } // end thread loop } // end of run /** override default paint - check for initial delay satisfied */ public void paint(Graphics g) { double scale=100.0; double xoff=1.0*scale; double yoff=2.5*scale; setBackground(Color.white); // animgo = false; // only draw once, don't update /* draw the Ikeda attractor */ // first set the constants determining the curve double c2=0.9; double c3=6.0; double rho=1.0; double x=0.1; double y=0.1; for (int i=1; i initdelay) // check for counter past delay period { /* provide for double-buffered animation */ if (offScrImage == null) { offScrImage = createImage(getSize().width, getSize().height); offScrGC = offScrImage.getGraphics(); } /* now fill in the buffer with background color to cover over previous image */ offScrGC.setColor(getBackground()); offScrGC.fillRect(0,0,getSize().width, getSize().height); offScrGC.setColor(getForeground()); /* call paint to fill in the picture in the off screen buffer, then xfer buffer to screen */ paint(offScrGC); g.drawImage(offScrImage, 0, 0, null); } else { // if not past delay, bump the counter initcnt++; } // end check for delay period } // end of update /** start up the animation thread */ public void start() { if (kicker == null) { kicker = new Thread(this); kicker.start(); } } /** stop the animation thread */ public void stop() { if (kicker != null) { kicker.stop(); kicker=null; } } /** Sets the animation counter */ public void setAnimationCounter(int cntval) { xbmp = cntval; } /** documentation methods */ public String getAppletInfo() { return "Ikeda by Larry Gladney "; } public String[][] getParameterInfo() { String [][] info = { {"delay ","int ", "get delay factor for animation in msec"}, }; return info; } public void mouseReleased(MouseEvent e) { } public void mouseMoved(MouseEvent evt) { } public void mousePressed(MouseEvent evt) { } public void mouseDragged(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent evt) { evt.consume(); // we are handling this event here animgo = true; if (r == 255) // shift the current color scheme { r = 150; gr = 150; b = 0; } else if (r == 150) { r = 180; gr = 0; b = 180; } else { r = 255; gr = 0; b = 0; } // end check for current color scheme repaint(); } }