/** Daryl Rauhala * File: PlotCanvas.java * Henon Cavas with some flexability build in */ import java.awt.*; public class PlotCanvas extends Canvas { private static double maxDomX, maxDomY, minDomX, minDomY; private static double canHeight, canWidth, A, B; private double initX, initY; private int numPoints = 10000; public PlotCanvas() { super(); } public void setToCanvasSize() { Rectangle temp = getBounds(); canHeight = (double) temp.height; canWidth = (double) temp.width; } public void setHenonDefaults() { maxDomX = 2; maxDomY = 0.5; minDomX = -2; minDomY = -0.75; initX = 0.1; initY = 0.2; A = 1.4; B = 0.3; numPoints = 10000; } public void setUserDomain(double maxx,double maxy,double minx,double miny) { maxDomX = maxx; maxDomY = maxy; minDomX = minx; minDomY = miny; } public void setInitState(double x, double y) { initX = x; initY = y; } public void setIterations( int iterations ) { numPoints = iterations; } public void setParametersAB( double a, double b ) { A = a; B = b; } /** * These mapping functions set according to the given * Canvas size and the X and Y domain. * The origin is in the bottom left corner. */ private static int scaleY ( double y) { return (int) ( ((maxDomY - y)/(maxDomY-minDomY))*canHeight ); } private static int scaleX ( double x) { return (int) ( ((x - minDomX)/(maxDomX-minDomX ))*canWidth ); } /** * Now do the inverse functions to convert a MouseClick to * a number relative to the current Canvas size and scale */ public double scaleMouseX( int x ) { return (double) ( ( (double) x /canWidth)*(maxDomX - minDomX) + minDomX ) ; } public double scaleMouseY( int y ) { return (double) ( maxDomY - ( (double) y /canHeight)*(maxDomY - minDomY) ); } private static void plotPoints(Graphics g, double x, double y, int iterations ) { double xtemp; // these next lines draw the axis on the canvas for orientation g.drawLine(scaleX(0), scaleY(maxDomY), scaleX(0), scaleY(minDomY) ); g.drawLine(scaleX(maxDomX), scaleY(0), scaleX(minDomX), scaleY(0) ); for (int i = 1; i <= iterations; i++) { xtemp = 1.0 - A*x*x + y; y = B*x; x = xtemp; g.drawLine(scaleX(x), scaleY(y), scaleX(x), scaleY(y)); } } public void paint(Graphics g) { ZoomHandler.setClickNumber( true ); // reset the mouse click variable setToCanvasSize(); //set private variables to suit window size Rectangle r = getBounds(); // bounds for this canvas g.setColor(Color.black); plotPoints(g, initX, initY, numPoints); g.setColor(Color.red); g.drawRect(0, 0, r.width - 1, r.height - 1); } }