public class Fractal extends JFrame
1: // Fig. 15.23: Fractal.java
2: // Demonstrates user interface for drawing a fractal.
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 final int WIDTH = 400; // define width of GUI
16: private final int HEIGHT = 480; // define height of GUI
17: private final int MIN_LEVEL = 0, MAX_LEVEL = 15;
18: private Color color = Color.BLUE;
19:
20: private JButton changeColorJButton, increaseLevelJButton,
21: decreaseLevelJButton;
22: private JLabel levelJLabel;
23: private FractalJPanel drawSpace;
24: private JPanel mainJPanel, controlJPanel;
25:
26: // set up GUI
27: public Fractal()
28: {
29: super( "Fractal" );
30:
31: // set up control panel
32: controlJPanel = new JPanel();
33: controlJPanel.setLayout( new FlowLayout() );
34:
35: // set up color button and register listener
36: changeColorJButton = new JButton( "Color" );
37: controlJPanel.add( changeColorJButton );
38: changeColorJButton.addActionListener(
39: new ActionListener() // anonymous inner class
40: {
41: // process changeColorJButton event
42: public void actionPerformed( ActionEvent event )
43: {
44: color = JColorChooser.showDialog(
45: Fractal.this, "Choose a color", color );
46:
47: // set default color, if no color is returned
48: if ( color == null )
49: color = Color.BLUE;
50:
51: drawSpace.setColor( color );
52: } // end method actionPerformed
53: } // end anonymous inner class
54: ); // end addActionListener
55:
56: // set up decrease level button to add to control panel and
57: // register listener
58: decreaseLevelJButton = new JButton( "Decrease Level" );
59: controlJPanel.add( decreaseLevelJButton );
60: decreaseLevelJButton.addActionListener(
61: new ActionListener() // anonymous inner class
62: {
63: // process decreaseLevelJButton event
64: public void actionPerformed( ActionEvent event )
65: {
66: int level = drawSpace.getLevel();
67: level--; // decrease level by one
68:
69: // modify level if possible
70: if ( ( level >= MIN_LEVEL ) &&
71: ( level <= MAX_LEVEL ) )
72: {
73: levelJLabel.setText( "Level: " + level );
74: drawSpace.setLevel( level );
75: repaint();
76: } // end if
77: } // end method actionPerformed
78: } // end anonymous inner class
79: ); // end addActionListener
80:
81: // set up increase level button to add to control panel
82: // and register listener
83: increaseLevelJButton = new JButton( "Increase Level" );
84: controlJPanel.add( increaseLevelJButton );
85: increaseLevelJButton.addActionListener(
86: new ActionListener() // anonymous inner class
87: {
88: // process increaseLevelJButton event
89: public void actionPerformed( ActionEvent event )
90: {
91: int level = drawSpace.getLevel();
92: level++; // increase level by one
93:
94: // modify level if possible
95: if ( ( level >= MIN_LEVEL ) &&
96: ( level <= MAX_LEVEL ) )
97: {
98: levelJLabel.setText( "Level: " + level );
99: drawSpace.setLevel( level );
100: repaint();
101: } // end if
102: } // end method actionPerformed
103: } // end anonymous inner class
104: ); // end addActionListener
105:
106: // set up levelJLabel to add to controlJPanel
107: levelJLabel = new JLabel( "Level: 0" );
108: controlJPanel.add( levelJLabel );
109:
110: drawSpace = new FractalJPanel( 0 );
111:
112: // create mainJPanel to contain controlJPanel and drawSpace
113: mainJPanel = new JPanel();
114: mainJPanel.add( controlJPanel );
115: mainJPanel.add( drawSpace );
116:
117: add( mainJPanel ); // add JPanel to JFrame
118:
119: setSize( WIDTH, HEIGHT ); // set size of JFrame
120: setVisible( true ); // display JFrame
121: } // end constructor Fractal
122:
123: public static void main( String args[] )
124: {
125: Fractal demo = new Fractal();
126: demo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
127: } // end main
128: } // end class Fractal
129:
130: /*************************************************************************
131: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
132: * Pearson Education, Inc. All Rights Reserved. *
133: * *
134: * DISCLAIMER: The authors and publisher of this book have used their *
135: * best efforts in preparing the book. These efforts include the *
136: * development, research, and testing of the theories and programs *
137: * to determine their effectiveness. The authors and publisher make *
138: * no warranty of any kind, expressed or implied, with regard to these *
139: * programs or to the documentation contained in these books. The authors *
140: * and publisher shall not be liable in any event for incidental or *
141: * consequential damages in connection with, or arising out of, the *
142: * furnishing, performance, or use of these programs. *
143: *************************************************************************/