19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

13.8 Case Study: The MessagePanel Class 497<br />

The MessagePanel class is implemented in Listing 13.8. The program seems long but is<br />

actually simple, because most of the methods are get and set methods, and each method is<br />

relatively short and easy <strong>to</strong> read.<br />

LISTING 13.8<br />

MessagePanel.java<br />

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

2 import java.awt.Dimension;<br />

3 import java.awt.Graphics;<br />

4 import javax.swing.JPanel;<br />

5<br />

6 public class MessagePanel extends JPanel {<br />

7 /** The message <strong>to</strong> be displayed */<br />

8 private String message = "Wel<strong>com</strong>e <strong>to</strong> <strong>Java</strong>";<br />

9<br />

10 /** The x-coordinate where the message is displayed */<br />

11 private int xCoordinate = 20;<br />

12<br />

13 /** The y-coordinate where the message is displayed */<br />

14 private int yCoordinate = 20;<br />

15<br />

16 /** Indicate whether the message is displayed in the center */<br />

17 private boolean centered;<br />

18<br />

19 /** The interval for moving the message horizontally<br />

20 * and vertically */<br />

21 private int interval = 10;<br />

22<br />

23 /** Construct with default properties */<br />

24 public MessagePanel() {<br />

25 }<br />

26<br />

27 /** Construct a message panel with a specified message */<br />

28 public MessagePanel(String message) {<br />

29 this.message = message;<br />

30 }<br />

31<br />

32 /** Return message */<br />

33 public String getMessage() {<br />

34 return message;<br />

35 }<br />

36<br />

37 /** Set a new message */<br />

38 public void setMessage(String message) {<br />

39 this.message = message;<br />

40<br />

41<br />

repaint();<br />

}<br />

42<br />

43 /** Return xCoordina<strong>to</strong>r */<br />

44 public int getXCoordinate() {<br />

45 return xCoordinate;<br />

46 }<br />

47<br />

48 /** Set a new xCoordina<strong>to</strong>r */<br />

49 public void setXCoordinate(int x) {<br />

50 this.xCoordinate = x;<br />

51 repaint();<br />

52 }<br />

53<br />

54 /** Return yCoordina<strong>to</strong>r */<br />

repaint panel<br />

repaint panel

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

Saved successfully!

Ooh no, something went wrong!