Pythagoras tree with g2d

Posted by owca on Stack Overflow See other posts from Stack Overflow or by owca
Published on 2010-04-26T13:03:36Z Indexed on 2010/05/22 17:20 UTC
Read the original article Hit count: 223

Filed under:
|
|
|

I'm trying to build my first fractal (Pythagoras Tree):

alt text

in Java using Graphics2D. Here's what I have now :

import java.awt.*;
import java.awt.geom.*; 
import javax.swing.*;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    int i=0;
    Scanner scanner = new Scanner(System.in);

    System.out.println("Give amount of steps: ");
    i = scanner.nextInt();

    new Pitagoras(i);
    }
}

class Pitagoras extends JFrame {

private int powt, counter;

public Pitagoras(int i) {
    super("Pythagoras Tree.");
    setSize(1000, 1000);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    powt = i;
}

private void paintIt(Graphics2D g) {

    double p1=450, p2=800, size=200;

    for (int i = 0; i < powt; i++) {
        if (i == 0) {
            g.drawRect((int)p1, (int)p2, (int)size, (int)size);
            counter++;
        }
        else{
            if( i%2 == 0){
                //here I must draw two squares
            }
            else{
                //here I must draw right triangle
            }
        }
    }
}

@Override
public void paint(Graphics graph) {

    Graphics2D g = (Graphics2D)graph;
    paintIt(g);

}

So basically I set number of steps, and then draw first square (p1, p2 and size). Then if step is odd I need to build right triangle on the top of square. If step is even I need to build two squares on free sides of the triangle. What method should I choose now for drawing both triangle and squares ? I was thinking about drawing triangle with simple lines transforming them with AffineTransform but I'm not sure if it's doable and it doesn't solve drawing squares.

© Stack Overflow or respective owner

Related posts about java

Related posts about graphics