// File: TestScrollbar.java (Module 11) // // Author: Rahul Simha // Created: November 4, 1998. // // Shows how to use scrollbars. 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()); // Three scrollbars and the canvas will // be added to a panel. Panel p = new Panel (); p.setLayout (new GridLayout (4,1)); // The canvas on which to display the result. c = new Canvas (); c.setBackground (Color.white); p.add (c); // A local Listener for the scrollbars. 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()); p.add (red_bar); green_bar = new Scrollbar (Scrollbar.HORIZONTAL, 0, 10, 0, 255); green_bar.addAdjustmentListener (new ColorAdjustmentListener()); p.add (green_bar); blue_bar = new Scrollbar (Scrollbar.HORIZONTAL, 0, 10, 0, 255); blue_bar.addAdjustmentListener (new ColorAdjustmentListener()); p.add (blue_bar); this.add (p, BorderLayout.CENTER); // A quit button for the application. Button quitb = new Button ("Quit"); quitb.setBackground (Color.red); quitb.setFont (new Font ("Serif", Font.PLAIN | Font.BOLD, 15)); 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 (); } } public class TestScrollbar { public static void main (String[] argv) { NewFrame nf = new NewFrame (500, 300); } }