// This was project #6 by Christopher Sharp in the CS335 class of
// Spring 04 Semester at the University of Arizona.
// March 5, 2004 (Previously Feb 27) - 6_David_Sharp_Chris
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// The main method is in this class. See RectanglesPanel for
// the other half of the program.
// This draws a checkpoint with adjustable rectangles that
// colored and resized according to the specifications for
// assignment #6.
class RectanglesFrame extends JFrame {
public static final int DEFAULT_WIDTH = 425;
public static final int DEFAULT_HEIGHT = 425;
public static final int DEFAULT_ROWS = 8;
public static final int DEFAULT_COLUMNS = 8;
public static final int DEFAULT_TEXTLEN = 3;
private int myRows,myCols;
private JTextField myXtext,myYtext;
private JLabel myLabel;
private RectanglesPanel myRectangles;
private JCheckBox myCheckBox;
public RectanglesFrame() {
// Set up labels, initial and default values.
setTitle("CS335 colored rectangles playground");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
myRows = DEFAULT_ROWS;
myCols = DEFAULT_COLUMNS;
Container contentPane = getContentPane();
JPanel p = new JPanel();
contentPane.add(p, BorderLayout.SOUTH);
myRectangles = new RectanglesPanel(myRows, myCols);
myRectangles.setBackground(Color.WHITE);
contentPane.add(myRectangles);
myXtext = new JTextField(String.valueOf(myRows),DEFAULT_TEXTLEN);
myYtext = new JTextField(String.valueOf(myCols),DEFAULT_TEXTLEN);
myCheckBox = new JCheckBox("",true);
p.add(myCheckBox);
myLabel = new JLabel("show grid lines");
p.add(myLabel);
myLabel = new JLabel("rows");
p.add(myLabel);
p.add(myXtext);
myLabel = new JLabel("columns");
p.add(myLabel);
p.add(myYtext);
// Checkbox is initially selected.
myCheckBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
myRectangles.drawRectangles(myCheckBox.isSelected());
}
});
// Set up initial values;
myRectangles.drawRectangles(true);
myRectangles.setMouseXY(-1, -1);
myRectangles.setColor(Color.RED);
JPanel q = new JPanel();
contentPane.add(q, BorderLayout.NORTH);
ButtonGroup radio = new ButtonGroup();
addRadioButton("Green", false, radio, q, Color.GREEN);
addRadioButton("Blue", false, radio, q, Color.BLUE);
addRadioButton("Red", true, radio, q, Color.RED);
// Action listener for textbox selecting rows.
myXtext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int i;
try {
i = Integer.parseInt(myXtext.getText());
if (i < 1) // Ignore values < 1.
return;
myRows = i;
myRectangles.changeRows(myRows);
}
// Ignore a non-numerical string.
catch (NumberFormatException e) {}
}
});
// Action listener for textbox selecting columns.
myYtext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int i;
try {
i = Integer.parseInt(myYtext.getText());
if (i < 1)
return; // Ignore values < 1.
myCols = i;
myRectangles.changeCols(myCols);
}
// Ignore a non-numerical string.
catch (NumberFormatException e) {}
}
});
// Focus listener for textbox selecting rows.
myXtext.addFocusListener(new FocusListener() {
public void focusLost(FocusEvent event) {
int i;
try {
i = Integer.parseInt(myXtext.getText());
if (i < 1) // Ignore values < 1.
return;
myRows = i;
myRectangles.changeRows(myRows);
}
// Ignore a non-numerical string.
catch (NumberFormatException e) {}
}
public void focusGained(FocusEvent event){}
});
// Focus listener for textbox selecting columns.
myYtext.addFocusListener(new FocusListener() {
public void focusLost(FocusEvent event) {
int i;
try {
i = Integer.parseInt(myYtext.getText());
if (i < 1)
return; // Ignore values < 1.
myCols = i;
myRectangles.changeCols(myCols);
}
// Ignore a non-numerical string.
catch (NumberFormatException e) {}
}
public void focusGained(FocusEvent event){}
});
// Pass mouse X and Y when it has been clicked.
myRectangles.addMouseListener(new MouseClicked());
}
// Action listener for the three radio buttons.
private void addRadioButton(String text, boolean selected,
ButtonGroup group, JPanel panel, final Color color) {
JRadioButton button = new JRadioButton(text, selected);
panel.add(button);
group.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
myRectangles.setColor(color);
}
});
}
// Get X and Y when mouse has been clicked,
private class MouseClicked extends MouseAdapter {
public void mouseClicked(MouseEvent event) {
myRectangles.setMouseXY(event.getX(), event.getY());
}
}
// Main method.
public static void main(String[] args) {
RectanglesFrame frame = new RectanglesFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}