001package bradleyross.library.applets; 002// colorsSample.java 003// A simple Java applet 004// 005import java.awt.*; 006import java.applet.*; 007import java.awt.Button; 008import java.awt.event.ActionListener; 009import java.awt.event.ActionEvent; 010// 011/** 012* Demonstrates the use of buttons to change the colors on the 013* items in a graphic item. 014* <p>This Java file can be compiled using 015* <nobr><code>-source 1.3 -target 1.3</code></nobr> 016* so that it will run with browsers using Java Run-Time 017* Enviroments as early as Java 1.3. However, the standard 018* script now uses version 1.5 as the standard.</p> 019* @see java.awt.Button 020* @see java.awt.Canvas 021* @see java.awt.Panel 022* @see java.awt.GridBagConstraints 023* @see java.awt.GridBagLayout 024* @author Bradley Ross 025*/ 026public class ColorsSample extends Applet implements ActionListener 027{ 028/** 029 * 030 */ 031 private static final long serialVersionUID = 1L; 032/** 033* The inner class colorSet represents the buttons in the panel 034* which the user sees. 035* <p>Each of the buttons is defined as a public field in the inner class, with each 036* field belonging to the class java.awt.Button. The entire panel containing 037* the buttons is also defined as a public field, with the field of type 038* java.awt.Panel.</p> 039*/ 040class colorSet extends Component 041 { 042 043/** 044 * 045 */ 046 private static final long serialVersionUID = 1L; 047/** 048 * Integer value representing the button that 049 * was depressed. 050 */ 051 public int ColorChoice; 052 /** 053 * Object defining the panel of buttons. 054 */ 055 Panel colorPanel = new Panel(); 056 /** 057 * Object for the "black" button. 058 *<p>The other buttons are defined in the same manner.</p> 059 */ 060 public Button blackButton = new Button("black"); 061 /** 062 * Object for the "blue" button. 063 *<p>The actions in the constructor for blueButton 064 * differ from the other in that the label is 065 * changed to " Blue " while the message returned 066 * to the listener is still "blue".</p> 067 */ 068 public Button blueButton = new Button("blue"); 069 /** See the description of blackButton */ 070 public Button cyanButton = new Button("cyan"); 071 /** See the description of blackButton */ 072 public Button darkGrayButton = new Button("darkGray"); 073 /** See the description of blackButton */ 074 public Button grayButton = new Button("gray"); 075 /** See the description of blackButton */ 076 public Button lightGrayButton = new Button("lightGray"); 077 /** See the description of blackButton */ 078 public Button magentaButton = new Button("magenta"); 079 /** See the description of blackButton */ 080 public Button orangeButton = new Button("orange"); 081 /** See the description of blackButton */ 082 public Button pinkButton = new Button("pink"); 083 /** See the description of blackButton */ 084 public Button redButton = new Button("red"); 085 /** See the description of blackButton */ 086 public Button whiteButton = new Button("white"); 087 /** See the description of blackButton */ 088 public Button yellowButton = new Button("yellow"); 089 /** See the description of blackButton */ 090 public Button greenButton = new Button("green"); 091 /** 092 * This is the constructor for the colorSet class 093 * which defines the buttons. 094 *<p>For blackButton, the setLabel and setActionCommand 095 * methods are used to set the values.</p> 096 *<p>For the other buttons, label for the button and 097 * the message returned when the button is depressed 098 * are set equal to the string used in the constructor 099 * for the java.awt.Button object.</p> 100 *<p>The add method for the colorPanel object places the 101 * various buttons in the panel according to the 102 * GridBagLayout and GridBagConstraints classes.</p> 103 */ 104 public colorSet() 105 { 106 ColorChoice = 1; 107 setBackground(Color.white); 108 GridBagLayout layout = new GridBagLayout(); 109 GridBagConstraints constraints = new GridBagConstraints(); 110 colorPanel.setLayout(layout); 111 constraints.insets = new Insets (5, 5, 5, 5); 112 constraints.fill = GridBagConstraints.BOTH; 113 constraints.anchor = GridBagConstraints.SOUTHWEST; 114 constraints.weightx = 0; constraints.weighty = 0; 115 constraints.gridwidth = 1; constraints.gridheight = 1; 116 // 117 constraints.gridx = 1; constraints.gridy = 1; 118 colorPanel.add(blackButton, constraints); 119 // 120 constraints.gridx = 2; constraints.gridy = 1; 121 colorPanel.add(blueButton, constraints); 122 blueButton.setLabel(" Blue "); 123 blueButton.setActionCommand("blue"); 124 // 125 constraints.gridx = 3; constraints.gridy = 1; 126 colorPanel.add(cyanButton, constraints); 127 // 128 constraints.gridx = 4; constraints.gridy = 1; 129 colorPanel.add(darkGrayButton, constraints); 130 // 131 constraints.gridx = 5; constraints.gridy = 1; 132 colorPanel.add(grayButton, constraints); 133 // 134 constraints.gridx = 1; constraints.gridy = 2; 135 colorPanel.add(greenButton, constraints); 136 // 137 constraints.gridx = 2; constraints.gridy = 2; 138 colorPanel.add(lightGrayButton, constraints); 139 // 140 constraints.gridx = 3; constraints.gridy = 2; 141 colorPanel.add(magentaButton, constraints); 142 // 143 constraints.gridx = 4; constraints.gridy = 2; 144 colorPanel.add(orangeButton, constraints); 145 // 146 constraints.gridx = 5; constraints.gridy = 2; 147 colorPanel.add(pinkButton, constraints); 148 // 149 constraints.gridx = 1; constraints.gridy = 3; 150 colorPanel.add(redButton, constraints); 151 // 152 constraints.gridx = 2; constraints.gridy = 3; 153 colorPanel.add(whiteButton, constraints); 154 // 155 constraints.gridx = 3; constraints.gridy = 3; 156 colorPanel.add(yellowButton, constraints); 157 } 158 } // This is the end of the inner class 159static final String message = "Hello World!"; 160static final String errorMessage = "Illegal Code"; 161private Font font = new Font("serif", Font.ITALIC + Font.BOLD, 36); 162/** 163* The buttons object is the instantiation of the inner class colorSet. 164*/ 165colorSet buttons = new colorSet(); 166/** 167* Initialize the applet. 168*<p>The class colorsSample implements the interface 169* java.awt.ActionListener as well as extending the 170* class java.applet.Applet.</p> 171*<p>The addActionListener methods arrange for the 172* colorsSample class to receive the messages 173* when the buttons are depressed. This means that 174* that the ActionPerformed method for the class 175* is executed whenever a button is depressed.</p> 176* @see java.awt.event.ActionListener 177* @see java.applet.Applet 178*/ 179public void init() 180 { 181 FlowLayout layout = new FlowLayout(); 182 setLayout(layout); 183 setBackground(Color.white); 184 add(buttons.colorPanel); 185 buttons.blackButton.addActionListener(this); 186 buttons.blueButton.addActionListener(this); 187 buttons.cyanButton.addActionListener(this); 188 buttons.darkGrayButton.addActionListener(this); 189 buttons.grayButton.addActionListener(this); 190 buttons.greenButton.addActionListener(this); 191 buttons.lightGrayButton.addActionListener(this); 192 buttons.magentaButton.addActionListener(this); 193 buttons.orangeButton.addActionListener(this); 194 buttons.pinkButton.addActionListener(this); 195 buttons.redButton.addActionListener(this); 196 buttons.whiteButton.addActionListener(this); 197 buttons.yellowButton.addActionListener(this); 198 doLayout(); 199 } // End of init 200/** 201* The paint method is called whenever it is necessary to 202* redraw the contents of the applet. 203* <p>The paint method is called whenever it is necessary 204* redraw the image. It can be called by the window manager 205* when a window is resized, covered, or uncovered or when it 206* detects that another method has executed the repaint method.</p> 207* @param g The graphics item which is to be redrawn. 208*/ 209public void paint (Graphics g) 210 { 211 switch (buttons.ColorChoice) 212 { 213 case 1: 214 g.setColor(Color.black); 215 break; 216 case 2: 217 g.setColor(Color.blue); 218 break; 219 case 3: 220 g.setColor(Color.cyan); 221 break; 222 case 4: 223 g.setColor(Color.darkGray); 224 break; 225 case 5: 226 g.setColor(Color.gray); 227 break; 228 case 6: 229 g.setColor(Color.green); 230 break; 231 case 7: 232 g.setColor(Color.lightGray); 233 break; 234 case 8: 235 g.setColor(Color.magenta); 236 break; 237 case 9: 238 g.setColor(Color.orange); 239 break; 240 case 10: 241 g.setColor(Color.pink); 242 break; 243 case 11: 244 g.setColor(Color.red); 245 break; 246 case 12: 247 g.setColor(Color.yellow); 248 break; 249 case 13: 250 g.setColor(Color.gray); 251 g.fillRect(40, 130, 280, 75); 252 g.setColor(Color.black); 253 g.fillRect(40, 205, 40, 40); 254 g.setColor(Color.darkGray); 255 g.fillRect(80, 205, 40, 40); 256 g.setColor(Color.gray); 257 g.fillRect(120, 205, 40, 40); 258 g.setColor(Color.lightGray); 259 g.fillRect(160, 205, 40, 40); 260 g.setColor(Color.white); 261 g.fillRect(200, 205, 40, 40); 262 g.setColor(Color.lightGray); 263 g.fillRect(240, 205, 40, 40); 264 g.setColor(Color.gray); 265 g.fillRect(280, 205, 40, 40); 266 g.setColor(Color.white); 267 break; 268 default: 269 g.setColor(Color.pink); 270 } 271 g.setFont(font); 272 if (buttons.ColorChoice < 50) 273 {g.drawString(message, 40, 180);} 274 else 275 {g.drawString(errorMessage, 40, 180);} 276 System.out.println("Executing paint"); 277 } // end of paint 278/** 279* The actionPerformed method is executed whenever 280* an action is taken by the user. 281*<p>The event created by depressing the 282* the button is passed as argument of 283* the method and the getActionCommand method 284* obtains the character string associated 285* with the button as defined by the constructor 286* and other methods for the button.</p> 287*<p>This method then sets the ColorChoice field 288* in the buttons object according to which 289* button was depressed and then uses the 290* repaint method to cause the graphics on the 291* applet to be redrawn.</p> 292*/ 293public void actionPerformed (ActionEvent e) 294 { 295 String command = new String(); 296 command = e.getActionCommand(); 297 System.out.println("ActionEvent **" + command + "**"); 298 if (command.equals("black")) 299 { buttons.ColorChoice = 1; } 300 else if (command.equals("blue")) 301 { buttons.ColorChoice = 2; } 302 else if (command.equals("cyan")) 303 { buttons.ColorChoice = 3; } 304 else if (command.equals("darkGray")) 305 { buttons.ColorChoice = 4; } 306 else if (command.equals("gray")) 307 { buttons.ColorChoice = 5; } 308 else if (command.equals("green")) 309 { buttons.ColorChoice = 6; } 310 else if (command.equals("lightGray")) 311 { buttons.ColorChoice = 7; } 312 else if (command.equals("magenta")) 313 { buttons.ColorChoice = 8; } 314 else if (command.equals("orange")) 315 { buttons.ColorChoice = 9; } 316 else if (command.equals("pink")) 317 { buttons.ColorChoice = 10; } 318 else if (command.equals("red")) 319 { buttons.ColorChoice = 11; } 320 else if (command.equals("yellow")) 321 { buttons.ColorChoice = 12; } 322 else if (command.equals("white")) 323 { buttons.ColorChoice = 13; } 324 else 325 { buttons.ColorChoice = 99; } 326 repaint(); 327 } // end of actionPerformed 328}