Java Reference
In-Depth Information
The fractal image is drawn on a canvas defined by the KochPanel class shown
in Listing 12.7. The paint method makes the initial calls to the recursive method
drawFractal . The three calls to drawFractal in the paint method represent the
original three sides of the equilateral triangle that make up a Koch fractal of
order 1.
LISTING 12.7
//********************************************************************
// KochPanel.java Author: Lewis/Loftus
//
// Represents a drawing surface on which to paint a Koch Snowflake.
//********************************************************************
import java.awt.*;
import javax.swing.JPanel;
public class KochPanel extends JPanel
{
private final int PANEL_WIDTH = 400;
private final int PANEL_HEIGHT = 400;
private final double SQ = Math.sqrt(3.0) / 6;
private final int TOPX = 200, TOPY = 20;
private final int LEFTX = 60, LEFTY = 300;
private final int RIGHTX = 340, RIGHTY = 300;
private int current; // current order
//-----------------------------------------------------------------
// Sets the initial fractal order to the value specified.
//-----------------------------------------------------------------
public KochPanel ( int currentOrder)
{
current = currentOrder;
setBackground (Color.black);
setPreferredSize ( new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
}
//-----------------------------------------------------------------
// Draws the fractal recursively. Base case is an order of 1 for
// which a simple straight line is drawn. Otherwise three
Search WWH ::




Custom Search