/* * File JavaExamples/Caviness/ScrollTest_1_1.java * Copyright BF Caviness * 04 November 1997 Version 1.0 * 25 Nov 1998 changes by E Kaltofen: Java 1.1 event model */ /** * This example demonstrates the use of the JDK component Scrollbar * and further methods for TextField components. It uses three scroll * bars in addition to three TextFields to specify the red, green, * and blue components of a color to be displayed. The text fields * and the scroll bars interact with each other. For example, when the * red scoll bar is changed, it changes the value of the red text field * as well as the color. When the red text field is changed, the * corresponding change is made in the setting of the red scroll bar. * The green and blue textfield/scrollbar pairs work in an analogous * fashion. */ import java.awt.*; import java.awt.event.*; public class ScrollTest_1_1C extends Frame{ // EK: has two Panels (tfp, sbp) and a Canvas (palette) // tfp (textfieldpanel) has three TextFields // sbp (scrollbarpanel) has three Scrollbars (which are painted in // an Panel holding the Label and the Scrollbar) // and layed out vertically private TextField redTF; // Text field variables private TextField greenTF; private TextField blueTF; private Scrollbar redSB; // Scroll bar variables private Scrollbar greenSB; private Scrollbar blueSB; private ColorCanvas palette;// Canvas for color palette; EK:local class def'd below public ScrollTest_1_1C( String title ){ super( title ); // Set frame title Panel tfp = new Panel(); // A panel to hold the TextFields redTF = addTextField( tfp,"Red" ); // EK: ScrollTest_1_1.addTextfield redTF.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { System.out.println("Textfield.actionPerformed() w e " + e ); int col = Integer.parseInt(redTF.getText()); redSB.setValue(col); palette.setColor(); } }); greenTF = addTextField( tfp,"Green" ); greenTF.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { System.out.println("Textfield.actionPerformed() w e " + e ); int col = Integer.parseInt(greenTF.getText()); greenSB.setValue(col); palette.setColor(); } }); blueTF = addTextField( tfp,"Blue" ); blueTF.addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { System.out.println("Textfield.actionPerformed() w e " + e ); int col = Integer.parseInt(blueTF.getText()); blueSB.setValue(col); palette.setColor(); } }); Panel sbp = new Panel(); // A panel to hold the Scrollbars sbp.setLayout( new BorderLayout() ); // EK: Container.setLayout redSB = addScrollbar( sbp,"Red","North" ); // EK: ScrollTest_1_1.addScrollbar redSB.addAdjustmentListener(new AdjustmentListener () { public void adjustmentValueChanged(AdjustmentEvent e) { System.out.println("Scrollbar.adjustmentValueChanged() w e " + e ); int col = e.getValue(); redTF.setText( "" + col ); palette.setColor(); } } ); greenSB = addScrollbar( sbp,"Green","Center" ); greenSB.addAdjustmentListener(new AdjustmentListener () { public void adjustmentValueChanged(AdjustmentEvent e) { System.out.println("Scrollbar.adjustmentValueChanged() w e " + e ); int col = e.getValue(); greenTF.setText( "" + col ); palette.setColor(); } } ); blueSB = addScrollbar( sbp,"Blue","South" ); blueSB.addAdjustmentListener(new AdjustmentListener () { public void adjustmentValueChanged(AdjustmentEvent e) { System.out.println("Scrollbar.adjustmentValueChanged() w e " + e ); int col = e.getValue(); blueTF.setText( "" + col ); palette.setColor(); } } ); setLayout( new BorderLayout() ); // Layout frame setSize( 600,300 ); // EK: Component.setSize add( tfp,"North" ); // Add text fields // EK: Container.add(Component,Object) palette = new ColorCanvas(); add( palette,"Center" ); add( sbp,"South" ); // Add scroll bars this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("ScrollTest.windowClosing() w e " + e ); dispose(); System.exit(0); } } ); }// end ScrollTest_1_1(String) /** * Two auxilary functions used by the constructor */ public TextField addTextField( Panel p, String label ){ p.add( new Label( label ) ); TextField t = new TextField( "255" ); p.add( t ); // EK: Container.add(...) return t; } public Scrollbar addScrollbar( Panel p, String label, String here ){ Scrollbar b = new Scrollbar( Scrollbar.HORIZONTAL,0,0,0,255 ); // EK: orientation,init,visible,min,max b.setValue( 255 ); // Set initial value; EK: Scrollbar.setValue Panel pair = new Panel(); // Panel to hold each label and // pair.add( new Label( label ) ); // scroll bar pair p.add( new Label( label ), here ); // scroll bar pair // pair.add( b ); // p.add( pair, here ); p.add( b, here ); return b; } public static void main( String arg[] ){ Frame f = new ScrollTest_1_1( "Color Mixer" ); f.show(); } class ColorCanvas extends Canvas{ private Color backGround; private int red; private int green; private int blue; public ColorCanvas(){ setColor( 255,255,255 ); } public void setColor( int r, int g, int b ){ backGround = new Color( r,g,b ); setBackground( backGround ); // Note that paint() does not need // EK: Component.setBackground // to be redefined as we are only repaint(); // changing the bkground color and } // repaint() redoes the background. public void setColor() { setColor(redSB.getValue(), greenSB.getValue(), blueSB.getValue()); } }// end class ColorCanvas }// end class ScrollTest_1_1