// File: TestScrollbar2.java (Module 11) // // Author: Rahul Simha // Created: November 4, 1998. // // Demonstrates keyboard events. import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; class NewFrame extends Frame { // Data. Scrollbar // One scrollbar for each color. red_bar, blue_bar, green_bar; int // Intensity value for each color. red_value = 0, blue_value = 0, green_value = 0; Canvas c; // To display the mixed color. // Constructor. public NewFrame (int width, int height) { this.setTitle ("RGB combination"); this.setResizable (true); this.setBackground (Color.cyan); this.setSize (width, height); // this.setLayout (new BorderLayout()); Panel p = new Panel (); p.setLayout (new GridLayout (4,1)); c = new Canvas (); c.setBackground (Color.white); // A KeyListener for the canvas. c.addKeyListener (getKeyListener()); p.add (c); // A local class: class ColorAdjustmentListener implements AdjustmentListener { public void adjustmentValueChanged (AdjustmentEvent a) { adjust (); } } red_bar = new Scrollbar (Scrollbar.HORIZONTAL, 0, 10, 0, 255); red_bar.addAdjustmentListener (new ColorAdjustmentListener()); // A KeyListener for the scrollbar. red_bar.addKeyListener (getKeyListener()); p.add (red_bar); green_bar = new Scrollbar (Scrollbar.HORIZONTAL, 0, 10, 0, 255); green_bar.addAdjustmentListener (new ColorAdjustmentListener()); // A KeyListener for the scrollbar. green_bar.addKeyListener (getKeyListener()); p.add (green_bar); blue_bar = new Scrollbar (Scrollbar.HORIZONTAL, 0, 10, 0, 255); blue_bar.addAdjustmentListener (new ColorAdjustmentListener()); // A KeyListener for the scrollbar. blue_bar.addKeyListener (getKeyListener()); p.add (blue_bar); this.add (p, BorderLayout.CENTER); Button quitb = new Button ("Quit"); quitb.setBackground (Color.red); quitb.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15)); quitb.addKeyListener (getKeyListener()); quitb.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { System.exit (0); } } ); this.add (quitb, BorderLayout.SOUTH); // Finally, show the frame. this.setVisible (true); } // No-parameter constructor. public NewFrame () { this (500, 300); } // Create the composite color. public void adjust () { // Get the new values. red_value = red_bar.getValue(); green_value = green_bar.getValue (); blue_value = blue_bar.getValue (); // Set the color in each scrollbar. red_bar.setBackground (new Color (red_value, 0, 0)); green_bar.setBackground (new Color (0, green_value, 0)); blue_bar.setBackground (new Color (0, 0, blue_value)); // Create the composite color. Color new_color = new Color (red_value, green_value, blue_value); c.setBackground (new_color); c.repaint (); } // Create a KeyListener to be used everywhere. KeyListener getKeyListener () { return new KeyAdapter () { public void keyPressed (KeyEvent k) { if (k.getKeyCode() == KeyEvent.VK_Q) // "q" for quit. System.exit (0); else if (k.getKeyCode() == KeyEvent.VK_LEFT) { // Left arrow. red_bar.setValue (Math.max (0, red_value-10)); adjust (); } else if (k.getKeyCode() == KeyEvent.VK_RIGHT) { // Right arrow. red_bar.setValue (Math.min (255, red_value+10)); adjust (); } } }; } } public class TestScrollbar2 { public static void main (String[] argv) { NewFrame nf = new NewFrame (500, 300); } }