JavaApplet: Color Box



Here is the source code


imimport java.awt.*;
import java.applet.*;
import java.awt.event.*;

//the applet you will be using the ActionListener methods.
public class ColorBox extends Applet implements ActionListener

      Button okButton;
      CheckboxGroup radioGroup;
      Checkbox radio1;
      Checkbox radio2;
      Checkbox radio3;

         public void init()
            //we will use the FlowLayout
            setLayout(new FlowLayout());
            okButton = new Button("Colour Box");

            radioGroup = new CheckboxGroup();
            radio1 = new Checkbox("Red", radioGroup,false);
            radio2 = new Checkbox("Blue", radioGroup,false);
            radio3 = new Checkbox("Green", radioGroup,false);


            // Attach actions to the components

         // show the results of our actions
         public void paint(Graphics g)
            // If radio1 box is selected, radio1.getState() will return true and execute this
            if (radio1.getState()){
            // If it was not red we'll try if it is blue
            else if (radio2.getState()){
            // If it was not blue we'll try if it is green
            else if (radio3.getState()){
            // if non set to white
            // Now that the color is set you can get the text out the TextField
            g.fillRect (10, 100, 100, 200);
         // When the button is clicked this method will get automatically called
         // This is where you specify all actions.

         public void actionPerformed(ActionEvent click)
            // Here we will ask what component called this method
            if (click.getSource() == okButton)
               // So it was the okButton, then let's perform his actions
               // Let the applet perform Paint again.