# Programming Koch's Snowflake in Java

• 01-11-2009, 11:51 PM
sev51
Programming Koch's Snowflake in Java
I am in need of the starting points of how to graphically display this fractal using java... something like pseudo-Java code with steps and the formulas required to create Koch's Snowflake ... any help would be greatly appreciated! I am fairly new to Java and am using BlueJ as a compiler.
• 01-12-2009, 12:48 AM
Fubarable
You will learn far more if you try to make an attempt at solving this yourself first, and then come back here if you hit a road block.

There are several key issues involved here, one is how to draw using Java graphics, another being how to create a recursive method since Koch's snowflake is often used as an example of a problem that can be solved by recursion (but can also be solved by non-recursive techniques).

Best of luck!
• 01-12-2009, 01:07 AM
sev51
That's the problem.. i wish i knew where to start but i'm entirely too lost .. all i have is this (most likely useless) code:

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
//
// public class KSnowflake
// {
// public static void main (String[] args)
// {
// JFrame j = new JFrame ("KSnowflake..."); // create a new window
// j.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // stop program when we close the window
// // add a panel to the center of the content pane of the window
// j.getContentPane ().add (new KSnowflakePanel (), BorderLayout.CENTER);
// j.setSize (600, 600); // set size of the window
// j.setVisible (true); // make the window visible
// }
// }

public class KSnowflakeApplet extends JApplet implements MouseListener
{
// declare variables
int x1, x2, x3;
int y1, y2, y3;
int level;

// Constructor (setup)
public void init ()
{
setBackground(Color.black);

level =1;

x1 = 10;
x2 = 160;
x3 = 310;

y1 = 270;
y2 = 10;
y3 = 270;

}

public void paint (Graphics g)
{
g.setColor(Color.white);

Flake(level,x1,y1,x2,y2,g);

requestFocus ();
}

public void Flake(int level, int x1, int y1, int x5, int y5, Graphics g)
{
if(level == 1)
{
g.drawLine(x1,y1,x2,y2);
g.drawLine(x2,y2,x3,y3);
g.drawLine(x3,y3,x1,y1);
}
else
{

// recursion????

}
}

// To fulfill our obligations as a MouseListener, we implement the following...
public void mouseClicked (MouseEvent e)
{

repaint ();
}

public void mouseReleased (MouseEvent e)
{
}

public void mousePressed (MouseEvent e)
{
}

public void mouseEntered (MouseEvent e)
{
}

public void mouseExited (MouseEvent e)
{
}

}

I wish to program it recursively, and i thought that it wouldn't be such a challenge but after reading about Koch's Snowflake on a couple of websites i became so discouraged and confused.. Recursive methods as it is confuse me since my teacher never explained the concept and did not provide us with examples as to how to use the recursive method in various math calculations in java, like for instance recursively producing the Fibonacci sequence ... i have hit a bump and i haven't even started . ok here's a more detailed question: if the level of the snowflake is NOT 1, say 2, how do i proceed to produce this recursively?
• 01-12-2009, 05:18 AM
Fubarable
Quote:

That's the problem.. i wish i knew where to start but i'm entirely too lost
You'll want to talk to your teacher then and set up some one-on-one time. It will offer your best chance for success here.
• 01-12-2009, 05:49 AM
Fubarable
Quote:

if the level of the snowflake is NOT 1, say 2, how do i proceed to produce this recursively?
You would likely have a method that accepted two Point parameters, point0 and point1, and a generation number. It would use the points to calculate the 3 new points for the next level of the snowflake. It would increment the generation number. If this number is below some cut off it would call the same method 4 times with point pairs obtained from the old and new points, and with the new generation number. If the generation number is at or above a cut off, then the method would draw the line segments and would not recurs (the stopping condition).