|
Version 11 of the Clay Shell program is like the tenth version except that the GUI code has been significantly reorganized.
|
This version should work just like the previous one. (Just run it and try it out!)
|
package clay11;
import java.awt.*;
import java.awt.event.*;
public class Main {
<<<Just like in the previous version!>>>
}
class ClayFrame extends Frame implements ActionListener {
public ClayFrame(String title, Color color, Dimension size) {
<<<Just like in the previous version!>>>
}
// colors
private Color backgroundColor;
private Color hilightColor;
private Color lolightColor;
// norther region components
private Panel control;
private Button red;
private Button yellow;
private Button green;
private Button random;
private Button clear;
private Button clean;
private Button theButton; // selection
// central region components
private Panel content;
private TextArea ta;
private Canvas pa;
// southern region components
private TextField input;
private void establishColors() {
<<<Just like in the previous version!>>>
}
private void addComponents() {
// establish the layout manager
setLayout(new BorderLayout());
establishNorthernRegion();
establishSouthernRegion();
establishCentralRegion();
setBackgrounds();
establishListeners();
}
private void establishNorthernRegion() {
// create buttons
red = new Button("Red");
yellow = new Button("Yellow");
green = new Button("Green");
random = new Button("Random");
clear = new Button("Clear");
clean = new Button("Clean");
// create a panel and drop buttons into it
control = new Panel();
control.setLayout(new FlowLayout());
control.add(red);
control.add(yellow);
control.add(green);
control.add(random);
control.add(clear);
control.add(clean);
// add control panel to northern region
add(control,BorderLayout.NORTH);
}
private void establishCentralRegion() {
// Create content components
ta = new TextArea();
pa = new Canvas();
// create a panel to house content and add components
content = new Panel();
content.setLayout(new GridLayout(1,2));
content.add(ta);
content.add(pa);
// add control panel to central region
add(content,BorderLayout.CENTER);
}
private void establishSouthernRegion() {
// create the text field
input = new TextField();
// add to southern region
add(input,BorderLayout.SOUTH);
}
private void setBackgrounds() {
red.setBackground(lolightColor);
yellow.setBackground(lolightColor);
green.setBackground(lolightColor);
random.setBackground(lolightColor);
clear.setBackground(lolightColor);
clean.setBackground(lolightColor);
input.setBackground(lolightColor);
ta.setBackground(Color.white);
pa.setBackground(Color.white);
}
private void establishListeners() {
red.addActionListener(this);
yellow.addActionListener(this);
green.addActionListener(this);
random.addActionListener(this);
clear.addActionListener(this);
clean.addActionListener(this);
input.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
String command = null;
if ( source instanceof Button ) {
theButton = (Button)source;
String theLabel = theButton.getLabel();
command = theLabel;
} else if ( source instanceof TextField ) {
command = input.getText();
input.setText("");
}
command = command.toLowerCase();
ta.append(command + "\n");
interpret(command);
}
private void interpret(String command) {
if ( command.equals("red") ) {
pa.setBackground(Color.red);
} else if ( command.equals("yellow") ) {
pa.setBackground(Color.yellow);
} else if ( command.equals("green") ) {
pa.setBackground(Color.green);
} else if ( command.equals("random") ) {
pa.setBackground(randomColor());
} else if ( command.equals("clear") ) {
ta.setText("");
} else if ( command.equals("clean") ) {
pa.setBackground(Color.white);
} else {
ta.append("### Clay error: Unrecognizable command: " + command + "\n");
}
}
private Color randomColor() {
<<<Just like in the previous version!>>>
}
class CloseWindow extends WindowAdapter {
<<<Just like in the previous version!>>>
}
}
|