Java Reference
In-Depth Information
The
MessagePanel
class is implemented in Listing 13.8. The program seems long but is
actually simple, because most of the methods are
get
and
set
methods, and each method is
relatively short and easy to read.
L
ISTING
13.8
MessagePanel.java
1
import
java.awt.FontMetrics;
2
import
java.awt.Dimension;
3
import
java.awt.Graphics;
4
import
javax.swing.JPanel;
5
6
public class
MessagePanel
extends
JPanel
{
7
/** The message to be displayed */
8
private
String message =
"Welcome to Java"
;
9
10
/** The x-coordinate where the message is displayed */
11
private int
xCoordinate =
20
;
12
13
/** The y-coordinate where the message is displayed */
14
private int
yCoordinate =
20
;
15
16
/** Indicate whether the message is displayed in the center */
17
private boolean
centered;
18
19
/** The interval for moving the message horizontally
20
* and vertically */
21
private int
interval =
10
;
22
23
/** Construct with default properties */
24
public
MessagePanel()
{
25 }
26
27
/** Construct a message panel with a specified message */
28
public
MessagePanel(String message)
{
29
this
.message = message;
30 }
31
32
/** Return message */
33
public
String getMessage() {
34
return
message;
35 }
36
37
/** Set a new message */
38
public void
setMessage(String message) {
39
this
.message = message;
40
41 }
42
43
repaint();
repaint panel
/** Return xCoordinator */
44
public int
getXCoordinate() {
45
return
xCoordinate;
46 }
47
48
/** Set a new xCoordinator */
49
public void
setXCoordinate(
int
x) {
50
this
.xCoordinate = x;
51 repaint();
52 }
53
54
repaint panel
/** Return yCoordinator */