Clay Shell Version 9

Version 9 of the Clay Shell program is like the eighth version except that:

  1. additional functionality is added to clear the text field and to clean the canvas.
  2. the button functionality is consolidated into the northern region.
  3. the east and west regions are dropped.
  4. a "stub" button is placed into the southern region.

Demos

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

Code

package clay9;

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 Button comingSoon; // stub for something to come
    
    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;

    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");
        comingSoon = new Button("Coming soon ...");
        // create a text area
        ta = new TextArea();
        // create a painting area
        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);
        // 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);
        comingSoon.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);
        // add the components to the frame
        add(control,BorderLayout.NORTH);
        add(comingSoon,BorderLayout.SOUTH);
        add(content,BorderLayout.CENTER);
    }
    
    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if ( source instanceof Button ) {
            theButton = (Button)source;
            String theLabel = theButton.getLabel();
            ta.append(theLabel + "\n");
            if ( theLabel.equals("Red")) {
                pa.setBackground(Color.red);
            } else if ( theLabel.equals("Yellow")) {
                pa.setBackground(Color.yellow);
            } else if ( theLabel.equals("Green")) {
                pa.setBackground(Color.green);
            } else if ( theLabel.equals("Random")) {
                pa.setBackground(randomColor());
            } else if ( theLabel.equals("Clear")) {
                ta.setText("");
            } else if ( theLabel.equals("Clean")) {
                pa.setBackground(Color.white);
            }
            // color the buttons
            red.setBackground(lolightColor);
            yellow.setBackground(lolightColor);
            green.setBackground(lolightColor);
            random.setBackground(lolightColor);
            clear.setBackground(lolightColor);
            clean.setBackground(lolightColor);
            theButton.setBackground(hilightColor);
        }
    }
    
    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 a stub?
  3. How does a flow layout manager do its job?