/* * File $CLASSHOME/example-progs/scrollBar/ScrollTest.java * Copyright BF Caviness * 04 November 1997 Version 1.0 * 25 Nov 1998 changes by E Kaltofen */ /** * 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.*; public class ScrollTest 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( String title ){ super( title ); // Set frame title Panel tfp = new Panel(); // A panel to hold the TextFields redTF = addTextField( tfp,"Red" ); // EK: ScrollTest.addTextfield greenTF = addTextField( tfp,"Green" ); blueTF = addTextField( tfp,"Blue" ); Panel sbp = new Panel(); // A panel to hold the Scrollbars sbp.setLayout( new BorderLayout() ); // EK: Container.setLayout redSB = addScrollbar( sbp,"Red","North" ); // EK: ScrollTest.addScrollbar greenSB = addScrollbar( sbp,"Green","Center" ); blueSB = addScrollbar( sbp,"Blue","South" ); 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 }// end ScrollTest(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,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 pair.add( b ); p.add( pair, here ); return b; } /** * Handles events on the TextFields. Overrides Component.action(). * This method is deprecated in JDK 1.1. Here we are using the Java * 1.0 event handling paradigm. */ public boolean action( Event e, Object inputStr ){ if ( e.target instanceof TextField ){ System.out.println( "ScrollTest.action() w e = " + e + ", inputStr = " + inputStr ); int r = Integer.parseInt( redTF.getText() ); redSB.setValue( r ); // Set red scroll bar int g = Integer.parseInt( greenTF.getText() ); greenSB.setValue( g ); // Set green scroll bar int b = Integer.parseInt( blueTF.getText() ); blueSB.setValue( b ); // Set blue scroll bar palette.setColor( r,g,b ); // EK: repaints return true; } return false; } /** * Handles events on the Scrollbars. Overrides Component.handleEvent(). * We are using the deprecated Java 1.0 event handling paradigm. */ public boolean handleEvent( Event e ){ // EK: catch Toolkit "close" if ( e.target == this ) // catch Toolkit actions on Frame { System.out.println( "ScrollTest.handleEvent() w e = " + e ); if ( e.id == Event.WINDOW_DESTROY ) { this.dispose(); System.exit(0); } } int r,g,b; switch( e.id ){ case Event.SCROLL_LINE_UP: case Event.SCROLL_LINE_DOWN: case Event.SCROLL_ABSOLUTE: // EK: don't track every event System.out.println( "ScrollTest.handleEvent() w e = " + e ); r = redSB.getValue(); redTF.setText( "" + r ); // Set red textf // EK: TextField.setText g = greenSB.getValue(); greenTF.setText( "" + g ); b = blueSB.getValue(); blueTF.setText( "" + b ); palette.setColor( r,g,b ); } return super.handleEvent( e ); } // end handleEvent public static void main( String arg[] ){ Frame f = new ScrollTest( "Color Mixer" ); f.show(); } }// end class ScrollTest 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. }// end class ColorCanvas