Computer Science 252
Problem Solving with Java
Spring 2014, The College of Saint Rose
LivingFractal Demo
A working demo of LivingFractal will appear below. Click inside the applet to interact with it.
LivingFractal BlueJ Project
Click here to download a BlueJ project for LivingFractal.
LivingFractal Source Code
The Java source code for LivingFractal is below. Click on a file name to download it.
/**
* FractalApplet.java
* Kim Bruce 9/4/99. Last modified 11/4/99
* (c) 1999 Williams College
*
* Updated by Jim Teresco for CSC 252, The College of Saint Rose, Fall 2013
*
* $Id: LivingFractal.java 2244 2013-11-06 04:54:22Z terescoj $
*/
import objectdraw.*;
import java.awt.*;
// Applet to build fractal "star" picture
public class LivingFractal extends WindowController {
public static final double PI = 3.14159; // The number Pi
public static final int limit = 10; // Size at which stop pushing line into fractal
public static final double size = 200.0; // Size of lines in original triangle
public static final double startX = 100.0; // Starting coordinates of lower left corner
public static final double startY = 150.0; // of triangle
// Initializes the applet by creating the triangle with three "PushLine"s.
public void begin() {
PushLine line1 = new PushLine(new Location(startX,startY),size,0,limit,canvas);
Location dest = line1.getDest();
PushLine line2 = new PushLine(dest,size,4*PI/3,limit,canvas);
PushLine line3 = new PushLine(line2.getDest(),size,2*PI/3,limit,canvas);
}
}
/*
* AngLine.java Jonathan Kallay 7/14/99
* (c) 1999 Williams College
*/
import java.awt.*;
import objectdraw.*;
/**
* AngLine is an implementation of a drawable line object.
*
* @author Jonathan Kallay
* @version 1.0 last update 8/25/99
*
* Updated by Jim Teresco for CSC 252, The College of Saint Rose, Fall 2013
*
* $Id: AngLine.java 2244 2013-11-06 04:54:22Z terescoj $
*/
public class AngLine extends Line implements LineInterface {
private double length;
private double angle;
/**
* Creates a new AngLine object.
* @param start the starting point of the line.
* @param dest the destination point of the line.
* @param c the canvas in which the line is created.
*/
public AngLine(Location start, double length, double radianAngle, DrawingCanvas canvas){
super(start,new Location(start.getX() + length*Math.cos(radianAngle),
start.getY() - length*Math.sin(radianAngle)),canvas);
this.length = length;
angle = radianAngle;
}
public Location getDest() {
return new Location(start.getX() + length*Math.cos(angle),
start.getY() - length*Math.sin(angle));
}
}
/**
* Program to create a line which after a pause pushes out into a _/\_ shape
* (and then continues recursively pushing out).
* Written 11/4/99 by Kim Bruce
* Modified 4/00 by Andrea Danyluk
*
* Updated by Jim Teresco for CSC 252, The College of Saint Rose, Fall 2013
*
* $Id: PushLine.java 2244 2013-11-06 04:54:22Z terescoj $
*/
import java.awt.*;
import objectdraw.*;
public class PushLine extends ActiveObject implements LineInterface {
private static final int PAUSETIME = 400;
private Location start,dest; // Start and end Location of line
private double length, // length of line
radians, // angle of line with positive x-axis
limit; // size at which stop expanding line
private DrawingCanvas canvas; // canvas to be drawn on
private AngLine baseLine; // straight line which will be replaced by "pushed" line
/**
* Constructor for line which eventually pushes out into fractal
* @param start - starting coordinates for line
* @param length - length of original line
* @param radians - angle of line from the horizontal
* @param limit - if length < limit then don't push out into fractal
* @param canvas - where line will be drawn
*/
public PushLine(Location start, double length, double radians, double limit,
DrawingCanvas canvas) {
// create line which will later be replaced by "pushed" out figure
baseLine = new AngLine(start,length,radians,canvas);
this.start = start;
this.length = length;
this.radians = radians;
this.limit = limit;
this.canvas = canvas;
dest = baseLine.getDest();
start();
}
// After pause, start pushing line out into fractal pattern
public void run() {
// Lines to be pushed
LineInterface firstLine, secondLine, thirdLine, fourthLine;
Location secondPt, thirdPt, fourthPt;
pause(PAUSETIME); // Pause to let user see straight line before "pushing"
if (length < limit) { // Base case -- just make lines in _/\_ shape
firstLine = new AngLine(start,length/3,radians,canvas);
secondPt = firstLine.getDest();
secondLine = new AngLine(secondPt,length/3,radians+Math.PI/3,canvas);
thirdPt = secondLine.getDest();
thirdLine = new AngLine(thirdPt,length/3,radians-Math.PI/3,canvas);
fourthPt = thirdLine.getDest();
fourthLine = new AngLine(fourthPt,length/3,radians,canvas);
}
else { // Recursive case -- make _/\_ shape with PushLines
firstLine = new PushLine(start,length/3,radians,limit,canvas);
secondPt = firstLine.getDest();
secondLine = new PushLine(secondPt,length/3,radians+Math.PI/3,limit,canvas);
thirdPt = secondLine.getDest();
thirdLine = new PushLine(thirdPt,length/3,radians-Math.PI/3,limit,canvas);
fourthPt = thirdLine.getDest();
fourthLine = new PushLine(fourthPt,length/3,radians,limit,canvas);
}
baseLine.hide(); // Erase original straight line
}
// returns end point of line
public Location getDest() {
return dest;
}
}
/**
* interface for line which can calculate its end point.
* Written 11/4/99 by Kim Bruce
*
* Updated by Jim Teresco for CSC 252, The College of Saint Rose, Fall 2013
*
* $Id: LineInterface.java 2244 2013-11-06 04:54:22Z terescoj $
*/
import objectdraw.*;
public interface LineInterface {
// returns end point of line
public Location getDest();
}