FileDocCategorySizeDatePackage
ColourChooser.javaAPI DocExample6059Sun Mar 29 21:45:14 BST 1998None

ColourChooser.java

// File: ColourChooser.java
// T Balls : March 1998
// A ColourChooser implemented as a modal Dialog
// Changes the background colour of the Frame that lanches it
// Scrollbars are used to adjust the colour which is displayed
// on the ColourChooser.  The ColourChooser may update the parent's
// background colour.  Cancelling the Dialog will restore the parent
// backgound colour - Closing will leave it changed.

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

public class ColourChooser extends Dialog 
                           implements AdjustmentListener, 
                                      ActionListener
{  public ColourChooser( Frame f, boolean m )
   {  super( f, "Colour Chooser", m );  // pass to parent.
      // save reference to parent
      parent = f;
      // and current background colour
      oldBkg = f.getBackground();
      
      // Controls and labels to add to West of Dialog
      Panel p = new Panel();
      p.setLayout( new BorderLayout() );
      
      // Labels
      Panel ppN = new Panel();
      ppN.setLayout( new GridLayout( 1, 3 ) );
      ppN.add( redL );
      ppN.add( greenL );
      ppN.add( blueL );
      p.add( "North", ppN );
      
      // Scrollbars
      Panel ppC = new Panel();
      ppC.setLayout( new GridLayout( 1, 3 ) );
      redSB.setValues( 127, 1, 0, 256 );
      redSB.setBlockIncrement( 8 );
      redSB.addAdjustmentListener( this );
      ppC.add( redSB );

      greenSB.setValues( 127, 1, 0, 256 );
      greenSB.setBlockIncrement( 8 );
      greenSB.addAdjustmentListener( this );
      ppC.add( greenSB );

      blueSB.setValues( 127, 1, 0, 256 );
      blueSB.setBlockIncrement( 8 );
      blueSB.addAdjustmentListener( this );
      ppC.add( blueSB );
      
      p.add( "Center", ppC );

      Panel ppS = new Panel();
      ppS.setLayout( new GridLayout( 1, 3 ) );
      ppS.add( new Label( "R", Label.CENTER ) );
      ppS.add( new Label( "G", Label.CENTER ) );
      ppS.add( new Label( "B", Label.CENTER ) );
      p.add( "South", ppS );
      
      add( "West", p );
      
      // Canvas that is used to show current colour
      disp.setSize( 50, 150 );
      add( "Center", disp );

      // buttons to control interaction with parent
      Panel buttons = new Panel();
      Button dismiss = new Button( "Close" );
      dismiss.addActionListener( this );
      buttons.add( dismiss );
      Button apply = new Button( "Apply" );
      apply.addActionListener( this );
      buttons.add( apply );
      Button cancel = new Button( "Cancel" );
      cancel.addActionListener( this );
      buttons.add( cancel );
      
      add( "South", buttons );
      
      // ensure that disp has corrent colour
      setColour();

      pack();
      setVisible( true );      
   }
   
   // Method called when any of the Scrollbars is altered
   public void adjustmentValueChanged( AdjustmentEvent e )
   {  if( e.getAdjustable() instanceof Scrollbar )
      // get the actual Scrollbar responsible for this change
      {  Scrollbar s = (Scrollbar)(e.getAdjustable() );
         int value = 255-e.getValue(); // 0 at top!
         if( s == redSB )
         {  red = value;
            redL.setText( "" + value );
         }
         if( s == greenSB )
         {  green = value;
            greenL.setText( "" + value );
         }
         if( s == blueSB )
         {  blue = value;
            blueL.setText( "" + value );
         }
         setColour(); // update disp
      }
   }
   
   // Method called by interaction Buttons
   public void actionPerformed( ActionEvent e )
   {  String label = e.getActionCommand();
      if( label.equals( "Close" ) )
      {  setVisible( false ); // disappear and cease
         dispose();   // release resources
      }
      if( label.equals( "Apply" ) )
      {  parent.setBackground( new Color( red, green, blue ) );
         parent.repaint(); // update parent
      }
      if( label.equals( "Cancel" ) )
      {  parent.setBackground( oldBkg ); // restore 
         parent.repaint(); // original parent colour
         setVisible( false );
         dispose();
      }
   }
   
   // update local display panel - local "View"
   private void setColour()
   {  Color c = new Color( red, green, blue );
      disp.setBackground( c );
      disp.repaint();
   }
   
   // local data
   private Frame parent;
   private Color oldBkg;
   private Canvas disp = new Canvas();
   private Label redL = new Label ( "128", Label.CENTER ); 
   private Label greenL = new Label ( "128", Label.CENTER ); 
   private Label blueL = new Label ( "128", Label.CENTER ); 
   private Scrollbar redSB = new Scrollbar( Scrollbar.VERTICAL );
   private Scrollbar greenSB = new Scrollbar( Scrollbar.VERTICAL );
   private Scrollbar blueSB = new Scrollbar( Scrollbar.VERTICAL );
   
   // the "Model"
   private int red = 127;
   private int green = 127;
   private int blue = 127;   
}

// Class to test the ColourChooser object
class TestColChoose extends Frame
{  public TestColChoose()
   {  setTitle( "Test Colour Chooser" );
      setSize( 300, 300 );
      Button colour = new Button( "Change Colour" );
      add( "South", colour );
      colour.setForeground( Color.black );
      colour.setBackground( Color.lightGray );
      colour.addActionListener( new ActionListener() {
         public void actionPerformed( ActionEvent e )
         {  new ColourChooser( TestColChoose.this, true );
            // First parameter needs scoping to get reference for
            // the enclosing Frame object
            // Change second parameter to false for non-modal Dialog
         }
      });
      
      addWindowListener( new WindowAdapter() {
         public void windowClosing( WindowEvent e )
         {  System.exit( 0 );
         }
      });
      setVisible( true );   
   }
   
   public static void main( String[] args )
   {  new TestColChoose();
   }
}