Koch Snowflake

Reply

Join Date: May 2008
Posts: 55
Reputation: LevelSix is an unknown quantity at this point 
Solved Threads: 3
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Junior Poster in Training

Koch Snowflake

 
0
  #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:

  1. import java.awt.Graphics;
  2. import java.awt.Graphics2D;
  3. import javax.swing.JComponent;
  4. import java.awt.geom.Line2D;
  5. import java.util.ArrayList;
  6. import java.awt.geom.GeneralPath;
  7. import java.awt.Polygon;
  8.  
  9. public class KochComponent extends JComponent
  10. {
  11. public KochComponent()
  12. {
  13. numIterations = 1;
  14. x = new ArrayList<Integer>();
  15. y = new ArrayList<Integer>();
  16. }
  17.  
  18. public void next()
  19. {
  20. numIterations++;
  21. repaint();
  22. }
  23.  
  24. public void paintComponent(Graphics g)
  25. {
  26. Graphics2D g2 = (Graphics2D) g;
  27.  
  28. int length = Math.min(getWidth(), getHeight()) * 2 / 3;
  29.  
  30. int x1 = 10;
  31. int y1 = length / 2;
  32. int x2 = x1 + length;
  33. int y2 = y1;
  34. int x3 = x1 + (length / 2);
  35. int y3 = y1 + length;
  36. draw(g2, numIterations, x1, y1, x2, y2);
  37. draw(g2, numIterations, x2, y2, x3, y3);
  38. draw(g2, numIterations, x3, y3, x1, y1);
  39. }
  40.  
  41. private void draw(Graphics2D g2, int iteration,
  42. double x1, double y1, double x2, double y2)
  43. {
  44.  
  45.  
  46. x.add((int)x1);
  47. x.add((int)x2);
  48. y.add((int)y1);
  49. y.add((int)y2);
  50.  
  51.  
  52. double n = 3 * Math.pow(4, iteration-1);
  53. vertex = (int)n;
  54.  
  55.  
  56.  
  57.  
  58.  
  59. }
  60. public void paint(Graphics g)
  61. {
  62. paintComponent(g);
  63. Graphics2D g2 = (Graphics2D)g;
  64. super.paint(g2);
  65. Object[] obj = x.toArray();
  66. Object[] obj2 = y.toArray();
  67. xPoints = new int[obj.length];
  68. yPoints = new int[obj2.length];
  69. for (int k = 0; k < obj.length; k++)
  70. {
  71. xPoints[k] = ((Integer)obj[k]).intValue();
  72. }
  73. for (int l = 0; l < obj2.length; l++)
  74. {
  75. yPoints[l] = ((Integer)obj2[l]).intValue();
  76. }
  77. Polygon koch = new Polygon(xPoints, yPoints, vertex);
  78. path = new GeneralPath(koch);
  79. path.moveTo(xPoints[0], yPoints[0]);
  80. for (int i = 1; i < xPoints.length; i++)
  81. {
  82. path.lineTo(xPoints[i], yPoints[i]);
  83. }
  84. path.closePath();
  85. g2.draw(path);
  86.  
  87. }
  88.  
  89.  
  90. private int numIterations;
  91. private ArrayList<Integer> x;
  92. private ArrayList<Integer> y;
  93. private GeneralPath path;
  94. private int vertex;
  95. private int[] xPoints;
  96. private int[] yPoints;
  97. }

  1. import java.awt.Dimension;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7.  
  8. public class KochFrame extends JFrame
  9. {
  10. public KochFrame()
  11. {
  12. setSize(FRAME_WIDTH, FRAME_HEIGHT);
  13. setTitle("KochViewer");
  14. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.  
  16. JPanel panel = new JPanel();
  17. panel.add(makeNextButton());
  18.  
  19. component = new KochComponent();
  20. component.setPreferredSize(new Dimension(
  21. COMPONENT_WIDTH, COMPONENT_HEIGHT));
  22. panel.add(component);
  23.  
  24. add(panel);
  25. }
  26.  
  27. private JButton makeNextButton()
  28. {
  29. JButton button = new JButton("Next");
  30. class ButtonListener implements ActionListener
  31. {
  32. public void actionPerformed(ActionEvent event)
  33. {
  34. component.next();
  35. }
  36. }
  37. button.addActionListener(new ButtonListener());
  38. return button;
  39. }
  40.  
  41. private static final int FRAME_WIDTH = 360;
  42. private static final int FRAME_HEIGHT = 500;
  43.  
  44. private static final int COMPONENT_WIDTH = 300;
  45. private static final int COMPONENT_HEIGHT = 400;
  46.  
  47. private KochComponent component;
  48. }

  1. import javax.swing.JFrame;
  2.  
  3. public class KochViewer
  4. {
  5. public static void main(String[] args)
  6. {
  7. JFrame frame = new KochFrame();
  8. frame.setTitle("KochViewer");
  9. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10. frame.setVisible(true);
  11. }
  12. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 412
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Koch Snowflake

 
0
  #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 Quick reply to this message  
Join Date: Jun 2008
Posts: 412
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Koch Snowflake

 
0
  #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 9: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 Quick reply to this message  
Join Date: Jun 2008
Posts: 412
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Koch Snowflake

 
0
  #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 Quick reply to this message  
Join Date: May 2008
Posts: 55
Reputation: LevelSix is an unknown quantity at this point 
Solved Threads: 3
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Junior Poster in Training

Re: Koch Snowflake

 
0
  #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 Quick reply to this message  
Join Date: Jun 2008
Posts: 412
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Koch Snowflake

 
0
  #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 Quick reply to this message  
Join Date: May 2008
Posts: 55
Reputation: LevelSix is an unknown quantity at this point 
Solved Threads: 3
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Junior Poster in Training

Re: Koch Snowflake

 
0
  #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 Quick reply to this message  
Join Date: Jun 2008
Posts: 412
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Koch Snowflake

 
0
  #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 Quick reply to this message  
Join Date: May 2008
Posts: 55
Reputation: LevelSix is an unknown quantity at this point 
Solved Threads: 3
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Junior Poster in Training

Re: Koch Snowflake

 
0
  #9
Jul 22nd, 2008
Very. I can't for the life of me see why it does this.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
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 Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Koch Snowflake

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC