import java.awt.*;
import javax.swing.*;
public class TestMessagePanel extends JFrame {
public TestMessagePanel() {
MessagePanel messagePanel1 = new MessagePanel("Welcome to Java");
MessagePanel messagePanel2 = new MessagePanel("Java is fun");
MessagePanel messagePanel3 = new MessagePanel("Java is cool");
MessagePanel messagePanel4 = new MessagePanel("I love Java");
messagePanel1.setFont(new Font("SansSerif", Font.ITALIC, 20));
messagePanel2.setFont(new Font("Courier", Font.BOLD, 20));
messagePanel3.setFont(new Font("Times", Font.ITALIC, 20));
messagePanel4.setFont(new Font("Californian FB", Font.PLAIN, 20));
messagePanel1.setBackground(Color.RED);
messagePanel2.setBackground(Color.CYAN);
messagePanel3.setBackground(Color.GREEN);
messagePanel4.setBackground(Color.WHITE);
messagePanel1.setCentered(true);
setLayout(new GridLayout(2, 2));
add(messagePanel1);
add(messagePanel2);
add(messagePanel3);
add(messagePanel4);
}
public static void main(String[] args) {
TestMessagePanel frame = new TestMessagePanel();
frame.setTitle("TestMessagePanel");
frame.setSize(300, 200);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.awt.FontMetrics;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class MessagePanel extends JPanel {
/** The message to be displayed */
private String message = "Welcome to Java";
/** The x-coordinate where the message is displayed */
private int xCoordinate = 20;
/** The y-coordinate where the message is displayed */
private int yCoordinate = 20;
/** Indicate whether the message is displayed in the center */
private boolean centered = false;
/** The interval for moving the message horizontally and
* vertically */
private int interval = 10;
/** Construct with default properties */
public MessagePanel() {
}
/** Construct with default panel with a specified message */
public MessagePanel(String message) {
this.message = message;
}
/** Return message */
public String getMessage() {
return message;
}
/** Set a new message */
public void setMessage(String message) {
this.message = message;
super.repaint();
}
/** Return xCoordiante */
public int getXCoordinate() {
return xCoordinate;
}
/** Return yCoordinate */
public int getYCoordinate() {
return yCoordinate;
}
/** Set a new xCoordinate */
public void setXCoordinate(int xCoordinate) {
this.xCoordinate = xCoordinate;
super.repaint();
}
/** Set a new yCoordinate */
public void setYCoordinate(int yCoordinate) {
this.yCoordinate = yCoordinate;
super.repaint();
}
/** Return centered */
public boolean isCentered() {
return centered;
}
/** Set a new Centered */
public void setCentered(boolean centered) {
this.centered = centered;
super.repaint();
}
/** Return interval */
public int getInterval() {
return interval;
}
/** Set a new interval */
public void setInterval(int interval) {
this.interval = interval;
}
/** Move the message left */
public void moveLeft() {
xCoordinate -= interval;
super.repaint();
}
/** Move the message right */
public void moveRight() {
xCoordinate += interval;
super.repaint();
}
/** Move the message up */
public void moveUp() {
yCoordinate -= interval;
super.repaint();
}
/** Move the message down */
public void moveDown() {
yCoordinate += interval;
super.repaint();
}
/** Paint the message */
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(centered) {
// Get font metrics for the current font
FontMetrics fm = g.getFontMetrics(g.getFont());
// Get the position of the leftmost character in the baseline
xCoordinate = getWidth() / 2 - fm.stringWidth(message) / 2;
yCoordinate = getHeight() / 2 + fm.getAscent() / 2;
}
g.drawString(message, xCoordinate, yCoordinate);
}
public Dimension getPreferredSize() {
return new Dimension(200, 30);
}
}
import javax.swing.*;
public class TestMessagePanel extends JFrame {
public TestMessagePanel() {
MessagePanel messagePanel1 = new MessagePanel("Welcome to Java");
MessagePanel messagePanel2 = new MessagePanel("Java is fun");
MessagePanel messagePanel3 = new MessagePanel("Java is cool");
MessagePanel messagePanel4 = new MessagePanel("I love Java");
messagePanel1.setFont(new Font("SansSerif", Font.ITALIC, 20));
messagePanel2.setFont(new Font("Courier", Font.BOLD, 20));
messagePanel3.setFont(new Font("Times", Font.ITALIC, 20));
messagePanel4.setFont(new Font("Californian FB", Font.PLAIN, 20));
messagePanel1.setBackground(Color.RED);
messagePanel2.setBackground(Color.CYAN);
messagePanel3.setBackground(Color.GREEN);
messagePanel4.setBackground(Color.WHITE);
messagePanel1.setCentered(true);
setLayout(new GridLayout(2, 2));
add(messagePanel1);
add(messagePanel2);
add(messagePanel3);
add(messagePanel4);
}
public static void main(String[] args) {
TestMessagePanel frame = new TestMessagePanel();
frame.setTitle("TestMessagePanel");
frame.setSize(300, 200);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.awt.FontMetrics;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class MessagePanel extends JPanel {
/** The message to be displayed */
private String message = "Welcome to Java";
/** The x-coordinate where the message is displayed */
private int xCoordinate = 20;
/** The y-coordinate where the message is displayed */
private int yCoordinate = 20;
/** Indicate whether the message is displayed in the center */
private boolean centered = false;
/** The interval for moving the message horizontally and
* vertically */
private int interval = 10;
/** Construct with default properties */
public MessagePanel() {
}
/** Construct with default panel with a specified message */
public MessagePanel(String message) {
this.message = message;
}
/** Return message */
public String getMessage() {
return message;
}
/** Set a new message */
public void setMessage(String message) {
this.message = message;
super.repaint();
}
/** Return xCoordiante */
public int getXCoordinate() {
return xCoordinate;
}
/** Return yCoordinate */
public int getYCoordinate() {
return yCoordinate;
}
/** Set a new xCoordinate */
public void setXCoordinate(int xCoordinate) {
this.xCoordinate = xCoordinate;
super.repaint();
}
/** Set a new yCoordinate */
public void setYCoordinate(int yCoordinate) {
this.yCoordinate = yCoordinate;
super.repaint();
}
/** Return centered */
public boolean isCentered() {
return centered;
}
/** Set a new Centered */
public void setCentered(boolean centered) {
this.centered = centered;
super.repaint();
}
/** Return interval */
public int getInterval() {
return interval;
}
/** Set a new interval */
public void setInterval(int interval) {
this.interval = interval;
}
/** Move the message left */
public void moveLeft() {
xCoordinate -= interval;
super.repaint();
}
/** Move the message right */
public void moveRight() {
xCoordinate += interval;
super.repaint();
}
/** Move the message up */
public void moveUp() {
yCoordinate -= interval;
super.repaint();
}
/** Move the message down */
public void moveDown() {
yCoordinate += interval;
super.repaint();
}
/** Paint the message */
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(centered) {
// Get font metrics for the current font
FontMetrics fm = g.getFontMetrics(g.getFont());
// Get the position of the leftmost character in the baseline
xCoordinate = getWidth() / 2 - fm.stringWidth(message) / 2;
yCoordinate = getHeight() / 2 + fm.getAscent() / 2;
}
g.drawString(message, xCoordinate, yCoordinate);
}
public Dimension getPreferredSize() {
return new Dimension(200, 30);
}
}