Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import javax.swing.JFrame;
- public class FractalTree extends JFrame
- {
- public double toRad(double grad)
- {
- return (grad*Math.PI)/180;
- }
- public FractalTree()
- {
- super("Fractal Tree");
- setBounds(0, 0, 1024, 768);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- private void drawTree(Graphics g, int x1, int y1, double angle, int depth)
- {
- if (depth == 0) return;
- int x2 = x1 + (int) (Math.cos(toRad(angle)) * depth * 10.0);
- int y2 = y1 + (int) (Math.sin(toRad(angle)) * depth * 10.0);
- g.drawLine(x1, y1, x2, y2);
- drawTree(g, x2, y2, angle - 20, depth - 1);
- drawTree(g, x2, y2, angle + 20, depth - 1);
- }
- @Override
- public void paint(Graphics g)
- {
- getContentPane().setBackground(Color.BLACK);
- g.setColor(Color.YELLOW);
- drawTree(g, 512, 768, -90, 11);
- }
- public static void main(String[] args) {
- JFrame jf = new FractalTree();
- jf.setVisible(true);
- jf.getContentPane().setBackground(Color.BLACK);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement