import java.awt.*; class Palette extends Frame { private Color colour; // indicates current color selected -> used by whiteboard /* * This class creates the artist palette of color that one * can use on the whiteboard. */ public Palette() { /* * This sets the default color to black and * makes the background color of the frame * to be yellow */ colour = Color.black; setBackground( Color.yellow ); /* * This section declares and creates a button * for each of the colors we are going to * implement */ Button blue_button = new Button("Blue"); Button magenta_button = new Button("Magenta"); Button red_button = new Button("Red"); Button green_button = new Button("Green"); Button black_button = new Button("Black"); Button gray_button = new Button("Gray"); Button yellow_button = new Button("Yellow"); Button orange_button = new Button("Orange"); Button cyan_button = new Button("Cyan"); Button pink_button = new Button("Pink"); /* * use grid layout to force buttons into 2 column * this makes them appear vertical */ setLayout( new GridLayout( 5, 2 ) ); /* this section is to set the buttons to their * respective colors so the button displayed * is the color that the button says it is */ blue_button.setBackground(Color.blue); magenta_button.setBackground(Color.magenta); red_button.setBackground(Color.red); green_button.setBackground(Color.green); black_button.setBackground(Color.black); gray_button.setBackground(Color.gray); yellow_button.setBackground(Color.yellow); orange_button.setBackground(Color.orange); cyan_button.setBackground(Color.cyan); pink_button.setBackground(Color.pink); /* * This section of code adds /displays each button * inside the color palette frame */ add( blue_button ); add( magenta_button ); add( red_button ); add( green_button ); add( black_button ); add( gray_button ); add( yellow_button ); add( orange_button ); add( cyan_button ); add( pink_button ); } public boolean action( Event evt, Object arg ) { if( "Blue".equals( arg ) ) { set_palette_Color( Color.blue ); } if( "Magenta".equals( arg ) ) { set_palette_Color( Color.magenta ); } if( "Red".equals( arg ) ) { set_palette_Color( Color.red ); } if( "Black".equals( arg ) ) { set_palette_Color( Color.black ); } if( "Gray".equals( arg ) ) { set_palette_Color( Color.gray ); } if( "Green".equals( arg ) ) { set_palette_Color( Color.green ); } if( "Yellow".equals( arg ) ) { set_palette_Color( Color.yellow ); } if( "Orange".equals( arg ) ) { set_palette_Color( Color.orange ); } if( "Pink".equals( arg ) ) { set_palette_Color( Color.pink ); } if( "Cyan".equals( arg ) ) { set_palette_Color( Color.cyan ); } return( true ); // event won't cascade backwards in object hierarchy } public Color getColor() { return( colour ); } public void set_palette_Color(Color c) { System.out.println( "The color has been set....\n" ); colour = c; } }
Written By Graham L. Mehl
Last Modified on April 23, 1996
© Villanova University