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

ColourChooser

public class ColourChooser extends Dialog implements ActionListener, AdjustmentListener

Fields Summary
private Frame
parent
private Color
oldBkg
private Canvas
disp
private Label
redL
private Label
greenL
private Label
blueL
private Scrollbar
redSB
private Scrollbar
greenSB
private Scrollbar
blueSB
private int
red
private int
green
private int
blue
Constructors Summary
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 );      
   
Methods Summary
public voidactionPerformed(java.awt.event.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();
      }
   
public voidadjustmentValueChanged(java.awt.event.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
      }
   
private voidsetColour()

  Color c = new Color( red, green, blue );
      disp.setBackground( c );
      disp.repaint();