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 455,964 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,607 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: 801 | Replies: 7 | Solved
Reply
Join Date: Nov 2007
Posts: 13
Reputation: HeroOfTime is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
HeroOfTime HeroOfTime is offline Offline
Newbie Poster

Question Fractals and the Chaos Game help.

  #1  
Nov 16th, 2007
Hi, I am a first Year Computer Science student learning java. First, what I want help with is not a homework assignment, I just started it on my own. This program is supposed to test the "Chaos Game" The chaos game is explained here: Chaos Game

So, the basics that I know I need to do are to find get a random point inside the triangle, connect it to a random vertex, find the midpoint, and then color in that dot. Some problems I had were filling in a single space, and for some reason, the line that I use instead of a dot is not being put inside the triangle. Here is my code.

  1. /**
  2.  * @(#)FractalStart.java
  3.  *
  4.  *
  5.  * @author
  6.  * @version 1.00 2007/11/15
  7.  */
  8.  
  9. import javax.swing.JFrame;
  10.  
  11. public class FractalStart
  12. {
  13.  
  14. public static void main(String[] args)
  15. {
  16. JFrame frame = new JFrame ("Fractal");
  17. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.  
  19. TriangleButton panel = new TriangleButton();
  20.  
  21. frame.getContentPane().add(panel);
  22. frame.pack();
  23. frame.setVisible(true);
  24. }
  25. }
  26.  
  27.  
  28.  
  29. //Draws some triangles,
  30. //Does some Fractal stuff with it.
  31. //11-02-07
  32.  
  33. import javax.swing.*;
  34. import java.awt.*;
  35. import java.awt.event.*;
  36. import java.util.Random;
  37. import java.util.ArrayList;
  38.  
  39.  
  40. public class Fractal extends JPanel
  41. {
  42. private int[] triangle01 = {150, 300, 150};
  43. private int[] triangle02 = {300, 300, 150};
  44.  
  45. Random gen = new Random();
  46. int point1 = gen.nextInt(300);
  47. int point2 = gen.nextInt(300);
  48. int side = 3;
  49. int point3;
  50. int point4;
  51. int midPoint1;
  52. int midPoint2;
  53. int point01;
  54. int point02;
  55. int[] dot1;
  56. int[] dot2;
  57. ArrayList<Integer> dots = new ArrayList<Integer>(); //holds all the points that are made in this program
  58.  
  59. Polygon triangle = new Polygon(triangle01, triangle02, triangle01.length);
  60.  
  61. public Fractal()
  62. {
  63. addKeyListener (new DirectionListener());
  64. setFocusable(true);
  65. setBackground (Color.black);
  66. setPreferredSize (new Dimension (1000,1000));
  67. }
  68.  
  69.  
  70. public void paintComponent (Graphics page)
  71. {
  72. //This should be drawing each part dot through the loop
  73. super.paintComponent (page);
  74. page.setColor(Color.black);
  75. for (int count = 0; count < dots.size(); count++)
  76. {
  77. int Npoint = dots.get(count);
  78. count++;
  79. int Npoint1 = dots.get(count);
  80. int[] joe = {Npoint, Npoint1};
  81. int[] joe1 = {Npoint + 1, Npoint1 + 1};
  82. page.setColor(Color.red);
  83. page.drawPolyline(joe,joe1,joe.length);
  84. }
  85. page.setColor(Color.white);
  86. page.drawPolygon(triangle);
  87. }
  88.  
  89. public void fractal1()
  90. {
  91. //Makes two random points inside the triangle
  92. //and then find the midpoint between that and a random side
  93. do
  94. {
  95. point1 = gen.nextInt(500); //point inside triangle
  96. point2 = gen.nextInt(500);
  97.  
  98. side = gen.nextInt(2);
  99.  
  100. }while (triangle.contains(point1,point2));
  101.  
  102.  
  103. point3 = triangle01[side]; //x point
  104. point4 = triangle02[side]; //y point of veritice
  105.  
  106. Integer midPoint1 = (point1 + point3) / 2;
  107. Integer midPoint2 = (point2 + point4) / 2;
  108.  
  109. dots.add(midPoint1); //adds the points to the dot.
  110. dots.add(midPoint2);
  111. }
  112.  
  113.  
  114.  
  115. private class DirectionListener implements KeyListener
  116. {
  117. public void keyPressed (KeyEvent event)
  118. {
  119. switch (event.getKeyCode())
  120. {
  121. case KeyEvent.VK_DOWN:
  122. fractal1();
  123. break;
  124. }
  125. repaint();
  126. }
  127. public void keyTyped (KeyEvent event) {}
  128. public void keyReleased (KeyEvent event) {}
  129. }
  130. }

