Java Reference
In-Depth Information
19 frame.setLocationRelativeTo( null ); // Center the frame
20 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21 frame.setVisible( true );
22 }
23 }
F IGURE 13.9
Six FigurePanel objects are created to display six figures.
The FigurePanel class is implemented in Listing 13.3. Four constants— LINE ,
RECTANGLE , ROUND_RECTANGLE , and OVAL —are declared in lines 6-9. Four types of figures
are drawn according to the type property (line 37). The setColor method (lines 39, 44, 53,
62) sets a new color for the drawing.
L ISTING 13.3 FigurePanel.java
1 import java.awt.*;
2 import javax.swing.JPanel;
3
4 public class FigurePanel extends JPanel {
5
// Declare constants
constants
6
public static final int LINE = 1 ;
7
public static final int RECTANGLE = 2 ;
8
public static final int ROUND_RECTANGLE = 3 ;
9
public static final int OVAL = 4 ;
10
11
private int type = 1 ;
12
private boolean filled = false ;
13
14 /** Construct a default FigurePanel */
15 public FigurePanel() {
16 }
17
18
/** Construct a FigurePanel with the specified type */
19
public FigurePanel( int type) {
20
this .type = type;
21 }
22
23
/** Construct a FigurePanel with the specified type and filled */
24
public FigurePanel( int type, boolean filled) {
25
this .type = type;
26
this .filled = filled;
27 }
28
29 @Override // Draw a figure on the panel
30
override
paintComponent(g)
protected void paintComponent(Graphics g) {
31
super .paintComponent(g);
32
33
// Get the appropriate size for the figure
34
int width = getWidth();
35
int height = getHeight();
36
37
switch (type)
check type
{
38
case LINE: // Display two cross lines
 
 
Search WWH ::




Custom Search