My Original program was to.......
Write a Java applet that uses recursion to draw a Koch snowflake fractal
of any given order. A Koch snowflake of order 0 is an equilateral
triangle. To create the next-higher-order fractal, each line segment in
the shape is modified by replacing the middle third with a sharp
protrusion made of two line segments, each having the same length as the
replaced one......
and here is my code for this assignment.....
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.TextField;
public class KochSnowFlake
extends java.applet.Applet
implements java.awt.event.ActionListener
{
private final static int DEPTH_DEFAULT = 0;
private final static double cos60 = 0.5;
private final static double sin60 = Math.sqrt(3)*0.5;
private final static double value2root3over3 = Math.sqrt(3)*2/3;
private int curDepth = DEPTH_DEFAULT;
private boolean doLines = true;
private final Button bDraw = new Button("Click to Draw");
private final TextField tfIterations = new TextField("" +
DEPTH_DEFAULT,3);
private boolean infoShown = false;
private final Panel pGfx =
new Panel() {
public void paint(Graphics g) {
drawPicture(g,new Rectangle(this.getSize()));
}
};
public void init() {
Panel pControls = new Panel();
pControls.setLayout(new FlowLayout());
pControls.add(bDraw);
pControls.add(new Panel());
pControls.add(tfIterations);
pControls.add(new Label("How Many Iterations?"));
setLayout(new BorderLayout());
add(pGfx,BorderLayout.CENTER);
add(pControls,BorderLayout.SOUTH);
bDraw.addActionListener(this);
}
public void drawSegment(Graphics g, Point a, Point b, int depth) {
if(depth==0) {
if(doLines)
g.drawLine(a.x,a.y, b.x,b.y);
}
else {
final Point distance = new Point( (b.x-a.x)/3, (b.y-a.y)/3 );
final Point pa = new Point( a.x+distance.x, a.y+distance.y);
final Point pb = new Point( b.x-distance.x, b.y-distance.y);
final Point pTip = new Point(
pa.x + (int)(distance.x*cos60 + distance.y*sin60),
pa.y + (int)(distance.y*cos60 - distance.x*sin60)
);
final int newDepth = depth-1;
drawSegment(g, a,pa ,newDepth);
drawSegment(g, pa,pTip ,newDepth);
drawSegment(g, pTip,pb ,newDepth);
drawSegment(g, pb,b ,newDepth);
}
}
public void drawPicture(Graphics g, Rectangle bounds) {
g.setColor(Color.white);
g.fillRect(bounds.x,bounds.y, bounds.width,bounds.height);
if(false) {
g.setColor(Color.blue);
g.drawRect(bounds.x,bounds.y, bounds.width-1,bounds.height-1);
}
if( (bounds.width<2) || (bounds.height<2) )
return;
if(!doLines)
return;
final double w,h;
if(bounds.height < (int)(bounds.width * value2root3over3)) {
h = bounds.height;
w = h / value2root3over3;
}
else {
w = bounds.width;
h = w * value2root3over3;
}
final int top = bounds.y + (int)((bounds.height-h)*0.5 + (h*0.25));
final Point p1 = new Point(bounds.x +
(int)((bounds.width-w)*0.5), top);
final Point p2 = new Point(bounds.x +
(int)((bounds.width+w)*0.5), top);
final Point p3 = new Point(
bounds.x + (bounds.width>>1),
bounds.y + (int)((bounds.height+h)*0.5)
);
g.setColor(Color.black);
drawSegment(g, p1,p2, curDepth);
drawSegment(g, p2,p3, curDepth);
drawSegment(g, p3,p1, curDepth);
}
public void actionPerformed(java.awt.event.ActionEvent ae) {
java.lang.Object o = ae.getSource();
try {
curDepth = Integer.parseInt(tfIterations.getText());
}
catch(NumberFormatException e) {
curDepth = DEPTH_DEFAULT;
}
if(curDepth < 0)
curDepth = 0;
drawPicture(pGfx.getGraphics(),new Rectangle(pGfx.getSize()));
}
}
.....my code for that part of the assignment was correct it worked perfectly.....
now i have to fix this program or change it rather to implement a
1-dimensional 90 degree angle variant where the Koch(0) will be a
square....
koch(1) will be a straight line and so on...
I have no idea what to change...i know i have to make it draw 4 line segments rather that just 3 and change some math but i don't know how or where to do that within my assignment...
PLEASE HELP!
THANK YOU!!