To Generate Hénon's Strange Attractor
/**
* @author Henon's Strange Attractor Generator
* @author Robert J Morton
* @version 30 October 1997 */
import java.awt.*;
public class Henon extends java.applet.Applet implements Runnable
{
int XBIAS = 150, YBIAS = 150; //pixel co-ordinates of real point 0,0
int XMAX = 300, YMAX = 300; //dimensions of the display area (pixels)
double Scale = 100; //pixels per unit real eg 100 pixels = 1.00d
double x = 0, y = 0, z; //real co-ordinates of 'chaotic body'
int i, j; //its pixel co-ordinates within the visible x-y plane
Color A[][], colour, //colour array and current plot colour
bgc = Color.lightGray, //background colour
fgc, //foreground colour
axc = Color.gray; //axes colour
Thread HenonThread; //declare a thread reference variable
long TimeFrame = 150, T; //inter-plot time frame (milliseconds)
public void init() {
A = new Color[XMAX][YMAX]; //create colour array for the display area
for(i = 0 ; i < XMAX ; i++) //clear the colour array to background colour
for(j = 0 ; j < YMAX ; j++)
A[i][j] = bgc;
for(i = 0 ; i < XMAX ; i++) A[i][YBIAS] = axc; //create the x-axis graticule
for(j = 0 ; j < YMAX ; j++) A[XBIAS][j] = axc; //create the y-axis graticule
fgc = new Color(0, 128, 64); //colour for the plots
T = System.currentTimeMillis() + TimeFrame; //end of first plot's time frame
}
public void paint(Graphics g) { //PAINT / RE-PAINT THE PICTURE SO FAR
for(j = 0 ; j < YMAX ; j++) { //for each of the YMAX lines in the picture
int I = 0; //starting dot of next single-colour stretch of line
i = 0; //to find next pixel in current line with a different colour
colour = A[I][j]; //make colour of first dot the reference colour
while(++i < XMAX) { //while not yet reached end of current line
if(colour != A[i][j]) { //if this dot a different colour from previous dot
g.setColor(colour); //set colour to colour of previous dot
g.drawLine(I, j, i - 1, j); //draw line from start dot to previous dot
colour = A[I = i][j]; //make reference colour that of this dot
}
}
g.setColor(colour); //set colour for final stretch of current line
g.drawLine(I, j, i - 1, j); //draw the last stretch of the current line
}
}
public void update(Graphics g) { //called in response a request for a repaint()
z = x; //z inherits last time's value of x
x = 1.4 * x * x + 0.3 * y - 1; //advance x
y = z; //advance y
int i = XBIAS + (int)(x * Scale); //Compute the pixel co-ordinates
int j = YBIAS - (int)(y * Scale); // corresponding to the new values of x and y.
A[i][j] = fgc; //put the plot on the 'canvas'
g.setColor(fgc); //set colour for plot
g.drawLine(i, j, i, j); //draw the plot in the applet window
}
public void run() { //run the Track Recorder painting thread
while(true) { //permanent loop
repaint(); //paint in the next plot
long s = T - System.currentTimeMillis(); //get time left in this plot's time frame
if(s < 5) s = 5; //in case machine isn't fast enough
try { Thread.currentThread().sleep(s); //sleep for the time remaining
} catch (InterruptedException e) { } //catch interrupt from GUI or browser
T = System.currentTimeMillis() + TimeFrame; //get when next plot's time frame will end
}
}
public void start() { //Start the program thread by
HenonThread = new Thread(this); //creating the thread object
HenonThread.start(); //and starting it running by
} //requesting a call to run()
public void stop() { HenonThread.stop(); } //Stop this a program thread
/* Robert J Morton, the author of this program,
is a poor but Right Honourable Fellow of the
Ancient and Noble Order of the Long-term Unemployed.
Offers of work please to: robmorton@clara.net */
}
This page's parent within this Web Site.
About this Web Site.
Its home page.
Email its Author.