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.

500 Chapter 13 Graphics<br />

1 public class TestDrawMessage extends javax.swing.JFrame {<br />

2 public void TestDrawMessage() {<br />

3 add(new DrawMessage());<br />

4 }<br />

5<br />

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

7 javax.swing.JFrame frame = new TestDrawMessage();<br />

8 frame.setSize(100, 200);<br />

9 frame.setVisible(true);<br />

10 }<br />

11 }<br />

12<br />

13 class DrawMessage extends javax.swing.JPanel {<br />

14 @Override<br />

15 protected void PaintComponent(java.awt.Graphics g) {<br />

16 super.paintComponent(g);<br />

17 g.drawString("Wel<strong>com</strong>e <strong>to</strong> <strong>Java</strong>", 20, 20);<br />

18 }<br />

19 }<br />

Key<br />

Point<br />

13.9 Case Study: The StillClock Class<br />

This case study develops a class that displays a clock on a panel.<br />

The contract of the StillClock class is shown in Figure 13.20.<br />

VideoNote<br />

The StillClock class<br />

javax.swing.JPanel<br />

StillClock<br />

The get and set methods for these<br />

data fields are provided in the class, but<br />

omitted in the UML diagram for brevity.<br />

-hour: int<br />

-minute: int<br />

-second: int<br />

+StillClock()<br />

+StillClock(hour: int, minute: int,<br />

second: int)<br />

+setCurrentTime(): void<br />

The hour in the clock.<br />

The minute in the clock.<br />

The second in the clock.<br />

Constructs a default clock for the current time.<br />

Constructs a clock with a specified time.<br />

Sets hour, minute, and second <strong>to</strong> current time.<br />

FIGURE 13.20<br />

StillClock displays an analog clock.<br />

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

an analog clock and uses the MessagePanel class <strong>to</strong> display the hour, minute, and second in<br />

a panel, as shown in Figure 13.21a.<br />

LISTING 13.9<br />

DisplayClock.java<br />

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

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

3<br />

4 public class DisplayClock extends JFrame {

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

Saved successfully!

Ooh no, something went wrong!