1.        package messagepaneldemo;

 

2.        import java.awt.Font;

3.        import java.awt.FontMetrics;

4.        import java.awt.Dimension;

5.        import java.awt.Graphics;

6.        import javax.swing.JPanel;

 

7.        public class MessagePanel extends JPanel {

8.             private String message = "Welcome to Java";

9.             private int xCoordinate = 20;

10.                       private int yCoordinate = 20;

11.                       private boolean centered;

 

12.                       /**Default constructor*/

13.                       public MessagePanel() {

14.                       }

 

15.                       /**Constructor with a message parameter*/

16.                       public MessagePanel(String message) {

17.                           this.message = message;

18.                       }

 

19.                       public String getMessage() {

20.                           return message;

21.                       }

 

22.                       public void setMessage(String message) {

23.                           this.message = message;

24.                           repaint();

25.                       }

 

26.                       public int getXCoordinate() {

27.                           return xCoordinate;

28.                       }

 

29.                       public void setXCoordinate(int x) {

30.                           this.xCoordinate = x;

31.                       }

 

32.                       public int getYCoordinate() {

33.                           return yCoordinate;

34.                       }

 

35.                       public void setYCoordinate(int y) {

36.                           this.yCoordinate = y;

37.                       }

 

38.                       public boolean isCentered() {

39.                           return centered;

40.                       }

 

41.                       public void setCentered(boolean centered) {

42.                           this.centered = centered;

43.                       }

 

44.                       public void paintComponent(Graphics g) {

45.                           super.paintComponent(g);

 

46.                           if (centered) {

47.                             // Get font metrics for the current font

48.                             FontMetrics fm = g.getFontMetrics();

 

49.                             // Find the center location to display

50.                             int w = fm.stringWidth(message);  // Get the string width

51.                             // Get the string height, from top to the baseline

52.                             int h = fm.getAscent();

53.                             xCoordinate = getWidth()/2-w/2;

54.                             yCoordinate = getHeight()/2+h/2;

55.                          }

 

56.                          g.drawString(message, xCoordinate, yCoordinate);

57.                       }

 

58.                       /**Override get method for preferredSize*/

59.                       public Dimension getPreferredSize() {

60.                           return new Dimension(200, 100);

61.                       }

 

62.                       /**Override get method for minimumSize*/

63.                       public Dimension getMinimumSize() {

64.                           return new Dimension(200, 100);

65.                       }

 

66.                       /**Move the message in the pane to the left*/

67.                       public void moveLeft() {

68.                           if (getXCoordinate() > 10) {

69.                           // Shift the message to the left

70.                           setXCoordinate(getXCoordinate()-10);

71.                           repaint();

72.                         }

73.                       }

 

74.                       /**Move the message in the panel to the right*/

75.                       public void moveRight() {

76.                           if (getXCoordinate() < getSize().width - 20) {

77.                           // Shift the message to the right

78.                           setXCoordinate(getXCoordinate()+10);

79.                           repaint();

80.                          }

81.                       }

82.                       }