Results 1 to 5 of 5
  1. #1
    sev51 is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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).

    If you do come back, your chances of getting helpful help increase with the specificity of your question. Asking broad overly general questions will get answers, well, like this one, while asking very specific questions about say an exception your code is generating will usually result in a very specific and helpful answer.

    Best of luck!

  3. #3
    sev51 is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    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;


    addMouseListener (this);
    }

    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?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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).

Similar Threads

  1. Java Programming in Netbeans
    By cserenop51 in forum NetBeans
    Replies: 5
    Last Post: 11-05-2008, 04:21 AM
  2. interface C programming with Java
    By kapil1089theking in forum AWT / Swing
    Replies: 8
    Last Post: 09-11-2008, 06:09 AM
  3. java game programming
    By gammaman in forum New To Java
    Replies: 1
    Last Post: 12-25-2007, 02:01 AM
  4. Java networking programming (I)
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 12-24-2007, 08:21 PM
  5. Java Programming
    By JavaForums in forum Java Tutorial
    Replies: 0
    Last Post: 07-29-2007, 12:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •