001package bradleyross.library.applets; 002// 003// scrollbarSample.java 004// A simple Java applet 005// 006import java.awt.*; 007import java.applet.*; 008import java.awt.event.*; 009/** 010* Demonstrates the behavior of a scrollbar in an applet. 011* <p>This Java file can be compiled using 012* <nobr><code>-source 1.3 -target 1.3</code></nobr> 013* so that it will run with browsers using Java Run-Time 014* Enviroments as early as Java 1.3. However, the current 015* scripts use version 1.5 as the target.</p> 016* @author Bradley Ross 017*/ 018public class ScrollbarSample extends Applet implements AdjustmentListener 019 { 020 /** 021 * 022 */ 023 private static final long serialVersionUID = 1L; 024 /** 025 * This class is used to listen to the pull-down menu used to select 026 * the color of the object being drawn. 027 */ 028 class secondListener implements ItemListener 029 { 030 Color colorChoice; 031 /** 032 * String value containing the name of the selected color. 033 * <p>Since this is a public value, it can be read and 034 * changed by other objects.</p> 035 */ 036 public String colorName = new String(); 037 Choice a; 038 secondListener(Choice a2) 039 { 040 a = a2; 041 colorName = "red"; 042 } 043 /** 044 * This method is executed by the system whenever 045 * the value in the pull-down menu. 046 */ 047 public void itemStateChanged(ItemEvent e) 048 { 049 colorName = a.getSelectedItem(); 050 System.out.println("Color changed to " + colorName); 051 screen1.repaint(); 052 } 053 } /* End of class secondListener */ 054 // Parameters for Scrollbar are 055 // int Orientation 056 // int Value -- Original value 057 // int Visible -- Size represented by indicator in scrollbar 058 // int Minimum 059 // int Maximum 060 /** 061 * Integer value indicating the angle to be used in creating the partial 062 * arc of the circle. 063 *<p>Since this is a public field, it can be read and modified by other 064 * objects.</p> 065 */ 066 public int angle; 067 Choice choiceBox = new Choice(); 068 /** 069 * Object listening for changes to the pull-down menu. 070 * <p>The addItemListener method that is called in the init 071 * method for the class links the listening object to 072 * the object which represents the pull-down menu.</p> 073 */ 074 secondListener colorListener = new secondListener(choiceBox); 075 /** 076 * Object for the scrollbar. 077 * <p>This notation allows you to both specify the 078 * parameters for setting up the scrollbar while overriding some 079 * of the methods used in the object.</p> 080 * <p>The addAdjustment listener method in the init method for the 081 * class links the object for the scrollbar to the object 082 * listening to the scrollbar.</p> 083 */ 084 Scrollbar a = new Scrollbar(Scrollbar.HORIZONTAL, 90, 1, 0, 361) 085 { 086 /** 087 * 088 */ 089 private static final long serialVersionUID = 1L; 090 // When sizing the scrollbar, the program seems to use 091 // getPreferredSize rather than getMinimumSize 092 // When creating the canvas objects in Checks2, I had 093 // used getMinimumSize 094 public Dimension getPreferredSize() 095 { return new Dimension (400, 30); } 096 public Dimension getMinimumSize() 097 { return new Dimension (400, 30); } 098 }; /* End of code for Scrollbar a */ 099 /** 100 * Object representing the partial circle which is to be drawn. 101 *<p>This notation allows the the Canvas class to be subclassed at the 102 * same time the object is instantiated by overriding methods. The 103 * Canvas object doesn't do anything useful unless the methods 104 * are overridden in a subclass.</p> 105 *<p>In this case, overriding the paint method adds the instructions 106 * that draw the partial circle according to the rules obtained 107 * from two listeners.</p> 108 *<p>Since the subclass is created but not named, this is an 109 * anonymous subclass.</p> 110 * @see java.awt.Canvas 111 */ 112 Canvas screen1 = new Canvas() 113 { 114 /** 115 * 116 */ 117 private static final long serialVersionUID = 1L; 118 public Dimension getPreferredSize() 119 { return new Dimension (150, 170); } 120 public Dimension getMinimumSize() 121 { return new Dimension (150, 170); } 122 public void paint(Graphics g) 123 { 124 g.setColor(Color.white); 125 g.fillRect(1, 1, 150, 170); 126 if (colorListener.colorName.equalsIgnoreCase("red")) 127 {g.setColor(Color.red);} 128 else if (colorListener.colorName.equalsIgnoreCase("green")) 129 {g.setColor(Color.green);} 130 else if (colorListener.colorName.equalsIgnoreCase("blue")) 131 {g.setColor(Color.blue);} 132 g.fillArc(10, 10, 100, 100, 0, angle); 133 g.drawString (Integer.toString(angle), 10, 130); 134 } 135 }; /* End of code for Canvas screen1 */ 136 /** 137 * The init method is called when the applet is started. 138 *<p>The pull-down menu is represented by the choiceBox object, 139 * while the statement 140 * <code>choiceBox.addItemListener(colorListener)</code> 141 * links the object for the pull-down menu to the object 142 * listening for changes to the pull-down menu.</p> 143 *<p>The statement 144 * <code>a.addAdjustmentListener(this)</code> means that 145 * changes to the scrollbar (object a) cause the 146 * adjustmentValueChanged method of this class to 147 * executed.</p> 148 */ 149 public void init() { 150 System.out.println("Starting program ScrollBar1"); 151 setBackground(Color.white); 152 angle = 90; 153 GridBagLayout layout = new GridBagLayout(); 154 GridBagConstraints constraints = new GridBagConstraints(); 155 setLayout(layout); 156 constraints.insets = new Insets (5, 5, 5, 5); 157 constraints.fill = GridBagConstraints.NONE; 158 constraints.anchor = GridBagConstraints.WEST; 159 constraints.gridwidth = 2; 160 constraints.gridheight = 1; 161 constraints.weightx = 0.0; 162 constraints.weighty = 0.0; 163 // 164 constraints.gridx = 1; 165 constraints.gridy = 1; 166 add(a, constraints); 167 a.addAdjustmentListener(this); 168 // 169 constraints.anchor = GridBagConstraints.SOUTH; 170 constraints.gridx = 1; 171 constraints.gridy = 2; 172 add(screen1, constraints); 173 // 174 constraints.anchor = GridBagConstraints.WEST; 175 constraints.gridwidth = 1; 176 constraints.weightx = 0.0; 177 constraints.weighty = 0.0; 178 constraints.gridy = 3; 179 choiceBox.addItem("red"); 180 choiceBox.addItem("green"); 181 choiceBox.addItem("blue"); 182 add(choiceBox, constraints); 183 choiceBox.addItemListener(colorListener); 184 doLayout(); 185 // setSize (width, height) 186 setSize(450, 375); 187 } 188 /** 189 * Method called by the system every time the position 190 * on the scrollbar is changed. 191 */ 192 public void adjustmentValueChanged(AdjustmentEvent e) 193 { 194 angle = a.getValue(); 195 screen1.repaint(); 196 } 197 /** 198 * Method to be executed when repainting the window. 199 * <p>The paint method is called whenever it is necessary to 200 * redraw the contents of the window created by the 201 * applet. This could be because the window is resized, 202 * covered, or uncovered, or it could be because some 203 * object calls the repaint method.</p> 204 * <p>It should be noted that the object for drawing the 205 * partial circle above has its own paint method.</p> 206 */ 207 public void paint (Graphics g) { 208 g.setColor(Color.blue); 209 } 210}