Clay Shell Version 10

Version 10 of the Clay Shell program is like the eighth version except that the button stub is replaced by a text field which is used to input commands textually.

Demos

java -jar ".../Clay10.jar" -w 400 -h 300

Code

package clay10;

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!>>>

    }
  
    private Color backgroundColor;
    private Color hilightColor;
    private Color lolightColor;

    private Button red;
    private Button yellow;
    private Button green;
    private Button random;
    private Button clear;
    private Button clean;
    private Button theButton;  // selection
    
    private TextArea ta;
    private Canvas pa;
    
    // panel to house the text area and the canvas
    private Panel content;
    // panel to house functionality (buttons)
    private Panel control;
    
    // text field into which commands will be typed
    private TextField input;

    private void establishColors() {

        <<<Just like in the previous version!>>>

    }

    private void addComponents() {
        // establish the layout manager
        setLayout(new BorderLayout());
        // create five 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 text area
        ta = new TextArea();
        // create a painting area
        pa = new Canvas();
        // create the text field
        input = new TextField();
        // create a panel to house content and add components
        content = new Panel();
        content.setLayout(new GridLayout(1,2));
        content.add(ta);
        content.add(pa);
        // create a control panel and add components
        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);    
        // set backgrounds of buttons to white
        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);
        // add action listeners
        red.addActionListener(this);
        yellow.addActionListener(this);
        green.addActionListener(this);
        random.addActionListener(this);
        clear.addActionListener(this);
        clean.addActionListener(this);
        input.addActionListener(this);
        // add the components to the frame
        add(control,BorderLayout.NORTH);
        add(input,BorderLayout.SOUTH);
        add(content,BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        String command = null;
        if ( source instanceof Button ) {
            theButton = (Button)source;
            command = theButton.getLabel();
        } else if ( source instanceof TextField ) {
            command = input.getText();
            input.setText("");
        }
        command = command.toLowerCase();
        if ( command.equalsIgnoreCase("Red") ) {
            ta.append(command + "\n");
            pa.setBackground(Color.red);
        } else if ( command.equalsIgnoreCase("Yellow") ) {
            ta.append(command + "\n");
            pa.setBackground(Color.yellow);
        } else if ( command.equalsIgnoreCase("Green") ) {
            ta.append(command + "\n");
            pa.setBackground(Color.green);
        } else if ( command.equalsIgnoreCase("Random") ) {
            ta.append(command + "\n");
            pa.setBackground(randomColor());
        } else if ( command.equalsIgnoreCase("Clear") ) {
            ta.append(command + "\n");
            ta.setText("");
        } else if ( command.equalsIgnoreCase("Clean") ) {
            ta.append(command + "\n");
            pa.setBackground(Color.white);
        } else {
            ta.append("### Clay error:  Unrecognizable command: " + command + "\n");
        }
    }

    private Color randomColor() {

        <<<Just like in the previous version!>>>

    }

    private void paintCanvas(String color) {

        <<<Just like in the previous version!>>>

    }

    class CloseWindow extends WindowAdapter {

        <<<Just like in the previous version!>>>

    }

}

Questions

  1. What is different between this version of the program and the previous one. (Identify all the differences in the code.)
  2. What is text field?