/** Henon's Strange Attractor Generator by Robert J Morton UK-YE572246C 30 October 1997 */ import java.awt.*; public class henon extends java.applet.Applet implements Runnable { int XBIAS = 150, // pixel co-ordinates of real point 0,0 YBIAS = 150, XMAX = 300, // dimensions of the display area (pixels) YMAX = 300, i, // pixel co-ordinates of the chaotic body j; // within the visible xy plane long TF = 150, T; // inter-plot time frame (milliseconds) double Scale = 100, //pixels per unit real eg 100 pixels = 1.00d x = 0, y = 0, z; //real co-ordinates of 'chaotic body' Color A[][], // colour array and current plot colour colour, bgc = Color.black, // background colour fgc = Color.white, // foreground colour axc = Color.gray; // axes colour Thread HT; // declare a thread reference variable public void init() { // Create a colour array for the display area. A = new Color[XMAX][YMAX]; // Clear the whole colour array to the background colour. for(i = 0 ; i < XMAX ; i++) for(j = 0 ; j < YMAX ; j++) A[i][j] = bgc; // Create the x and y axis graticules. for(i = 0 ; i < XMAX ; i++) A[i][YBIAS] = axc; for(j = 0 ; j < YMAX ; j++) A[XBIAS][j] = axc; // Set the system time at which the first time frame will finish. T = System.currentTimeMillis() + TF; } public void paint(Graphics g) { // PAINT/RE-PAINT THE PICTURE SO FAR // For each of the YMAX lines in the picture ... for(j = 0 ; j < YMAX ; j++) { int I = 0; //starting dot of next single-colour stretch of line /* To find next pixel in current line with a different colour, make the colour of first dot the reference colour, then while we have not yet reached the end of the current line ... */ i = 0; colour = A[I][j]; while(++i < XMAX) { /* If this dot is a different colour to previous dot, set the drawing colour to the colour of previous dot, draw a line from the start dot to the previous dot and make the reference colour that of this dot. */ if(colour != A[i][j]) { g.setColor(colour); g.drawLine(I,j,i - 1,j); colour = A[I = i][j]; } } /* Set the colour for the final stretch of the current line and draw the last stretch of the current line. */ g.setColor(colour); g.drawLine(I,j,i - 1,j); } } // called in response a request for a repaint() public void update(Graphics g) { 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 /* Compute the pixel co-ordinates cor- responding to the new values of x and y */ int i = XBIAS + (int)(x * Scale), j = YBIAS - (int)(y * Scale); 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 Henon Attractor's painting thread while(true) { // permanent loop repaint(); // paint in the next plot /* Get the time left in this plot's time frame and ensure that it is at lease 5 milliseconds. Then put the thread to sleep for this remaining amount of time. */ long s = T - System.currentTimeMillis(); if(s < 5) s = 5; try { Thread.currentThread().sleep(s); } /* Catch any external events here during the thread's sleep time but do nothing about them because they are normal events. */ catch (InterruptedException e) { } // Set the system time at which the next plots time frame will end. T = System.currentTimeMillis() + TF; } } /* Start the program thread by creating the thread object and starting it running by requesting a call to run(). */ public void start() { HT = new Thread(this); HT.start(); } public void stop() { HT = null; } // Stop this a program thread }