//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //A Simple Fractal //Description: Draws a Sierpinski Triangle with a square for a starting shape. //CS 284 //Programmed by Jonathan Voris //for 1/24/06 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //for graphics usage import java.awt.*; //for keyboard input import java.util.Scanner; //the fractal class extends Frame to use its window calls class fractal extends Frame { private int windowSize = 700; private int numSteps = 1; public static void main(String[] args) { //call fractal's constructor fractal fract = new fractal(); //start the fractal drawing process fract.run(); } //fractal constructor creates a window by calling Frame methods public fractal() { //window title setTitle("Sierpinski Triangle"); //window location setLocation(0, 0); //window size setSize(windowSize, windowSize); //background color of the window setBackground(Color.white); //make the window appear setVisible(true); } //run performs the fractal's drawing process private void run() { //create a new scanner for reading keyboard input Scanner sc = new Scanner(System.in); //loop continuously while (true) { //input the number of steps of the fractal to draw System.out.print("Please input the number of steps to use:"); numSteps = sc.nextInt(); System.out.println(); // ask window system to call paint() to draw fractal with // the new number of steps repaint(); } } public void paint(Graphics g) { g.setColor(Color.black); //initial call to drawFract - start with g, which is necessary for graphics output, //the inputted number of steps, and the desired overall fractal size. Using //0, 0, windowSize will create a fractal that takes up the whole graphics window. drawFract(g, numSteps, 0, 0, windowSize); } //drawFract accepts a graphics object, the number of times of detail to repeat in the fractal //pattern, square coordinates and a square size as input. It recursively calls itself, //further subdividing the fractal each time. This continues until the desired level of detail //has been exhausted, at which point a properly sized shape is drawn. private void drawFract(Graphics g, int numSteps, int squareX, int squareY, double squareSize) { //Base case - if the function is called with 1 for numSteps, no further level of detail //is needed. if (numSteps == 1) { //Draw a rectangle with the given coordinates and size. g.fillRect(squareX, squareY, (int)squareSize, (int)squareSize); //(I left the following line commented out since the starting shape used does not //affect the shape produced by the fractal.) //g.fillOval(squareX, squareY, (int)squareSize, (int)squareSize); } //Recursion - if more steps of detail are required, then recursively call drawFact //three times - one for each subobject to be drawn else { //draw a fractal in the top-middle of the current fractal space drawFract(g, numSteps-1, squareX + (int)squareSize/4, squareY + 0, squareSize/2); //draw a fractal in the bottom-left of the current fractal space drawFract(g, numSteps-1, squareX + 0, squareY + (int)squareSize/2, squareSize/2); //draw a fractal in the bottom-right of the current fractal space drawFract(g, numSteps-1, squareX + (int)squareSize/2, squareY + (int)squareSize/2, squareSize/2); } } }