import java.awt.*;
import java.awt.event.*;

        // Display a Canvas with a background Color that
        // can be manipulated. Allow the user to use the
        // deeper and brighter methods, as well as generating
        // random colours.
class RGBFrame extends Frame implements ActionListener {
    public RGBFrame(){
        final int width = 375, height = 150;
        setSize(width,height);
        setBackground(Color.white);

        // Add the canvas that will display the colour examples.
        Canvas canvas = getCanvas();
        canvas.setSize(width,2*height/3);
        // Start with a black background.
        canvas.setBackground(Color.black);
        add(canvas,"Center");

        // Add a label for a description of the current background colour
        // at the top of the frame.
        add(getLabel(),"North");
        describeColour();

        // Add a panel of buttons for user actions at the bottom
        // of the frame.
        Panel buttonPanel = new Panel();
        for(int i = 0; i < buttonLabels.length; i++){
            Button b = new Button(buttonLabels[i]);
            b.addActionListener(this);
            buttonPanel.add(b);
        }
        add(buttonPanel,"South");
    }

        // Act on a button press.
    public void actionPerformed(ActionEvent e){
        String command = e.getActionCommand();
        if((command == null) || command.equals("")){
            return;
        }
        else if(command.equals("")){
            return;
        }

        Canvas canvas = getCanvas();
        // Obtain the current background to manipulate.
        Color c = canvas.getBackground();

        if(command.equals(brighterString)){
            c = c.brighter();
        }
        else if(command.equals(darkerString)){
            c = c.darker();
        }
        else if(command.equals(randomString)){
            // Random number for RGB value.
            final int numColorValues = 256*256*256;
            c = new Color((int) (Math.random()*numColorValues));
        }
        else if(command.equals(quitString)){
            System.exit(0);
        }
        canvas.setBackground(c);
        describeColour();
        repaint();
    }

        // Extract the component colour values from the canvas's
        // background colour to create the text for the label.
    protected void describeColour(){
        Label label = getLabel();
        Color c = getCanvas().getBackground();
        if(c != null){
            label.setText("Red = "+c.getRed()+" Green = "+c.getGreen()+
                          " Blue = "+c.getBlue());
        }
        else{
            label.setText("");
        }
    }

    protected Canvas getCanvas(){
        return canvas;
    }

    protected Label getLabel(){
        return label;
    }

    private Canvas canvas = new Canvas();
    // Centre the text of the label
    private Label label = new Label("",Label.CENTER);
    // Strings for the button labels.
    private String brighterString = "Brighter", 
                   darkerString = "Darker",
                   randomString = "Random",
                   quitString = "Quit";
    private String[] buttonLabels = {
        brighterString, darkerString, randomString, quitString,
    };
}

