Lab 8 problem 2 solution

 

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

public class gui extends JFrame {
// Declare check boxes
private JCheckBox jchkCentered, jchkBold, jchkItalic;

// Declare a combo box to hold font names
private JComboBox jcboFontName = new JComboBox();

// Declare a combo box to hold font sizes
private JComboBox jcboFontSize = new JComboBox();

// Font name
private String fontName = "SansSerif";

// Font style
private int fontStyle = Font.PLAIN;

// Font Size
private int fontSize = 12;

// Declare a panel for displaying message
private MessagePanel messagePanel
= new MessagePanel("Java is Cool");

/** Main method */
public static void main(String[] args) {
Exercise17_8 frame = new Exercise17_8();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}

/** Default constructor */
public gui() {
setTitle("Message Center");

// Set the background color of messagePanel
messagePanel.setBackground(Color.yellow);

// Find all available font names
GraphicsEnvironment e =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontnames = e.getAvailableFontFamilyNames();
for (int i = 0; i < fontnames.length; i++)
jcboFontName.addItem(fontnames[i]);
jcboFontName.setSelectedItem("" + fontName);

// Add font sizes into jcboFontSize
for (int i = 1; i <= 100; i++)
jcboFontSize.addItem("" + i);
jcboFontSize.setSelectedItem("" + fontSize);

// Hold font name label and combo box
JPanel p1 = new JPanel(new BorderLayout(10, 10));
p1.add(new JLabel("Font Name"), BorderLayout.WEST);
p1.add(jcboFontName, BorderLayout.CENTER);

// Hold font size label and combo box
JPanel p2 = new JPanel(new BorderLayout(10, 10));
p2.add(new JLabel("Font Size"), BorderLayout.WEST);
p2.add(jcboFontSize, BorderLayout.CENTER);

// Add p1 and p2 into p3
JPanel p3 = new JPanel(new BorderLayout(10, 10));
p3.setBorder(new EmptyBorder(10, 10, 10, 10));
p3.add(p1, BorderLayout.CENTER);
p3.add(p2, BorderLayout.EAST);

// Put three check boxes in panel p
JPanel p = new JPanel();
p.add(jchkCentered = new JCheckBox("Centered"));
p.add(jchkBold = new JCheckBox("Bold"));
p.add(jchkItalic = new JCheckBox("Italic"));

// Set keyboard mnemonics
jchkCentered.setMnemonic('C');
jchkBold.setMnemonic('B');
jchkItalic.setMnemonic('I');

// Place messagePanel, p3, and p in the frame
setLayout(new BorderLayout());
add(messagePanel, BorderLayout.CENTER);
add(p3, BorderLayout.NORTH);
add(p, BorderLayout.SOUTH);

// Register listeners on jcboFontName and jcboFontSize
jcboFontName.addItemListener(new Listener());
jcboFontSize.addItemListener(new Listener());

// Register listeners on jchkCentered, jchkBold, and jchkItalic
jchkCentered.addItemListener(new Listener());
jchkBold.addItemListener(new Listener());
jchkItalic.addItemListener(new Listener());
}

class Listener implements ItemListener {
/** Handle check box selection */
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == jcboFontName) {
fontName = (String)(jcboFontName.getSelectedItem());

// Set font for the message
messagePanel.setFont(new Font(fontName, fontStyle, fontSize));
}
else if (e.getSource() == jcboFontSize) {
fontSize = Integer.parseInt(
(String)(jcboFontSize.getSelectedItem()));

// Set font for the message
messagePanel.setFont(new Font(fontName, fontStyle, fontSize));
}
else if ((e.getSource() == jchkBold) ||
(e.getSource() == jchkItalic)) {
fontStyle = Font.PLAIN;

// Determine a font style
if (jchkBold.isSelected())
fontStyle = fontStyle + Font.BOLD;
if (jchkItalic.isSelected())
fontStyle = fontStyle + Font.ITALIC;

// Set font for the message
messagePanel.setFont(new Font(fontName, fontStyle, fontSize));
}
else if (e.getSource() == jchkCentered) {
if (jchkCentered.isSelected())
messagePanel.setCentered(true);
else
messagePanel.setCentered(false);
}
}
}
}

_____________________________

// MessagePanel.java: Display a message on a JPanel
import java.awt.Font;
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;

/** The interval for moving the message horizontally and vertically */
private int interval = 10;

/** Default constructor */
public MessagePanel() {
}

/** Constructor with a message parameter */
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;
repaint();
}

/** Return xCoordinator */
public int getXCoordinate() {
return xCoordinate;
}

/** Set a new xCoordinator */
public void setXCoordinate(int x) {
this.xCoordinate = x;
repaint();
}

/** Return yCoordinator */
public int getYCoordinate() {
return yCoordinate;
}

/** Set a new yCoordinator */
public void setYCoordinate(int y) {
this.yCoordinate = y;
repaint();
}

/** Return centered */
public boolean isCentered() {
return centered;
}

/** Set a new centered */
public void setCentered(boolean centered) {
this.centered = centered;
repaint();
}

/** Return interval */
public int getInterval() {
return interval;
}

/** Set a new interval */
public void setInterval(int interval) {
this.interval = interval;
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();

// Find the center location to display
int stringWidth = fm.stringWidth(message);
int stringAscent = fm.getAscent();
// Get the position of the leftmost character in the baseline
xCoordinate = getWidth() / 2 - stringWidth / 2;
yCoordinate = getHeight() / 2 + stringAscent / 2;
}

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

/** Move the message left */
public void moveLeft() {
xCoordinate -= interval;
repaint();
}

/** Move the message right */
public void moveRight() {
xCoordinate += interval;
repaint();
}

/** Move the message up */
public void moveUp() {
yCoordinate -= interval;
repaint();
}

/** Move the message down */
public void moveDown() {
yCoordinate -= interval;
repaint();
}

/** Override get method for preferredSize */
public Dimension getPreferredSize() {
return new Dimension(200, 30);
}
}