The problem is that the dots aren't being put inside the triangle, only on the right line of it.... Why is this? What's a good solution? I've been debugging for a while and it still escapes me what is going on.

Thank you for your help! Oh, and tell me anything that might make it better, but simple, cause this is my first year.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,076
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 306
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Fractals and the Chaos Game help.

  #2  
Nov 16th, 2007
Try this as a starting point
  1. import java.awt.geom.Rectangle2D;
  2. import javax.swing.JFrame;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.util.Random;
  7. import java.util.ArrayList;
  8.  
  9.  
  10. public class Fractal extends JPanel
  11. {
  12. private int[] triangle01 = {150, 300, 150};
  13. private int[] triangle02 = {300, 300, 150};
  14.  
  15. Random gen = new Random();
  16. ArrayList<Point> dots = new ArrayList<Point>(); //holds all the points that are made in this program
  17.  
  18. Polygon triangle = new Polygon(triangle01, triangle02, triangle01.length);
  19.  
  20. public Fractal ()
  21. {
  22. addKeyListener (new DirectionListener());
  23. setFocusable(true);
  24. setBackground (Color.black);
  25. setPreferredSize (new Dimension (1000,1000));
  26. }
  27.  
  28.  
  29. public static void main(String[] args)
  30. {
  31. JFrame frame = new JFrame ("Fractal");
  32. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33.  
  34. Fractal panel = new Fractal();
  35.  
  36. frame.getContentPane().add(panel);
  37. frame.pack();
  38. frame.setVisible(true);
  39. }
  40.  
  41. public void paintComponent (Graphics page)
  42. {
  43. Graphics2D g2d = (Graphics2D)page;
  44. g2d.setBackground(Color.black);
  45. g2d.clearRect(0,0,getWidth(), getHeight());
  46. g2d.setColor(Color.red);
  47. for (Point p : dots){
  48. g2d.fillRect(p.x,p.y,1,1);
  49. }
  50. g2d.setColor(Color.white);
  51. g2d.drawPolygon(triangle);
  52. }
  53.  
  54.  
  55. public void createFractalPoint(Polygon poly){
  56. Point inner = getInnerPoint(poly);
  57. Point vertex = getVertex(poly);
  58. dots.add( getMidpoint(inner, vertex) );
  59. }
  60.  
  61. public Point getInnerPoint(Polygon poly){
  62. Rectangle2D bounds = poly.getBounds2D();
  63. Point p;
  64. do {
  65. p = new Point((int)(bounds.getMinX() + gen.nextFloat()*bounds.getWidth()),(int)(bounds.getMinY() + gen.nextFloat()*bounds.getHeight()));
  66. } while (!poly.contains(p));
  67. return p;
  68. }
  69.  
  70. public Point getVertex(Polygon poly){
  71. int index = gen.nextInt(poly.npoints);
  72. return new Point(poly.xpoints[index], poly.ypoints[index]);
  73. }
  74.  
  75. public Point getMidpoint(Point p1, Point p2) {
  76. return new Point((int)((p1.x+p2.x)/2), (int)((p1.y+p2.y)/2));
  77. }
  78.  
  79.  
  80. private class DirectionListener implements KeyListener
  81. {
  82. public void keyPressed (KeyEvent event)
  83. {
  84. createFractalPoint(triangle);
  85. repaint();
  86. }
  87. public void keyTyped (KeyEvent event) {}
  88. public void keyReleased (KeyEvent event) {}
  89. }
  90. }
