This case study develops a useful class that displays a message in a panel. The class enables the user to set the location of the message, center the message, and move the message with the specified interval. The contract of the class is shown in Figure 13.18.
Let us first write a test program in Listing 13.9 that uses the MessagePanel class to display four message panels, as shown in Figure 13.19.
1 import java.awt.*; 2 import javax.swing.*; 3 4 public class TestMessagePanel extends JFrame { 5 public TestMessagePanel() { 6 MessagePanel messagePanel1 = new MessagePanel( "Wecome to Java" ) ; 7 MessagePanel messagePanel2 = new MessagePanel( "Java is fun" ); 8 MessagePanel messagePanel3 = new MessagePanel( "Java is cool" ); 9 MessagePanel messagePanel4 = new MessagePanel( "I love Java" ); 10 messagePanel1.setFont( new Font( "SansSerif" , Font.ITALIC, 20 )); 11 messagePanel2.setFont( new Font( "Courier" , Font.BOLD, 20 )); 12 messagePanel3.setFont( new Font( "Times" , Font.ITALIC, 20 )); 13 messagePanel4.setFont( new Font( "Californian FB" , Font.PLAIN, 20 )); 14 messagePanel1.setBackground( Color .red); 15 messagePanel2.setBackground(Color.cyan); 16 messagePanel3.setBackground(Color.green); 17 messagePanel4.setBackground(Color.white); 18 messagePanel1.setCentered( true ); 19 20 setLayout( new GridLayout( 2 , 2 )); 21 add(messagePanel1); 22 add(messagePanel2); 23 add(messagePanel3); 24 add(messagePanel4); 25 } 26 27 public static void main(String[] args) { 28 TestMessagePanel frame = new TestMessagePanel(); 29 frame.setSize( 300 , 200 ); 30 frame.setTitle( "TestMessagePanel" ); 31 frame.setLocationRelativeTo( null ); // Center the frame 32 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 33 frame.setVisible( true ); 34 } 35 } |
The MessagePanel class is implemented in Listing 13.10. 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.
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 and vertically */ 20 private int interval = 10 ; 21 22 /** Construct with default properties */ 23 public MessagePanel() { 24 } 25 26 /** Construct a message panel with a specified message */ 27 public MessagePanel(String message) { 28 this .message = message; 29 } 30 31 /** Return message */ 32 public String getMessage() { 33 return message; 34 } 35 36 /** Set a new message */ 37 public void setMessage(String message) { 38 this .message = message; 39 repaint(); 40 } 41 42 /** Return xCoordinator */ 43 public int getXCoordinate() { 44 return xCoordinate; 45 } 46 47 /** Set a new xCoordinator */ 48 public void setXCoordinate( int x) { 49 this .xCoordinate = x; 50 repaint(); 51 } 52 53 /** Return yCoordinator */ 54 public int getYCoordinate() { 55 return yCoordinate; 56 } 57 58 /** Set a new yCoordinator */ 59 public void setYCoordinate( int y) { 60 this .yCoordinate = y; 61 repaint(); 62 } 63 64 /** Return centered */ 65 public boolean isCentered() { 66 return centered; 67 } 68 69 /** Set a new centered */ 70 public void setCentered( boolean centered) { 71 this .centered = centered; 72 repaint(); 73 } 74 75 /** Return interval */ 76 public int getInterval() { 77 return interval; 78 } 79 80 /** Set a new interval */ 81 public void setInterval( int interval) { 82 this .interval = interval; 83 repaint(); 84 } 85 86 /** Paint the message */ 87 protected void paintComponent(Graphics g) { 88 super .paintComponent(g); 89 90 if (centered) { 91 // Get font metrics for the current font 92 FontMetrics fm = g.getFontMetrics(); 93 94 // Find the center location to display 95 int stringWidth = fm.stringWidth(message); 96 int stringAscent = fm.getAscent(); 97 // Get the position of the leftmost character in the baseline 98 xCoordinate = getWidth() / 2 - stringWidth / 2 ; 99 yCoordinate = getHeight() / 2 + stringAscent / 2 ; 100 } 101 102 g.drawString(message, xCoordinate, yCoordinate); 103 } 104 105 /** Move the message left */ 106 public void moveLeft() { 107 xCoordinate -= interval; 108 repaint(); 109 } 110 111 /** Move the message right */ 112 public void moveRight() { 113 xCoordinate += interval; 114 repaint(); 115 } 116 117 /** Move the message up */ 118 public void moveUp() { 119 yCoordinate -= interval; 120 repaint(); 121 } 122 123 /** Move the message down */ 124 public void moveDown() { 125 yCoordinate += interval; 126 repaint(); 127 } 128 129 /** Override get method for preferredSize */ 130 public Dimension getPreferredSize() { 131 return new Dimension( 200 , 30 ); 132 } 133 } |
The paintComponent method displays the message centered, if the centered property is true (line 90). message is initialized to "Welcome to Java" in line 8. If it is not initialized, a NullPointerException runtime error would occur when you create a MessagePanel using the no-arg constructor, because message would be null in line 102.
Caution
The MessagePanel class uses the properties xCoordinate and yCoordinate to specify the position of the message displayed on the panel. Do not use the property names x and y , because they are already defined in the Component class to return the position of the component in the parent's coordinate system using getX() and getY() . |
Note
The Component class has the setBackground , setForeground , and setFont methods. These methods are for setting colors and fonts for the entire component. Suppose you want to draw several messages in a panel with different colors and fonts; you have to use the setColor and setFont methods in the Graphics class to set the color and font for the current drawing. |
Note
One of the key features of Java programming is the reuse of classes. Throughout the book, reusable classes are developed and later reused. MessagePanel is an example of this, as are Loan in §7.15 and FigurePanel in §13.7. It can be reused whenever you need to display a message on a panel. To make your class reusable in a wide range of applications, you should provide a variety of ways to use it. MessagePanel provides many properties and methods that will be used in several examples in the book. |