Java Reference
In-Depth Information
Origin (0, 0)
Fig. 18.17 | “Lo feather fractal” at level 2.
The application in Fig. 18.18 defines the user interface for drawing this fractal (shown
at the end of Fig. 18.19). The interface consists of three buttons—one for the user to change
the color of the fractal, one to increase the level of recursion and one to decrease the level of
recursion. A JLabel keeps track of the current level of recursion, which is modified by calling
method setLevel , to be discussed shortly. Lines 15-16 specify constants WIDTH and HEIGHT
to be 400 and 480 , respectively, for the size of the JFrame . The user triggers an ActionEvent
by clicking the Color button. The event handler for this button is registered in lines 37-54.
The method actionPerformed displays a JColorChooser . This dialog returns the selected
Color object or blue (if the user presses Cancel or closes the dialog without pressing OK ). Line
51 calls the setColor method in class FractalJPanel to update the color.
1
// Fig. 18.18: Fractal.java
2
// Fractal user interface.
3
import java.awt.Color;
4
import java.awt.FlowLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import javax.swing.JFrame;
8
import javax.swing.JButton;
9
import javax.swing.JLabel;
10
import javax.swing.JPanel;
11
import javax.swing.JColorChooser;
12
13
public class Fractal extends JFrame
14
{
15
private static final int WIDTH = 400 ; // define width of GUI
16
private static final int HEIGHT = 480 ; // define height of GUI
17
private static final int MIN_LEVEL = 0 ;
18
private static final int MAX_LEVEL = 15 ;
19
20
// set up GUI
21
public Fractal()
22
{
Fig. 18.18 | Fractal user interface. (Part 1 of 3.)
 
 
Search WWH ::




Custom Search