This still gives you some room to grow on. You are just using the midpoint, but the algorithm calls for a random distance between the point and vertex. You can easily do that with the slope and distance between the point and vertex.
You might also consider a method that takes a number of sides and creates that polygon for use, instead of the statically defined triangle that is there currently.
Hope this helps a bit.
Last edited by Ezzaral : Nov 16th, 2007 at 7:40 pm.
Reply With Quote  
Join Date: Nov 2007
Posts: 13
Reputation: HeroOfTime is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
HeroOfTime HeroOfTime is offline Offline
Newbie Poster

Re: Fractals and the Chaos Game help.

  #3  
Nov 18th, 2007
Ummmm okay, I'm going to state my problem again, cause My problem still isn't fixed...... Alright, so I thought I was doing everything correct, and it looks like it should be coming But, for some reason, the points that are made are not appearing inside the triangle, instead they make a line along the edge of the triangle! I put my problem in Italics, it would be great if someone could tell me why this is happening!
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Fractals and the Chaos Game help.

  #4  
Nov 18th, 2007
which one are you trying to do the Sierpinski Triangle?
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,076
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 306
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Fractals and the Chaos Game help.

  #5  
Nov 18th, 2007
Originally Posted by HeroOfTime View Post
Ummmm okay, I'm going to state my problem again, cause My problem still isn't fixed...... Alright, so I thought I was doing everything correct, and it looks like it should be coming But, for some reason, the points that are made are not appearing inside the triangle, instead they make a line along the edge of the triangle! I put my problem in Italics, it would be great if someone could tell me why this is happening!

Well, for one thing, you were not calculating the inner point correctly. The code that I reworked a bit for you does give a random inside point, without some of the hassles you were having with the data structures. Since I already took the time to get that working for you, I'm not much inclined to fiddle any more with it. By the way - you're welcome.

edit:
Oh, and tell me anything that might make it better, but simple
Which I did...
Last edited by Ezzaral : Nov 18th, 2007 at 2:38 pm.
Reply With Quote  
Join Date: Nov 2007
Posts: 13
Reputation: HeroOfTime is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
HeroOfTime HeroOfTime is offline Offline
Newbie Poster

Re: Fractals and the Chaos Game help.

  #6  
Nov 18th, 2007
Originally Posted by Ezzaral View Post
Well, for one thing, you were not calculating the inner point correctly. The code that I reworked a bit for you does give a random inside point, without some of the hassles you were having with the data structures. Since I already took the time to get that working for you, I'm not much inclined to fiddle any more with it. By the way - you're welcome.

edit: Which I did...


Sorry, I didn't mean that I didn't appreciate your help that you gave me. Thank you!

But, I just got a bit irritated, because It's still the same problem that illudes me completely of why it's not working....

One last time: Thank You again, and does anyone have any clue why they aren't going inside the triangle?

BTW, yes, I'm starting with the Triangle....
Reply With Quote  
Join Date: Nov 2007
Posts: 13
Reputation: HeroOfTime is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
HeroOfTime HeroOfTime is offline Offline
Newbie Poster

Re: Fractals and the Chaos Game help.

  #7  
Nov 18th, 2007
It's working now! I..... don't get it! It wasn't working before! That is crazy.... THANK YOU Ezzaral !!!! Man, now I feel bad for saying that you didn't solve my problem, cause you did, I think.... I willl probably add those functionallity that you said pretty soon, I'll post them when I get them done.
What if there were no hypothetical situations?
Reply With Quote  
Join Date: Nov 2007
Posts: 13
Reputation: HeroOfTime is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
HeroOfTime HeroOfTime is offline Offline
Newbie Poster

Re: Fractals and the Chaos Game help.

  #8  
