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.

496 Chapter 13 Graphics<br />

add message panel<br />

18 messagePanel1.setCentered(true);<br />

19<br />

20 setLayout(new GridLayout(2, 2));<br />

21 add(messagePanel1);<br />

22 add(messagePanel2);<br />

23 add(messagePanel3);<br />

24 add(messagePanel4);<br />

25 }<br />

26<br />

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

28 TestMessagePanel frame = new TestMessagePanel();<br />

29 frame.setSize(300, 200);<br />

30 frame.setTitle("TestMessagePanel");<br />

31 frame.setLocationRelativeTo(null); // Center the frame<br />

32 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />

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

34 }<br />

35 }<br />

javax.swing.JPanel<br />

MessagePanel<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 />

-xCoordinate: int<br />

-yCoordinate: int<br />

-centered: boolean<br />

-message: String<br />

-interval: int<br />

The x-coordinate for the message.<br />

The y-coordinate for the message.<br />

Specifies whether the message is displayed centered.<br />

The message <strong>to</strong> be displayed.<br />

The interval <strong>to</strong> move the message in the panel.<br />

+MessagePanel()<br />

+MessagePanel(message: String)<br />

+moveLeft(): void<br />

+moveRight(): void<br />

+moveUp(): void<br />

+moveDown(): void<br />

Constructs a default message panel.<br />

Constructs a message panel with a specified string.<br />

Moves the message <strong>to</strong> the left.<br />

Moves the message <strong>to</strong> the right.<br />

Moves the message up.<br />

Moves the message down.<br />

FIGURE 13.18<br />

MessagePanel displays a message on the panel.<br />

FIGURE 13.19<br />

TestMessagePanel uses MessagePanel <strong>to</strong> display four message panels.<br />

skip implementation?<br />

The rest of this section explains how <strong>to</strong> implement the MessagePanel class. Since you can<br />

use the class without knowing how it is implemented, you may skip the implementation if<br />

you wish.

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

Saved successfully!

Ooh no, something went wrong!