943,670 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8443
  • Java RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Aug 13th, 2008
0

Re: Koch Snowflake

Hmm, I don't see how rotate would be applied, unless you rotate polygon.... Well, there's an idea.
Reputation Points: 22
Solved Threads: 3
Junior Poster in Training
LevelSix is offline Offline
59 posts
since May 2008
Aug 13th, 2008
0

Re: Koch Snowflake

An idea that failed miserably. I can't seem to get the rotate(double) method to work at all. It tells me it can't find the method. I thought it was part of the Graphics2D package?

I'm probably implementing it wrong though, as I'm trying to do koch.rotate(theta);. Theta being the angle of course.
Last edited by LevelSix; Aug 13th, 2008 at 7:15 pm.
Reputation Points: 22
Solved Threads: 3
Junior Poster in Training
LevelSix is offline Offline
59 posts
since May 2008
Aug 13th, 2008
0

Re: Koch Snowflake

Given a Graphics2D context reference of "g2", you just call g2.rotate(theta) to rotate about the origin theta radians.
(Keeping in mind that the transformations are cumulative - the rotation and translation transforms are concatenated with the current transform on each call)
Last edited by Ezzaral; Aug 13th, 2008 at 8:00 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Aug 13th, 2008
0

Re: Koch Snowflake

Adding Grapics2D to the getKochPoints method would seem to cause numerous problems however. Then the calling of the method would have to be changed as well, and that method doesn't support graphics content either.
Reputation Points: 22
Solved Threads: 3
Junior Poster in Training
LevelSix is offline Offline
59 posts
since May 2008
Aug 14th, 2008
0

Re: Koch Snowflake

As I'm not sure how to implement the rotate method, I went back to try to get the calculations right. Unfortunately, I have not got it correct. My updated code:

Java Syntax (Toggle Plain Text)
  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. import java.awt.Point;
  9.  
  10. public class KochComponent3 extends JComponent
  11. {
  12.  
  13. ArrayList<Point> points = null;
  14.  
  15. /** create the initial triangle */
  16. private void createTriangle()
  17. {
  18. points = new ArrayList<Point>();
  19.  
  20. int length = Math.min(getWidth(), getHeight())*2/3;
  21.  
  22. int x1 = (getWidth()-length)/2;
  23. int y1 = length/2;
  24. points.add(new Point(x1, y1));
  25.  
  26. int x2 = x1+length;
  27. int y2 = y1;
  28. points.add(new Point(x2, y2));
  29.  
  30. int x3 = x1+(length/2);
  31. int y3 = y1+length;
  32. points.add(new Point(x3, y3));
  33.  
  34. }
  35.  
  36. public void next()
  37. {
  38. numIterations++;
  39.  
  40. ArrayList<Point> newPoints = new ArrayList<Point>();
  41. // generate the new points for each line segment
  42. for(int i = 0; i<points.size(); i++) {
  43. Point p1 = points.get(i);
  44. Point p2 = i==points.size()-1 ?
  45. points.get(0) : points.get(i+1);
  46. newPoints.add(p1);
  47. newPoints.addAll(getKochPoints(p1, p2));
  48.  
  49. }
  50. points = newPoints;
  51. xAngle += 30;
  52. yAngle += 30;
  53. repaint();
  54. }
  55.  
  56. public void paintComponent(Graphics g)
  57. {
  58. if(points==null)
  59. {
  60. createTriangle();
  61. }
  62.  
  63. Graphics2D g2 = (Graphics2D)g;
  64. Polygon koch = new Polygon();
  65. for (Point p : points){
  66. koch.addPoint(p.x, p.y);
  67. }
  68. g2.draw(koch);
  69.  
  70. }
  71.  
  72.  
  73.  
  74. /** Generate the points to add for each segment
  75.   * for each iteration.
  76.   * I am just putting in the easy ones here.
  77.   * You get to do the trickier one!
  78.   */
  79. private ArrayList<Point> getKochPoints(Point p1, Point p2)
  80. {
  81. ArrayList<Point> kochPoints = new ArrayList<Point>();
  82.  
  83. int dx = p2.x-p1.x;
  84. int dy = p2.y-p1.y;
  85.  
  86.  
  87. Point kp1 = new Point(p1.x+dx/3, p1.y+dy/3);
  88. kochPoints.add(kp1);
  89.  
  90.  
  91.  
  92.  
  93. if(p1.y == p2.y)
  94. {
  95. Point kp2 = new Point();
  96. kp2.setLocation(kp1);
  97. kp2.translate((int)((dx/3)/2), (int)(-dx/3));
  98. kochPoints.add(kp2);
  99. }
  100. else if(p1.x > p1.y)
  101. {
  102.  
  103.  
  104. double v1 = Math.toRadians(30);
  105. double v2 = Math.toRadians(60);
  106. Point kp2 = new Point();
  107. kp2.setLocation(kp1);
  108. kp2.translate((int)(-Math.cos(v1)*dx/3), (int)(-Math.sin(v2)*2*dx/3 + dx/6));
  109. kochPoints.add(kp2);
  110.  
  111. }
  112. else
  113. {
  114. double v1 = Math.toRadians(xAngle);
  115. double v2 = Math.toRadians(yAngle);
  116. Point kp2 = new Point();
  117. kp2.setLocation(kp1);
  118. kp2.translate((int)(Math.cos(v2)*dx + dx/6), (int)(Math.sin(v1)*dx/3));
  119. kochPoints.add(kp2);
  120. }
  121.  
  122. Point kp3 = new Point(p1.x+2*dx/3, p1.y+2*dy/3);
  123. kochPoints.add(kp3);
  124.  
  125.  
  126. return kochPoints;
  127. }
  128. private int xAngle = 30;
  129. private int yAngle = 60;
  130. private int numIterations;
  131.  
  132. }
Reputation Points: 22
Solved Threads: 3
Junior Poster in Training
LevelSix is offline Offline
59 posts
since May 2008
Aug 14th, 2008
0

Re: Koch Snowflake

Would recursion make this problem any easier?
Reputation Points: 22
Solved Threads: 3
Junior Poster in Training
LevelSix is offline Offline
59 posts
since May 2008
Feb 25th, 2010
0
Re: Koch Snowflake
Is a fractal?
Click to Expand / Collapse  Quote originally posted by LevelSix ...
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:

Java Syntax (Toggle Plain Text)
  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. }

Java Syntax (Toggle Plain Text)
  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. }

Java Syntax (Toggle Plain Text)
  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. }
Yes a fractal.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Aarex is offline Offline
2 posts
since Feb 2010
Feb 25th, 2010
0
Re: Koch Snowflake
Java Syntax (Toggle Plain Text)
  1. import java.awt.Graphics;
Last edited by Aarex; Feb 25th, 2010 at 1:54 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Aarex is offline Offline
2 posts
since Feb 2010
Feb 25th, 2010
0
Re: Koch Snowflake
What is wrong with you? This thread is over two years old. And you added nothing to it.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Problems creating a Web Service Implementation
Next Thread in Java Forum Timeline: Decryption of PNG image for secret txt





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC