Clay Shell Version 6

Version 6 of the Clay Shell program is like the fifth version except its buttons are alive!

Demos

java -jar ".../Clay6.jar" -w 400 -h 200

Code

package clay6;

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) {
        super(title);
        establishColors();
        setBackground(color);
        setSize(size.width,size.height);
        addComponents();
        setVisible(true);
        addWindowListener(new CloseWindow());
    }
    
    private Color backgroundColor;
    private Color hilightColor;
    private Color lolightColor;

    private Button nb;
    private Button sb;
    private Button eb;
    private Button wb;
    private Button cb;
    private Button theButton;  // selection

    private void establishColors()
    {
        Color purplerose = new Color(210,180,200);
        hilightColor = purplerose;
        lolightColor = Color.white;
    }

    private void addComponents()
    {
        // establish the layout manager
        setLayout(new BorderLayout());
        // create five buttons
        nb = new Button("North");
        sb = new Button("South");
        eb = new Button("East");
        wb = new Button("West");
        cb = new Button("Center");
        // set backgrounds of buttons to white
        nb.setBackground(lolightColor);
        sb.setBackground(lolightColor);
        eb.setBackground(lolightColor);
        wb.setBackground(lolightColor);
        cb.setBackground(lolightColor);
        // add action listeners
        nb.addActionListener(this);
        sb.addActionListener(this);
        eb.addActionListener(this);
        wb.addActionListener(this);
        cb.addActionListener(this);
        // add the buttons to the frame
        add(nb,BorderLayout.NORTH);
        add(sb,BorderLayout.SOUTH);
        add(cb,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;
            // color the buttons
            nb.setBackground(lolightColor);
            sb.setBackground(lolightColor);
            wb.setBackground(lolightColor);
            eb.setBackground(lolightColor);
            cb.setBackground(lolightColor);
            theButton.setBackground(hilightColor);
        }
    }

    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. Why are the buttons declared local to the frame class in this version, rather than local to the add components method as in the previous version?
  3. What is an interface ?
  4. What is an event handler ?
  5. What is an event ?
  6. Where is the ActionListener packaged?
  7. What method(s) are you obligated to implement in a class which implements the ActionListener interface? How do you know? Provide specifications.
  8. What is an ActionEvent ?
  9. Is the actionPerformed method an example of an "event handler"?
  10. How do buttons (components, more generally) communicate action events to other components?
  11. Where does the registration of the event communication processes take place in this program?
  12. Describe, in detail, at a conceptual level, the behaviour of the particular event handler written in this program.
  13. Describe, at the "superficial signature" level, the methods and operators used within the event handler of this program.