/*-------------------------------------------------------------------* * NTU Student: Java With Engineering Applications, Spring 1999 * Assignment # 5 - Java How To Program 2nd edition, Deitel & Deitel * Problem 11.16 on page 647 - CLASS Scroll: applet created to * practice Scroll bars and the AdjustmentListener techniques. * * Michael Kusmierz * Xerox Corporation * Rev 1.0 May 14, 1999 * * This code represents a students work in progress * and is not to be used beyond that purpose. *-------------------------------------------------------------------*/ import java.applet.*; import java.awt.*; import java.awt.event.*; /** * This type was created in VisualAge. */ public class Scroll extends Applet implements AdjustmentListener { private Label ivjLabel1 = null; private Scrollbar ivjScrollbar1 = null; private Scrollbar ivjScrollbar2 = null; private Scrollbar ivjScrollbar3 = null; private TextField ivjTextField1 = null; private TextField ivjTextField2 = null; private TextField ivjTextField3 = null; private Label ivjLabel2 = null; private Label ivjLabel3 = null; private Label ivjLabel4 = null; /************************* * ADJUSTMENT VALUE CHANGED - called by AdjustmentListener upon adjustment event * from the scroll bar * */ public void adjustmentValueChanged( AdjustmentEvent e ) { Color colorObj; colorObj = new Color(0,0,0); int intR =0, intG =0, intB = 0; //--- used a graphics object instead of paint Graphics gObj = this.getGraphics(); gObj.drawRect(48,48,354,77); //--- Capture scrollbar changes and reflect them in text boxes if (e.getSource() == ivjScrollbar1) { showStatus("Red Adjustment"); intR = ivjScrollbar1.getValue(); //---FIX scrollbar max - errors due to Java VM versions if (intR >255) intR = 255; ivjTextField1.setText( Integer.toString(intR) ); colorObj = new Color( intR ,0,0); ivjTextField1.setBackground( colorObj ); } else if (e.getSource() == ivjScrollbar2) { showStatus("Green Adjustment"); intG = ivjScrollbar2.getValue(); //---FIX scrollbar max - errors due to Java VM versions if (intG >255) intG = 255; ivjTextField2.setText( Integer.toString(intG) ); colorObj = new Color(0,ivjScrollbar2.getValue(),0); ivjTextField2.setBackground( colorObj ); } else if (e.getSource() == ivjScrollbar3) { showStatus("Blue Adjustment"); intB = ivjScrollbar3.getValue(); //---FIX scrollbar max - errors due to Java VM versions if (intB >255) intB = 255; ivjTextField3.setText( Integer.toString(intB) ); colorObj = new Color(0,0,ivjScrollbar3.getValue()); ivjTextField3.setBackground( colorObj ); } //--- don't forget to fill the rectangle with the color form the textbox's colorObj = new Color(ivjScrollbar1.getValue(),ivjScrollbar2.getValue() ,ivjScrollbar3.getValue()); gObj.setColor(colorObj); gObj.fillRect(50,50,350,75); } /** * Gets the applet information. * @return java.lang.String */ public String getAppletInfo() { return "Scroll created using VisualAge for Java."; } /** * Return the Label1 property value. * @return java.awt.Label */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Label getLabel1() { if (ivjLabel1 == null) { try { ivjLabel1 = new java.awt.Label(); ivjLabel1.setName("Label1"); ivjLabel1.setFont(new java.awt.Font("dialog", 1, 12)); ivjLabel1.setText("Color Chooser - Deitel & Deitel, p640, Problem 11.13"); ivjLabel1.setBounds(61, 12, 320, 23); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjLabel1; } /** * Return the Label2 property value. * @return java.awt.Label */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Label getLabel2() { if (ivjLabel2 == null) { try { ivjLabel2 = new java.awt.Label(); ivjLabel2.setName("Label2"); ivjLabel2.setFont(new java.awt.Font("dialog", 1, 12)); ivjLabel2.setText("R"); ivjLabel2.setBounds(326, 156, 41, 23); ivjLabel2.setForeground(java.awt.Color.red); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjLabel2; } /** * Return the Label3 property value. * @return java.awt.Label */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Label getLabel3() { if (ivjLabel3 == null) { try { ivjLabel3 = new java.awt.Label(); ivjLabel3.setName("Label3"); ivjLabel3.setFont(new java.awt.Font("dialog", 1, 12)); ivjLabel3.setText("G"); ivjLabel3.setBounds(326, 178, 41, 23); ivjLabel3.setForeground(java.awt.Color.green); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjLabel3; } /** * Return the Label4 property value. * @return java.awt.Label */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Label getLabel4() { if (ivjLabel4 == null) { try { ivjLabel4 = new java.awt.Label(); ivjLabel4.setName("Label4"); ivjLabel4.setFont(new java.awt.Font("dialog", 1, 12)); ivjLabel4.setText("B"); ivjLabel4.setBounds(326, 200, 41, 23); ivjLabel4.setForeground(java.awt.Color.blue); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjLabel4; } /** * Return the Scrollbar1 property value. * @return java.awt.Scrollbar */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Scrollbar getScrollbar1() { if (ivjScrollbar1 == null) { try { ivjScrollbar1 = new java.awt.Scrollbar(); ivjScrollbar1.setName("Scrollbar1"); ivjScrollbar1.setMaximum(275); ivjScrollbar1.setBounds(109, 155, 172, 23); ivjScrollbar1.setVisibleAmount(20); ivjScrollbar1.setValue(255); ivjScrollbar1.setOrientation(java.awt.Scrollbar.HORIZONTAL); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjScrollbar1; } /** * Return the Scrollbar2 property value. * @return java.awt.Scrollbar */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Scrollbar getScrollbar2() { if (ivjScrollbar2 == null) { try { ivjScrollbar2 = new java.awt.Scrollbar(); ivjScrollbar2.setName("Scrollbar2"); ivjScrollbar2.setMaximum(275); ivjScrollbar2.setBounds(109, 177, 172, 23); ivjScrollbar2.setVisibleAmount(20); ivjScrollbar2.setValue(255); ivjScrollbar2.setOrientation(java.awt.Scrollbar.HORIZONTAL); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjScrollbar2; } /** * Return the Scrollbar3 property value. * @return java.awt.Scrollbar */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Scrollbar getScrollbar3() { if (ivjScrollbar3 == null) { try { ivjScrollbar3 = new java.awt.Scrollbar(); ivjScrollbar3.setName("Scrollbar3"); ivjScrollbar3.setMaximum(275); ivjScrollbar3.setBounds(109, 199, 172, 23); ivjScrollbar3.setVisibleAmount(20); ivjScrollbar3.setValue(255); ivjScrollbar3.setOrientation(java.awt.Scrollbar.HORIZONTAL); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjScrollbar3; } /** * Return the TextField1 property value. * @return java.awt.TextField */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private TextField getTextField1() { if (ivjTextField1 == null) { try { ivjTextField1 = new java.awt.TextField(); ivjTextField1.setName("TextField1"); ivjTextField1.setFont(new java.awt.Font("dialog", 1, 12)); ivjTextField1.setBackground(java.awt.Color.black); ivjTextField1.setBounds(283, 156, 41, 23); ivjTextField1.setEditable(false); ivjTextField1.setForeground(java.awt.Color.white); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjTextField1; } /** * Return the TextField2 property value. * @return java.awt.TextField */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private TextField getTextField2() { if (ivjTextField2 == null) { try { ivjTextField2 = new java.awt.TextField(); ivjTextField2.setName("TextField2"); ivjTextField2.setFont(new java.awt.Font("dialog", 1, 12)); ivjTextField2.setBackground(java.awt.Color.black); ivjTextField2.setBounds(283, 178, 41, 23); ivjTextField2.setEditable(false); ivjTextField2.setForeground(java.awt.Color.white); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjTextField2; } /** * Return the TextField3 property value. * @return java.awt.TextField */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private TextField getTextField3() { if (ivjTextField3 == null) { try { ivjTextField3 = new java.awt.TextField(); ivjTextField3.setName("TextField3"); ivjTextField3.setFont(new java.awt.Font("dialog", 1, 12)); ivjTextField3.setBackground(java.awt.Color.black); ivjTextField3.setBounds(283, 200, 41, 23); ivjTextField3.setEditable(false); ivjTextField3.setForeground(java.awt.Color.white); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjTextField3; } /** * Called whenever the part throws an exception. * @param exception java.lang.Throwable */ private void handleException(Throwable exception) { /* Uncomment the following lines to print uncaught exceptions to stdout */ // System.out.println("--------- UNCAUGHT EXCEPTION ---------"); // exception.printStackTrace(System.out); } /********* * INIT - Handle the Applet init method. */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ public void init() { super.init(); try { setName("Scroll"); setLayout(null); setSize(426, 240); add(getTextField1(), getTextField1().getName()); add(getTextField2(), getTextField2().getName()); add(getTextField3(), getTextField3().getName()); add(getScrollbar1(), getScrollbar1().getName()); add(getScrollbar2(), getScrollbar2().getName()); add(getScrollbar3(), getScrollbar3().getName()); add(getLabel1(), getLabel1().getName()); add(getLabel2(), getLabel2().getName()); add(getLabel3(), getLabel3().getName()); add(getLabel4(), getLabel4().getName()); // user code begin {1} //--- Scroll bar listeners //ivjScrollbar1.addAdjustmentListener(new ScrollbarHandler(this)); //ivjScrollbar2.addAdjustmentListener(new ScrollbarHandler(this)); //ivjScrollbar3.addAdjustmentListener(new ScrollbarHandler(this)); ivjScrollbar1.addAdjustmentListener(this); ivjScrollbar2.addAdjustmentListener(this); ivjScrollbar3.addAdjustmentListener(this); //--- init the textfields ivjTextField1.setText(Integer.toString( ivjScrollbar1.getValue() )); ivjTextField2.setText(Integer.toString( ivjScrollbar2.getValue() )); ivjTextField3.setText(Integer.toString( ivjScrollbar3.getValue() )); ivjTextField1.setBackground( Color.red ); ivjTextField2.setBackground( Color.green ); ivjTextField3.setBackground( Color.blue ); // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } } /********** * MAIN entrypoint - starts the part when it is run as an application * @param args java.lang.String[] */ public static void main(java.lang.String[] args) { try { Frame frame; try { Class aFrameClass = Class.forName("com.ibm.uvm.abt.edit.TestFrame"); frame = (Frame)aFrameClass.newInstance(); } catch (java.lang.Throwable ivjExc) { frame = new Frame(); } Scroll aScroll; Class iiCls = Class.forName("Scroll"); ClassLoader iiClsLoader = iiCls.getClassLoader(); aScroll = (Scroll)java.beans.Beans.instantiate(iiClsLoader,"Scroll"); frame.add("Center", aScroll); frame.setSize(aScroll.getSize()); frame.setVisible(true); } catch (Throwable exception) { System.err.println("Exception occurred in main() of java.applet.Applet"); exception.printStackTrace(System.out); } } /********** * PAINT - This method PAINTS a box around the area to be filled with a color * @param g java.awt.Graphics */ public void paint(Graphics g) { g.drawRect(48,48,354,77); } }