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 495<br />

This is a MessagePanel object<br />

stringWidth()<br />

getHeight() stringAscent()<br />

(xCoordinate, yCoordinate)<br />

xCoordinate = getWidth / 2 - stringWidth / 2;<br />

yCoordinate = getHeight / 2 - stringAscent / 2;<br />

FIGURE 13.17 The program uses the FontMetrics class <strong>to</strong> measure the string width and<br />

height and displays it at the center of the panel.<br />

The methods getWidth() and getHeight() (lines 36–37) defined in the Component class<br />

return the <strong>com</strong>ponent’s width and height, respectively.<br />

Since the message is centered, the first character of the string should be positioned at<br />

(xCoordinate, yCoordinate), as shown in Figure 13.17.<br />

13.19 How do you find the leading, ascent, descent, and height of a font?<br />

13.20 How do you find the exact length in pixels of a string in a Graphics object?<br />

13.8 Case Study: The MessagePanel Class<br />

This case study develops a useful class that displays a message in a panel. The class<br />

enables the user <strong>to</strong> set the location of the message, center the message, and move the<br />

message a specified interval.<br />

The contract of the MessagePanel class is shown in Figure 13.18.<br />

Let us first write a test program in Listing 13.7 that uses the MessagePanel class <strong>to</strong> display<br />

four message panels, as shown in Figure 13.19.<br />

LISTING 13.7<br />

TestMessagePanel.java<br />

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

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

3<br />

4 public class TestMessagePanel extends JFrame {<br />

5 public TestMessagePanel() {<br />

6 MessagePanel messagePanel1 = new MessagePanel("Wel<strong>com</strong>e <strong>to</strong> <strong>Java</strong>") ;<br />

7 MessagePanel messagePanel2 = new MessagePanel("<strong>Java</strong> is fun");<br />

8 MessagePanel messagePanel3 = new MessagePanel("<strong>Java</strong> is cool");<br />

9 MessagePanel messagePanel4 = new MessagePanel("I love <strong>Java</strong>");<br />

10 messagePanel1.setFont(new Font("SansSerif", Font.ITALIC, 20));<br />

11 messagePanel2.setFont(new Font("Courier", Font.BOLD, 20));<br />

12 messagePanel3.setFont(new Font("Times", Font.ITALIC, 20));<br />

13 messagePanel4.setFont(new Font("Californian FB", Font.PLAIN, 20));<br />

14 messagePanel1.setBackground(Color.RED);<br />

15 messagePanel2.setBackground(Color.CYAN);<br />

16 messagePanel3.setBackground(Color.GREEN);<br />

17 messagePanel4.setBackground(Color.WHITE);<br />

✓Point✓ Check<br />

Key<br />

Point<br />

VideoNote<br />

The MessagePanel class<br />

create message panel<br />

set font<br />

set background

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

Saved successfully!

Ooh no, something went wrong!