User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 430,123 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,322 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1061 | Replies: 26
Reply
Join Date: May 2008
Posts: 44
Reputation: LevelSix is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Light Poster

Koch Snowflake

  #1  
Jul 20th, 2008
I need to write a program that draws the next iteration if a koch snowflake, when a button is clicked. It begins with a equilateral triangle. The program uses an ArrayList, Polygon, and GeneralPath, as I found these imports in the starter code. I think I may also be supposed to use recursion, but I'm unsure of where to do so, or how it would even help.

Thus far my code draws the initial triangle, and displays the button for drawing the enxt iteration. When clicked, I get an error message, stating that npoints > xpoints.length or ypoints.length. These are all constructors for the polygon, xpoints and ypoints are arrays of the x and y values, and npoints is the total number of points. As you can see my draw method adds the points to arraylists, and these lists are later converter to arrays, as the constructor for a polygon requires them to be. I believe my issue must be there. Either with the conversion, or the arraylist itself.

My Code:

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.awt.geom.GeneralPath;
import java.awt.Polygon;

public class KochComponent extends JComponent
{
   public KochComponent()
   {
      numIterations =  1;
      x = new ArrayList<Integer>();
      y = new ArrayList<Integer>();
   }
   
   public void next()
   {
      numIterations++;
      repaint();
   }

   public void paintComponent(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;

      int length = Math.min(getWidth(), getHeight()) * 2 / 3;

      int x1 = 10;
      int y1 = length / 2;
      int x2 = x1 + length;
      int y2 = y1;
      int x3 = x1 + (length / 2);
      int y3 = y1 + length;
      draw(g2, numIterations, x1, y1, x2, y2);
      draw(g2, numIterations, x2, y2, x3, y3);
      draw(g2, numIterations, x3, y3, x1, y1);
   }

   private void draw(Graphics2D g2, int iteration,
      double x1, double y1, double x2, double y2)
   {
     
     
      x.add((int)x1);
      x.add((int)x2);
      y.add((int)y1);
      y.add((int)y2);
      

      double n = 3 * Math.pow(4, iteration-1);
      vertex = (int)n;
      
     

      

   }
   public void paint(Graphics g)
   {
      paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;
      super.paint(g2);
      Object[] obj = x.toArray();
      Object[] obj2 = y.toArray();
      xPoints = new int[obj.length];
      yPoints = new int[obj2.length];
      for (int k = 0; k < obj.length; k++)
      {
          xPoints[k] = ((Integer)obj[k]).intValue();
      }
      for (int l = 0; l < obj2.length; l++)
      {
          yPoints[l] = ((Integer)obj2[l]).intValue();
      }
      Polygon koch = new Polygon(xPoints, yPoints, vertex);
      path = new GeneralPath(koch);
      path.moveTo(xPoints[0], yPoints[0]);
      for (int i = 1; i < xPoints.length; i++) 
      {
         path.lineTo(xPoints[i], yPoints[i]);
      }
      path.closePath();
       g2.draw(path);
       
    }
       

   private int numIterations;
   private ArrayList<Integer> x;
   private ArrayList<Integer> y;
   private GeneralPath path;
   private int vertex;
   private int[] xPoints;
   private int[] yPoints;
}

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class KochFrame extends JFrame
{
   public KochFrame()
   {
      setSize(FRAME_WIDTH, FRAME_HEIGHT);
      setTitle("KochViewer");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JPanel panel = new JPanel();
      panel.add(makeNextButton());

      component = new KochComponent();
      component.setPreferredSize(new Dimension(
            COMPONENT_WIDTH, COMPONENT_HEIGHT));
      panel.add(component);

      add(panel);
   }

   private JButton makeNextButton()
   {
      JButton button = new JButton("Next");
      class ButtonListener implements ActionListener
      {
         public void actionPerformed(ActionEvent event)
         {
            component.next();
         }
      }
      button.addActionListener(new ButtonListener());
      return button;
   }
   
   private static final int FRAME_WIDTH = 360;
   private static final int FRAME_HEIGHT  =  500;
 
   private static final int COMPONENT_WIDTH = 300;
   private static final int COMPONENT_HEIGHT = 400;

   private KochComponent component;
}

