Java Reference
In-Depth Information
public static void main(String[] args) {
FractalTree fractalTree = new FractalTree();
fractalTree.createAndShowGUI();
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() != null) {
if (e.getActionCommand().equals("Exit")) {
System.exit(0);
}
if (e.getActionCommand().equals("Repaint")) {
fractalTreePanel.repaint();
}
}
}
}
As I'm sure you know by now, the FractalTree class merely manages the user interface and provides
a place for another class to do the drawing. So let's move on to the class that does the fun stuff: drawing
our fractal trees.
Listing 14-7. FractalTreePanel.java
package com.bryantcs.examples.fractals;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class FractalTreePanel extends JPanel {
private static final long serialVersionUID = 1L;
private final static double RADIANS = Math.PI / 180.0;
Random rand;
public FractalTreePanel() {
rand = new Random();
}
private void drawSegment(Graphics g, int x1, int y1, double angle, int depth) {
if (depth == 0) return; // the stop condition
int xAngleOffset = new Double(Math.cos(angle * RADIANS) * depth * 10.0).intValue();
int yAngleOffset =
new Double(Math.sin(angle * RADIANS) * depth * 10.0).intValue();
int x2 = x1 + xAngleOffset;
int y2 = y1 + yAngleOffset;
int colorValue = 256 - ((depth - 1) * 32) - 1;
Search WWH ::




Custom Search