hi there, i am doing a java course in college, and have been given a task to do regarding fractals. i have been given 2 classes, on of which i need to complete, but i am at a total loss as to how to do this please could anyone help me. here are the 2 classes i was given;
private final int MAX = 256; // max iterations private final double SX = -2.025; // start value real private final double SY = -1.125; // start value imaginary private final double EX = 0.6; // end value real private final double EY = 1.125; // end value imaginary
private static int y1, x1; // window size. The fractal will completely fill it. // smaller window=faster calculations but less resolution
private static double xstart, ystart, xend, yend, xzoom, yzoom; // dimensions of drawing area private static float xy=1.16666666666f; // aspect ratio
private Image img=null; private Graphics g1=null; // a drawing surface for the fractal image private Graphics g2=null; // a drawing surface for overlay text
private boolean fracReadyforDisplay=false;
// Constructor public Fractal(int winHeight) {
// flag used by paint methods so that they won't start drawing the // fractal while it is still being calculated fracReadyforDisplay=false;
// top bar text setTitle("Mandelbrot's 'apple man' fractal");
// set the window height (and subsequently the width) of the graphics window y1=winHeight;
// limit window size to be between 100 and 1000 pixels high if(y1<100) y1=100; if(y1>1000) y1=1000;
// calculate the graphic window width from the height so that we have a nice aspect ratio x1=(int)((float)y1*(float)xy); setSize(x1, y1+28); // +28: offsets window title bar
// show it on the desktop setVisible(true);
// create the drawing surface for the fractal img = createImage(x1, y1); g1 = img.getGraphics();
// drawing surface for overlay text output only g2= getGraphics();
// action to perform when closing the window addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { close(); } });
} // end of constructor
// clean-up and close the graphics window public void close() { img = null; // free resources g1 = null; // " g2=null; // " System.gc(); // garbage collector System.out.println("Closing"); // output for CourseMaster to check dispose(); // remove the window from the desktop }
// draw method called by the user, the 4 parameters define the dimensions // of the area to be shown in the graphics window public void draw(double xs, double ys, double xe, double ye ) { xstart = xs; ystart = ys; xend = xe; yend = ye;
// Figure out when the iteration goes to infinity (it will when 'm' is over 4) and set the colour value accordingly // MAX is the maximum amount of iterations allowed. required as some points NEVER reach infinity // The higher MAX the better the accuracy of the representation. 256 is a good compromise. // Color values returned range from 0.0 to 1.0 private float pointColour(double xValue, double yValue) { double r = 0.0, i = 0.0, m = 0.0; int j = 0;
while ((j < MAX) && (m < 4.0)) { j++; m = r * r - i * i; i = 2.0 * r * i + yValue; r = m + xValue; } return (float)j / (float)MAX; }
// calculate the fractal points private void calcFractal() { int x, y; float h, b, alt = 0.0f;
fracReadyforDisplay=false;
if((g2==null)||(g1==null)) return;
// draw a progress bar g2.setColor(Color.blue);
// hard-core calculations for (x = 0; x < x1; x+=2) {
g2.drawLine(x,y1+28-20, x,y1+28); // fill progress bar g2.drawLine(x+1,y1+28-20, x+1,y1+28); // fill progress bar
for (y = 0; y < y1; y++) { h = pointColour(xstart + xzoom * (double)x, ystart + yzoom * (double)y); // color value if (h != alt) { b = 1.0f - h * h; // brightnes g1.setColor(Color.getHSBColor(h, 0.8f, b)); alt = h; } g1.drawLine(x, y, x + 1, y); } // end of for(y=... } // end of for(x=...
// Write some info g1.setColor(Color.white); String buff="x1="+xstart+" x2="+xend; g1.drawString(buff,5,15); System.out.println("\nFractal window now at:\n"+buff);
// main method, just for testing the fractal and demonstrating its use public static void main( String[ ] args ) {
double x1=-2.025, y1=-1.125,x2=0.6,y2=1.125;
// construct a new fractal, size should be between 200 and 1000 // the smaller, the faster Fractal F=new Fractal(400);
// draw the fractal F.draw(x1, y1, x2, y2 );
} // end of main
} // end of class
Fractalcontrol.java
public class FractalControl {
// 4 constants, defining the default dimensions of the fractal window (static final doubles)
static final double X1 = 2.025; static final double Y1 = 1.125; static final double X2 = 0.6; static final double Y2 = 1.125;
public static void main (String[ ] args) {
// variable declarations here
// construct a new fractal, size between 100 and 1000 // Important: // for submission use size 100, // otherwise the marking process // will take a VERY long time ! Fractal myFrac=new Fractal(400);
// 'while' loop starts here, checking if user has entered an 'x' to exit the programme
// use the 'draw' method of the Fractal class to draw the fractal. // pass the 4 constants defined above into the draw methods in the order: // x1, (left edge pos), y1, (upper edge pos), x2 (right edge pos), y2 (lower edge pos) myFrac.draw(x1, y1, x2, y2);
// display the menu options 'L=left, 'R'=right, 'U'=up. 'D'=down '+'=zoom out, etc.
// read in the user's choice
// start of 'switch' statement (see 'Hints' in question)
// end of switch(in...
// end of while
// use the 'close' method of the Fractal class to close the fractal graphics window
} // end of main
} // end of class FractalControl
any help at all with this taks would be greatly appreciated. thanks Bob