/*
      Program: 4.13
      Programmer:  Brad Shedd
      Date:    May 22, 2006
      File:    ColorApplet.java
      Purpose: Changes background color according to the users input.
*/

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

public class ColorApplet extends Applet implements ActionListener
{
  
//declare variables
   int red, green, blue;
  
boolean done = false;

  
//constuct components
   Label redLabel = new Label("Enter the red value: ");
      TextField redField =
new TextField(10);
   Label greenLabel =
new Label("Enter the green value: ");
      TextField greenField =
new TextField(10);
   Label blueLabel =
new Label("Enter the blue value: ");
      TextField blueField =
new TextField(10);
   Button changeButton =
new Button("change");
   Label outputLabel =
new Label("Click the change button to see the new color.");

  
public void init()
   {
      add(redLabel);
      add(redField);
      add(greenLabel);
      add(greenField);
      add(blueLabel);
      add(blueField);
      add(changeButton);
      changeButton.addActionListener(
this);
      add(outputLabel);
   }
// end of init method

   public void actionPerformed(ActionEvent e)
      { 
        
while (!done)
         {
        
        
           
if (red<0 || red>255)
            { 
               redField.requestFocus();
              
throw new NumberFormatException();
            }
           
else
           
if (green<0 || green>255)
            {
               greenField.requestFocus();
              
throw new NumberFormatException();
            }
           
else
           
if (blue<0 || blue>255)
            {
               blueField.requestFocus();          
              
throw new NumberFormatException();

            }
           
else done = true;
           
         }
        
         red = Integer.parseInt(redField.getText());
         green = Integer.parseInt(greenField.getText());
         blue = Integer.parseInt(blueField.getText());
         Color color =
new Color(red, green, blue);
         setBackground(color);

      }
}

Homepage