import javax.swing.JFrame;

public class KochViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new KochFrame();
      frame.setTitle("KochViewer");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Location: new york
Posts: 321
Reputation: sciwizeh is on a distinguished road 
Rep Power: 1
Solved Threads: 17
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Whiz

Re: Koch Snowflake

  #2  
Jul 20th, 2008
your vertex variable is holding a number too high, i am trying to figure out why
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote  
Join Date: Jun 2008
Location: new york
Posts: 321
Reputation: sciwizeh is on a distinguished road 
Rep Power: 1
Solved Threads: 17
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Whiz

Re: Koch Snowflake

  #3  
Jul 20th, 2008
vertex is actually the right number,

found it,

your trying to draw with the new number of points before making the points, put your paintComponent(g); at the end of the method
Last edited by sciwizeh : Jul 20th, 2008 at 8:54 pm.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote  
Join Date: Jun 2008
Location: new york
Posts: 321
Reputation: sciwizeh is on a distinguished road 
Rep Power: 1
Solved Threads: 17
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Whiz

Re: Koch Snowflake

  #4  
Jul 20th, 2008
ok... the problem is right, i think, but the solution causes the same problem
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote  
Join Date: May 2008
Posts: 44
Reputation: LevelSix is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Light Poster

Re: Koch Snowflake

  #5  
Jul 20th, 2008
Yeah, I think you're right. That is why I mentioned the arrays. Is there a simpler way to convert arraylists to an array?
Reply With Quote  
Join Date: Jun 2008
Location: new york
Posts: 321
Reputation: sciwizeh is on a distinguished road 
Rep Power: 1
Solved Threads: 17
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Whiz

Re: Koch Snowflake

  #6  
Jul 20th, 2008
well, i would re-arrange the two paint methods, to avoid using the arrays before making them.

i don't know whether it would make a difference to the returned array, but i would also try adding a call to trimToSize() call before toArray()

another thing you could do to avoid the casting to array type, you could look into the toArray(T[] a) method, but that isn't necessary
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote  
Join Date: May 2008
Posts: 44
Reputation: LevelSix is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Light Poster

Re: Koch Snowflake

  #7  
Jul 22nd, 2008
I placed int variables in the draw method, and the paint method that are derived from the size of arrylist x. After draw is done being called, the size is 6. After the program moves on to paint, the size is 0. This is where the problem lies.
Reply With Quote  
Join Date: Jun 2008
Location: new york
Posts: 321
Reputation: sciwizeh is on a distinguished road 
Rep Power: 1
Solved Threads: 17
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Whiz

Re: Koch Snowflake

  #8  
Jul 22nd, 2008
that's weird.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote  
Join Date: May 2008
Posts: 44
Reputation: LevelSix is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Light Poster

Re: Koch Snowflake

  #9  
Jul 22nd, 2008
Very. I can't for the life of me see why it does this.
Reply With Quote  
Join Date: Jun 2008
Location: WA, USA
Posts: 801
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Rep Power: 4
Solved Threads: 78
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Practically a Posting Shark

Re: Koch Snowflake

  #10  
Jul 23rd, 2008
What algorithm is necessary to generate the points for the Koch Snowflake?

Are you omitting lines in the KochSnowflake?

Also, by the way I would not rely on the paint method or paintComponent method to have code in it without some kind of flag.

You could put a global-scoped boolean data type in your program and place an if statement that uses that boolean and code your graphics within it.

The reason for this? Everytime the window is resized your paint method will be called, along with paintComponent for JComponent.

As long as you understand the algorithm for generating this Koch Snowflake, you can code around the problem or start from scratch without much of a hassle, but I will try to look for the problem.

Sciwizeh was correct about the vertex getting more points than the existing points in the arrays. It may have been due to the JComponent being painted before you were capable of clicking the button (which I believe was stated before in a slightly different way).

Having some kind of flag may save you some trouble, but I'm not sure how much trouble it'll save you since your code mainly depends on the algorithm that produces the snowflake.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 3:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC