assistance with the following code:

1. package triangleMod;
2. import java.awt.Dimension;
3. import java.awt.Frame;
4. import java.awt.Point;
5.
6. import javax.swing.JFrame;
7.
8.
9. public class Main extends Frame{
10.
11. static Point a;
12. static Point b;
13. static Point c;
14. static Triangle tryAngle;
15. static JFrame frame;
16.
17.
18. private static final long serialVersionUID = 1L;
19.
20. //frame setup taken from:
21. //http://java.sun.com/docs/books/tutorial/uiswing/examples/components/FrameDemoProject/src/components/FrameDemo.java
22. private static void createAndShowGUI() {
23. //Create and set up the window.
24. frame = new JFrame("Fractal: Sierpinski's Triangle");
25. a = new Point(0, 443);
26. b = new Point(512, 443);
27. c = new Point(256, 0);
28. tryAngle = new Triangle(a, b, c);
29.
30. tryAngle.setPreferredSize(new Dimension(514, 446));
31. //frame.add(tryAngle);
32. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
33.
34. frame.getContentPane().add(tryAngle);
35.
36. //Display the window.
37. frame.pack();
38. frame.setVisible(true);
39. }
40.
41. public static void main(String[] args) {
42. //Schedule a job for the event-dispatching thread:
43. //creating and showing this application's GUI.
44. javax.swing.SwingUtilities.invokeLater(new Runnable() {
45. public void run() {
46. createAndShowGUI();
47. }
48. });
49. }
50. }


1. package triangleMod;
2. import java.awt.Component;
3. import java.awt.Graphics;
4. import java.awt.Point;
5.
6. class Triangle extends Component{
7.
8. private static final long serialVersionUID = 1L;
9.
10. //A, B, C initial triangle-points, assigned in the constructor
11. private Point A, B, C;
12. //points for the 3 new inner triangles in the current recursive step
13. private Point A1, B1, C1, A2, B2, C2, A3, B3, C3;
14.
15. public Triangle(Point a, Point b, Point c){
16. this.A = a;
17. this.B = b;
18. this.C = c;
19. }
20.
21. //helper var for recursion termination
22. int i= 300;
23.
24. //draws fractal
25. public void drawFractal(Graphics g, Point a, Point b, Point c){
26.
27. /**
28. * draw outer triangle
29. **/
30.
31. //AB
32. g.drawLine(a.x, a.y, b.x, b.y);
33. //AC
34. g.drawLine(a.x, a.y, c.x, c.y);
35. //BC
36. g.drawLine(b.x, b.y, c.x, c.y);
37.
38. /**
39. * Points of the 3 new triangles get assigned
40. **/
41.
42. //lower left triangle coordinates
43. A1 = new Point(A.x, A.y);
44. B1 = new Point(c.x, b.y);
45. C1 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
46.
47. //lower right triangle coordinates
48. A2 = new Point(c.x, b.y);
49. B2 = new Point(B.x, B.y);
50. C2 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
51.
52. //upper triangle coordinates
53. A3 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
54. B3 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
55. C3 = new Point(C.x, C.y);
56.
57. /**
58. * draw inner reversed triangle
59. **/
60.
61. //AB
62. g.drawLine(A3.x, A3.y, B3.x, B3.y);
63. //AC
64. g.drawLine(A3.x, A3.y, A2.x, A2.y);
65. //BC
66. g.drawLine(B3.x, B3.y, A2.x, A2.y);
67.
68. /**
69. * rekusive method calls for the three new triangles
70. **/
71.
72. //termination condition not set correctly, helper var i used for testing
73. while (i-->0) {
74.
75. drawFractal(g, A1, B1, C1);
76. //drawFractal(g, A2, B2, C2);
77. //drawFractal(g, A3, B3, C3);
78.
79. }
80. }
81.
82. public void paint(Graphics g){
83. drawFractal(g, A, B, C);
84. }
85. }

Recommended Answers

All 2 Replies

What exactly is the question?

A) Please use code tags (CODE=Java), in place of (CODE).

B) That wasn't much of a question, but it seems like your problem is that it doesn't recursively draw the triangles, right? Well, I ran it on my computer, and realized that 'i' was never being reset after drawing the recursive pattern. If you reset i to 300 (i think 300 is a bit too much XD) before calling drawFractal the first time, it works (If I understiood what you were asking and what the program is supposed to do...).

llemes4011

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.