19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

13.4 Case Study: The FigurePanel Class 485<br />

13.4 Case Study: The FigurePanel Class<br />

This case study develops the FigurePanel class for displaying various figures.<br />

This example develops a useful class for displaying various figures. The class enables the user<br />

<strong>to</strong> set the figure type and specify whether the figure is filled, and it displays the figure on a<br />

panel. The UML diagram for the class is shown in Figure 13.8. The panel can display lines,<br />

rectangles, round-cornered rectangles, and ovals. Which figure <strong>to</strong> display is decided by the<br />

type property. If the filled property is true, the rectangle, round-cornered rectangle, and<br />

oval are filled in the panel.<br />

Key<br />

Point<br />

VideoNote<br />

The FigurePanel class<br />

javax.swing.JPanel<br />

FigurePanel<br />

+LINE = 1<br />

+RECTANGLE = 2<br />

+ROUND_RECTANGLE = 3<br />

+OVAL = 4<br />

-type: int<br />

-filled: boolean<br />

+FigurePanel()<br />

+FigurePanel(type: int)<br />

+FigurePanel(type: int, filled: boolean)<br />

+getType(): int<br />

+setType(type: int): void<br />

+isFilled(): boolean<br />

+setFilled(filled: boolean): void<br />

LINE, RECTANGLE,<br />

ROUND_RECTANGLE, and OVAL are<br />

constants, indicating the figure type.<br />

Specifies the figure type (default: 1).<br />

Specifies whether the figure is filled (default: false).<br />

Creates a default figure panel.<br />

Creates a figure panel with the specified type.<br />

Creates a figure panel with the specified type and filled property.<br />

Returns the figure type.<br />

Sets a new figure type.<br />

Checks whether the figure is filled with a color.<br />

Sets a new filled property.<br />

FIGURE 13.8<br />

FigurePanel displays various types of figures on the panel.<br />

The UML diagram serves as the contract for the FigurePanel class. The user can use the<br />

class without knowing how the class is implemented. Let us begin by writing a program in<br />

Listing 13.2 that uses the class <strong>to</strong> display six figure panels, as shown in Figure 13.9.<br />

LISTING 13.2 TestFigurePanel.java<br />

1 import java.awt.*;<br />

2 import javax.swing.*;<br />

3<br />

4 public class TestFigurePanel extends JFrame {<br />

5 public TestFigurePanel() {<br />

6 setLayout(new GridLayout(2, 3, 5, 5));<br />

7 add(new FigurePanel(FigurePanel.LINE));<br />

8 add(new FigurePanel(FigurePanel.RECTANGLE));<br />

9 add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));<br />

10 add(new FigurePanel(FigurePanel.OVAL));<br />

11 add(new FigurePanel(FigurePanel.RECTANGLE, true));<br />

12 add(new FigurePanel(FigurePanel.ROUND_RECTANGLE, true));<br />

13 }<br />

14<br />

15 public static void main(String[] args) {<br />

16 TestFigurePanel frame = new TestFigurePanel();<br />

17 frame.setSize(400, 200);<br />

18 frame.setTitle("TestFigurePanel");<br />

create figures

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!