19.09.2015 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

13.8 Case Study: The MessagePanel Class 499<br />

115 repaint();<br />

116 }<br />

117<br />

118 /** Move the message up */<br />

119 public void moveUp() {<br />

120 yCoordinate -= interval;<br />

121 repaint();<br />

122 }<br />

123<br />

124 /** Move the message down */<br />

125 public void moveDown() {<br />

126 yCoordinate += interval;<br />

127 repaint();<br />

128 }<br />

129<br />

130 @Override /** Override get method for preferredSize */<br />

131 public Dimension getPreferredSize() {<br />

132 return new Dimension(200, 30);<br />

133 }<br />

134 }<br />

override<br />

getPreferredSize<br />

The paintComponent method displays the message centered, if the centered property<br />

is true (line 91). message is initialized <strong>to</strong> Wel<strong>com</strong>e <strong>to</strong> <strong>Java</strong> in line 8. If it were not<br />

initialized, a NullPointerException runtime error would occur when you created<br />

a MessagePanel using the no-arg construc<strong>to</strong>r, because message would be null in<br />

line 103.<br />

Caution<br />

The MessagePanel class uses the properties xCoordinate and yCoordinate <strong>to</strong><br />

specify the position of the message displayed on the panel. Do not use the property<br />

names x and y, because they are already defined in the Component class <strong>to</strong> return the<br />

position of the <strong>com</strong>ponent in the parent’s coordinate system using getX() and<br />

getY().<br />

Note<br />

The Component class has the setBackground, setForeground, and setFont<br />

methods. These methods are for setting colors and fonts for the entire <strong>com</strong>ponent. If<br />

you wanted <strong>to</strong> draw several messages in a panel with different colors and fonts, you<br />

would have <strong>to</strong> use the setColor and setFont methods in the Graphics class <strong>to</strong><br />

set the color and font for the current drawing.<br />

Note<br />

A key feature of <strong>Java</strong> programming is the reuse of classes. Throughout this book,<br />

reusable classes are developed and later reused. MessagePanel is an example, as are<br />

Loan in Listing 10.2 and FigurePanel in Listing 13.3. MessagePanel can be<br />

reused whenever you need <strong>to</strong> display a message on a panel. To make your class reusable<br />

in a wide range of applications, you should provide a variety of ways <strong>to</strong> use it.<br />

MessagePanel provides many properties and methods that will be used in several<br />

examples in the book.<br />

design classes for reuse<br />

13.21 If message is not initialized in line 8 in Listing 13.8, MessagePanel.java, what will<br />

happen when you create a MessagePanel using its no-arg construc<strong>to</strong>r?<br />

13.22 The following program is supposed <strong>to</strong> display a message on a panel, but nothing is<br />

displayed. There are problems in lines 2 and 15. Correct them.<br />

✓Point✓ Check

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

Saved successfully!

Ooh no, something went wrong!