Nov 19th, 2007
alright, I've pretty much got the whole thing done, just some touch up that I might not ever do. Oh, and we haven't gone over exceptions, so that's why there are'nt any in there.

  1. import java.awt.geom.Rectangle2D;
  2. import javax.swing.JFrame;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.util.Random;
  7. import java.util.ArrayList;
  8.  
  9. public class Fractal2 extends JPanel
  10. {
  11. private int[] triangle01 = {10, 500, 245};
  12. private int[] triangle02 = {10, 10, 440};
  13. private int[] square01 = {10,500,500,10};
  14. private int[] square02 = {10,10,500,500};
  15.  
  16. Random gen = new Random();
  17.  
  18. ArrayList<Point> dots = new ArrayList<Point>(); //holds all the points that
  19. //are made in this program
  20.  
  21. ArrayList<Point> newShape = new ArrayList<Point>(); // Holds the points
  22. //for the new user entered shape
  23.  
  24. Polygon triangle = new Polygon(triangle01, triangle02, triangle01.length);
  25. Polygon square = new Polygon(square01, square02, square01.length);
  26. Point previousPoint = getInnerPoint(triangle);
  27. Polygon currentPolygon = triangle;
  28. JRadioButton square1, triangle1, user;
  29. JButton add1, add10, add100, add1000;
  30. JButton clear;
  31. int currentnum = 1;
  32. int currentden = 2;
  33. JTextField den, num;
  34. Polygon userShape;
  35.  
  36.  
  37. public Fractal2 ()
  38. {
  39. setLayout(new BorderLayout());
  40. JPanel south = new JPanel();
  41.  
  42.  
  43. JLabel numerator = new JLabel("numerator");
  44. JLabel denominator = new JLabel("denominator");
  45. den = new JTextField("2", 3);
  46. num = new JTextField("1", 3);
  47.  
  48. square1 = new JRadioButton("Square ", false);
  49. triangle1 = new JRadioButton("Triangle", true);
  50. user = new JRadioButton("User", false);
  51.  
  52. ButtonGroup group = new ButtonGroup();
  53. group.add(triangle1);
  54. group.add(square1);
  55. group.add(user);
  56.  
  57. ShapeListener listener = new ShapeListener();
  58. square1.addActionListener(listener);
  59. triangle1.addActionListener(listener);
  60.  
  61. add1 = new JButton ("1");
  62. add10 = new JButton ("10");
  63. add100 = new JButton ("100");
  64. add1000 = new JButton ("1000");
  65. clear = new JButton ("Clear");
  66. add1.addActionListener(listener);
  67. add10.addActionListener(listener);
  68. add100.addActionListener(listener);
  69. add1000.addActionListener(listener);
  70. clear.addActionListener(listener);
  71.  
  72. south.add(add1);
  73. south.add(add10);
  74. south.add(add100);
  75. south.add(add1000);
  76. south.add(triangle1);
  77. south.add(square1);
  78. south.add(clear);
  79. south.add(numerator);
  80. south.add(num);
  81. south.add(denominator);
  82. south.add(den);
  83. south.add(clear);
  84.  
  85.  
  86.  
  87.  
  88. add(south, BorderLayout.SOUTH);
  89. NewShapeListener listener2 = new NewShapeListener();
  90. addMouseListener (listener2);
  91. addMouseMotionListener (listener2);
  92.  
  93. setBackground (Color.black);
  94. setPreferredSize (new Dimension (1000,1000));
  95.  
  96. }
  97.  
  98.  
  99. public static void main(String[] args)
  100. {
  101. JFrame frame = new JFrame ("Fractal");
  102. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  103.  
  104. Fractal2 panel = new Fractal2();
  105.  
  106. frame.getContentPane().add(panel);
  107. frame.pack();
  108. frame.setVisible(true);
  109. }
  110.  
  111. public void paintComponent (Graphics page)
  112. {
  113. Graphics2D g2d = (Graphics2D)page;
  114. g2d.setBackground(Color.black);
  115. g2d.clearRect(0,0,getWidth(), getHeight());
  116. g2d.setColor(Color.white);
  117. for (Point p : dots){
  118. g2d.fillRect(p.x,p.y,1,1);
  119. }
  120. g2d.setColor(Color.white);
  121. g2d.drawPolygon(currentPolygon);
  122. }
  123.  
  124.  
  125. public void createFractalPoint(Polygon poly){
  126. Point vertex = getVertex(poly);
  127. if (dots.size() == 0)
  128. {
  129. dots.add(previousPoint);
  130. }
  131. Point last = dots.get(0);
  132. for (Point x : dots)
  133. last = x;
  134. Point inner = getDistance(last, vertex, currentnum, currentden);
  135. dots.add(inner);
  136. }
  137.  
  138. public Point getInnerPoint(Polygon poly){
  139. Rectangle2D bounds = poly.getBounds2D();
  140. Point p;
  141. do {
  142. p = new Point((int)(bounds.getMinX() +
  143. gen.nextFloat()*bounds.getWidth()),
  144. (int)(bounds.getMinY() + gen.nextFloat()*bounds.getHeight()));
  145. } while (!poly.contains(p));
  146. return p;
  147. }
  148.  
  149. public Point getVertex(Polygon poly){
  150. int index = gen.nextInt(poly.npoints);
  151. return new Point(poly.xpoints[index], poly.ypoints[index]);
  152. }
  153.  
  154. public Point getDistance(Point p1, Point p2, int numerator, double denom) {
  155. return new Point((int)( ((p1.x+p2.x) * numerator) / denom), (int)( (
  156. (p1.y+p2.y) * numerator) / denom)); }
  157.  
  158. public Polygon getUserShape()
  159. {
  160. int size = newShape.size();
  161. int[] userShapex = new int[size];
  162. for (int x = 0; x < size; x++)
  163. {
  164. userShapex[x] = newShape.get(x).x;
  165. }
  166. int[] userShapey = new int[size];
  167. for (int x = 0; x < size; x++)
  168. {
  169. userShapey[x] = newShape.get(x).y;
  170. }
  171. Polygon userShape = new Polygon(userShapex, userShapey, size);
  172. return userShape;
  173.  
  174.  
  175. }
  176.  
  177. private class ShapeListener implements ActionListener
  178. {
  179. public void actionPerformed (ActionEvent event)
  180. {
  181. Object source = event.getSource();
  182. String denValStr = den.getText();
  183. String numValStr = num.getText();
  184. currentnum = Integer.parseInt(numValStr);
  185. currentden = Integer.parseInt(denValStr);
  186.  
  187. if (source == square1)
  188. {
  189. dots.clear();
  190. currentPolygon = square;
  191. }
  192. else if (source == triangle1)
  193. {
  194. dots.clear();
  195. currentPolygon = triangle;
  196. }
  197. repaint();
  198. if (source == add1)
  199. createFractalPoint(currentPolygon);
  200. else if (source == add10)
  201. {
  202. int x = 0;
  203. while (x < 10)
  204. {
  205.  
  206. createFractalPoint(currentPolygon);
  207. x++;
  208. }
  209. }
  210. else if (source == add100)
  211. {
  212. int x = 0;
  213. while (x < 100)
  214. {
  215. createFractalPoint(currentPolygon);
  216. x++;
  217. }
  218.  
  219. }
  220. else if (source == add1000)
  221. {
  222. int x = 0;
  223. while ( x < 1000)
  224. {
  225. createFractalPoint(currentPolygon);
  226. x++;
  227. }
  228. }
  229. else if (source == clear)
  230. {
  231. newShape.clear();
  232. }
  233. else if (source == user)
  234. {
  235. if (newShape.size() > 2)
  236. userShape = getUserShape();
  237. }
  238. repaint();
  239. }
  240. }
  241.  
  242. private class NewShapeListener implements MouseListener, MouseMotionListener
  243. {
  244. public void mousePressed (MouseEvent event)
  245. {
  246. Point point1 = event.getPoint();
  247. newShape.add(point1);
  248. if (newShape.size() > 2)
  249. currentPolygon = getUserShape();
  250. repaint();
  251. }
  252. public void mouseClicked (MouseEvent event) {}
  253. public void mouseReleased (MouseEvent event) {}
  254. public void mouseEntered (MouseEvent event) {}
  255. public void mouseExited (MouseEvent event) {}
  256. public void mouseMoved (MouseEvent event) {}
  257. public void mouseDragged (MouseEvent event) {}
  258. }
  259. }
What if there were no hypothetical situations?
Reply With Quote