Java Reference
In-Depth Information
1 public class TestDrawMessage extends javax.swing.JFrame {
2 public void TestDrawMessage() {
3 add( new DrawMessage());
4 }
5
6 public static void main(String[] args) {
7 javax.swing.JFrame frame = new TestDrawMessage();
8 frame.setSize( 100 , 200 );
9 frame.setVisible( true );
10 }
11 }
12
13 class DrawMessage extends javax.swing.JPanel {
14 @Override
15 protected void PaintComponent(java.awt.Graphics g) {
16 super .paintComponent(g);
17 g.drawString( "Welcome to Java" , 20 , 20 );
18 }
19 }
13.9 Case Study: The StillClock Class
This case study develops a class that displays a clock on a panel.
Key
Point
The contract of the StillClock class is shown in Figure 13.20.
VideoNote
The StillClock class
javax.swing.JPanel
The get and set methods for these
data fields are provided in the class, but
omitted in the UML diagram for brevity.
StillClock
-hour: int
-minute: int
-second: int
The hour in the clock.
The minute in the clock.
The second in the clock.
+StillClock()
+StillClock(hour: int, minute: int,
second: int)
+setCurrentTime(): void
Constructs a default clock for the current time.
Constructs a clock with a specified time.
Sets hour, minute, and second to current time.
F IGURE 13.20 StillClock displays an analog clock.
Let us first write a test program in Listing 13.9 that uses the StillClock class to display
an analog clock and uses the MessagePanel class to display the hour, minute, and second in
a panel, as shown in Figure 13.21a.
L ISTING 13.9 DisplayClock.java
1 import java.awt.*;
2 import javax.swing.*;
3
4 public class DisplayClock extends JFrame {
 
 
Search WWH ::




Custom Search