|
Version 12 of the Clay Shell program is like the tenth version except that the interpreter is "refactored" into a class.
|
This version should work just like the previous one. (Just run it and try it out!)
|
package clay12;
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!>>>
}
// interpreter
private Interpreter interpreter;
// 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() {
<<<Just like in the previous version!>>>
}
private void establishNorthernRegion() {
<<<Just like in the previous version!>>>
}
private void establishCentralRegion() {
<<<Just like in the previous version!>>>
}
private void establishSouthernRegion() {
<<<Just like in the previous version!>>>
}
private void setBackgrounds() {
<<<Just like in the previous version!>>>
}
private void establishListeners() {
<<<Just like in the previous version!>>>
}
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");
interpreter.interpret(command);
}
public TextArea ta() {
return ta;
}
public Canvas pa() {
return pa;
}
class CloseWindow extends WindowAdapter {
public CloseWindow() {
}
public void windowClosing(WindowEvent event) {
System.exit(0);
}
}
}
class Interpreter {
private ClayFrame clayFrame;
public Interpreter(ClayFrame cf) {
clayFrame = cf;
}
public void interpret(String command)
{
if ( command.equals("red") ) {
clayFrame.pa().setBackground(Color.red);
} else if ( command.equals("yellow") ) {
clayFrame.pa().setBackground(Color.yellow);
} else if ( command.equals("green") ) {
clayFrame.pa().setBackground(Color.green);
} else if ( command.equals("random") ) {
clayFrame.pa().setBackground(randomColor());
} else if ( command.equals("clear") ) {
clayFrame.ta().setText("");
} else if ( command.equals("clean") ) {
clayFrame.pa().setBackground(Color.white);
} else {
clayFrame.ta().append("### Clay error: Unrecognizable command: " + command + "\n");
}
}
public Color randomColor() {
<<<Just like in the previous version!>>>
}
}
|