Clay Shell Version 8

Version 8 of the Clay Shell program is like the seventh version except that:

  1. the central region is now divided vertically into two parts. The left part is filled with the text area and the right part is filled with a canvas.
  2. the border buttons are relabeled Green, Red, Yellow, and Random.
  3. button functionality is now two-fold: the button name is displayed in the text area and the corresponding color is displayed in the canvas.

Demos

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

Code

package clay8;

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 nb;
    private Button sb;
    private Button eb;
    private Button wb;
    private Button theButton;  // selection
    
    private TextArea ta;
    private Canvas pa;
    
    // panel to house the text area and the canvas
    private Panel content;

    private void establishColors() {

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

    }

    private void addComponents() {
        // establish the layout manager
        setLayout(new BorderLayout());
        // create five buttons
        nb = new Button("Green");
        sb = new Button("Random");
        eb = new Button("Yellow");
        wb = new Button("Red");
        // create a text area
        ta = new TextArea();
        // create a painting area
        pa = new Canvas();
        // create a panel to house components and add them
        content = new Panel();
        content.setLayout(new GridLayout(1,2));
        content.add(ta);
        content.add(pa);
        // set backgrounds of buttons to white
        nb.setBackground(lolightColor);
        sb.setBackground(lolightColor);
        eb.setBackground(lolightColor);
        wb.setBackground(lolightColor);
        ta.setBackground(Color.white);
        pa.setBackground(Color.white);
        // add action listeners
        nb.addActionListener(this);
        sb.addActionListener(this);
        eb.addActionListener(this);
        wb.addActionListener(this);
        // add the buttons to the frame
        add(nb,BorderLayout.NORTH);
        add(sb,BorderLayout.SOUTH);
        add(content,BorderLayout.CENTER);
        add(eb,BorderLayout.EAST);
        add(wb,BorderLayout.WEST);
    }
    
    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());
            }
            // color the buttons
            nb.setBackground(lolightColor);
            sb.setBackground(lolightColor);
            wb.setBackground(lolightColor);
            eb.setBackground(lolightColor);
            theButton.setBackground(hilightColor);
        }
    }
    
    private Color randomColor() {
       int r = (int)(Math.random()*256);
       int g = (int)(Math.random()*256);
       int b = (int)(Math.random()*256);
       return new Color(r,g,b);
    }

    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 canvas?
  3. What is the panel used for?
  4. How does a grid layout manager do its job?