The Artima Developer Community
Sponsored Link

Java Answers Forum
fractals

1 reply on 1 page. Most recent reply: Feb 19, 2007 9:46 AM by A B

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
bob

Posts: 1
Nickname: bob20gsi
Registered: Feb, 2005

fractals Posted: Feb 10, 2005 3:56 AM
Reply to this message Reply
Advertisement
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;

fractals.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class Fractal extends Frame {

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;

// adjust dimensions
if ((float)((xend - xstart) / (yend - ystart)) != xy )
xstart = xend - (yend - ystart) * (double)xy;
xzoom = (xend - xstart) / (double)x1;
yzoom = (yend - ystart) / (double)y1;

// claculate
calcFractal( );

// draw the result of the calculation
if((g2!=null)&&(fracReadyforDisplay))
g2.drawImage(img, 0, 28, this);

} // end of draw method


// override paint method so that window movements, etc trigger a proper
// re-paint of the image
public void paint (Graphics g) {

if((g!=null)&&(fracReadyforDisplay))
g.drawImage(img, 0, 28, this);

} // end of paint


// 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);

buff="y1="+ystart+" y2="+yend;
g1.drawString(buff,5,30);
System.out.println(buff);

buff="Magnifica tion="+(2.625/(xend-xstart));
g1.drawString(buff,5,45);
System.out.println(buff) ;

fracReadyforDisplay=true;

} // end of calcFractal




// 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


A B

Posts: 1
Nickname: bigbeck
Registered: Feb, 2007

Re: fractals Posted: Feb 19, 2007 9:46 AM
Reply to this message Reply
Hey, I am also doing a course in programming and i have the same assignment to do. Exactly the same question. Maybe we could help each other?

Flat View: This topic has 1 reply on 1 page
Topic: fractals Previous Topic   Next Topic Topic: NEED SERIOUS HELP NOW!!! I'm too NEW at this

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use