|
Version 13 of the Clay Shell program is like the previous version except that button/text functionality is augmented to include a command called quote which displays a random quote.
|
java -jar ".../Clay13.jar" -s medium -c random

|
/*
* Main.java
*
* Created on May 31, 2006, 10:56 PM
*/
package clay13;
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 quote;
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() {
// create buttons
quote = new Button("Quote");
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(quote);
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() {
<<<Just like in the previous version!>>>
}
private void establishSouthernRegion() {
<<<Just like in the previous version!>>>
}
private void setBackgrounds() {
quote.setBackground(lolightColor);
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() {
quote.addActionListener(this);
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) {
<<<Just like in the previous version!>>>
}
public TextArea ta() {
return ta;
}
public Canvas pa() {
return pa;
}
class CloseWindow extends WindowAdapter {
<<<Just like in the previous version!>>>
}
}
class Interpreter {
private ClayFrame clayFrame;
public Interpreter(ClayFrame cf) {
clayFrame = cf;
establishQuotes();
}
private String[] quotes;
private void establishQuotes()
{
String q0 = "The limits of my language are the limits " +
"of my world.\n[Ludwig Witgenstein]";
String q1 = "If I could say it, I wouldn't have to " +
"dance it.\n[Isadora Duncon]";
String q2 = "The Brain - is wider than the Sky\nFor - " +
"put them side by side\nThe one the other will " +
"contain\nWith ease - and You - beside\n[Emily " +
"Dickinson]";
quotes = new String[3];
quotes[0] = q0;
quotes[1] = q1;
quotes[2] = q2;
}
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 if ( command.equals("quote") ) {
interpretQuoteCommand();
} else {
clayFrame.ta().append("### Clay error: Unrecognizable command: " + command + "\n");
}
}
private void interpretQuoteCommand() {
int rn = (int)(Math.random()*quotes.length);
String quote = quotes[rn];
clayFrame.ta().append(quote + "\n");
}
public Color randomColor() {
<<<Just like in the previous version!>>>
}
}
|