1 35 36 39 package demo.swingset; 40 41 import swingwtx.swing.*; 42 import swingwtx.swing.event.*; 43 import swingwtx.swing.text.*; 44 import swingwtx.swing.border.*; 45 import swingwtx.swing.colorchooser.*; 46 import swingwtx.swing.filechooser.*; 47 import javax.accessibility.*; 48 49 import swingwt.awt.*; 50 import swingwt.awt.event.*; 51 import swingwt.awt.image.*; 52 import java.beans.*; 53 import java.util.*; 54 import java.io.*; 55 import java.applet.*; 56 import java.net.*; 57 58 64 public class ComboBoxDemo extends DemoModule implements ActionListener { 65 66 Face face; 67 JLabel faceLabel; 68 69 JComboBox hairCB; 70 JComboBox eyesCB; 71 JComboBox mouthCB; 72 73 JComboBox presetCB; 74 75 Hashtable parts = new Hashtable(); 76 77 80 public static void main(String [] args) { 81 ComboBoxDemo demo = new ComboBoxDemo(null); 82 demo.mainImpl(); 83 } 84 85 88 public ComboBoxDemo(SwingSet2 swingset) { 89 super(swingset, "ComboBoxDemo", "toolbar/JComboBox.gif"); 92 93 createComboBoxDemo(); 94 } 95 96 public void createComboBoxDemo() { 97 JPanel demo = getDemoPanel(); 98 99 JPanel demoPanel = getDemoPanel(); 100 demoPanel.setLayout(new BoxLayout(demoPanel, BoxLayout.Y_AXIS)); 101 102 JPanel innerPanel = new JPanel(); 103 innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS)); 104 105 demoPanel.add(Box.createRigidArea(VGAP20)); 106 demoPanel.add(innerPanel); 107 demoPanel.add(Box.createRigidArea(VGAP20)); 108 109 innerPanel.add(Box.createRigidArea(HGAP20)); 110 111 JPanel comboBoxPanel = new JPanel() { 113 public Dimension getMaximumSize() { 114 return new Dimension(getPreferredSize().width, super.getMaximumSize().height); 115 } 116 }; 117 comboBoxPanel.setLayout(new BoxLayout(comboBoxPanel, BoxLayout.Y_AXIS)); 118 119 comboBoxPanel.add(Box.createRigidArea(VGAP15)); 120 121 JLabel l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.presets"))); 122 l.setAlignmentX(JLabel.LEFT_ALIGNMENT); 123 presetCB = (JComboBox) comboBoxPanel.add(createPresetComboBox()); 124 presetCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT); 125 l.setLabelFor(presetCB); 126 comboBoxPanel.add(Box.createRigidArea(VGAP30)); 127 128 l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.hair_description"))); 129 l.setAlignmentX(JLabel.LEFT_ALIGNMENT); 130 hairCB = (JComboBox) comboBoxPanel.add(createHairComboBox()); 131 hairCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT); 132 l.setLabelFor(hairCB); 133 comboBoxPanel.add(Box.createRigidArea(VGAP15)); 134 135 l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.eyes_description"))); 136 l.setAlignmentX(JLabel.LEFT_ALIGNMENT); 137 eyesCB = (JComboBox) comboBoxPanel.add(createEyesComboBox()); 138 eyesCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT); 139 l.setLabelFor(eyesCB); 140 comboBoxPanel.add(Box.createRigidArea(VGAP15)); 141 142 l = (JLabel) comboBoxPanel.add(new JLabel(getString("ComboBoxDemo.mouth_description"))); 143 l.setAlignmentX(JLabel.LEFT_ALIGNMENT); 144 mouthCB = (JComboBox) comboBoxPanel.add(createMouthComboBox()); 145 mouthCB.setAlignmentX(JComboBox.LEFT_ALIGNMENT); 146 l.setLabelFor(mouthCB); 147 comboBoxPanel.add(Box.createRigidArea(VGAP15)); 148 149 comboBoxPanel.add(new JPanel(new BorderLayout())); 151 152 154 face = new Face(); 155 JPanel facePanel = new JPanel(); 156 facePanel.setLayout(new BorderLayout()); 157 facePanel.setBorder(new BevelBorder(BevelBorder.LOWERED)); 158 159 faceLabel = new JLabel(); 160 facePanel.add(faceLabel, BorderLayout.CENTER); 161 Object [] controlledByObjects = new Object [3]; 164 controlledByObjects[0] = hairCB; 165 controlledByObjects[1] = eyesCB; 166 controlledByObjects[2] = mouthCB; 167 172 181 innerPanel.add(comboBoxPanel); 183 innerPanel.add(Box.createRigidArea(HGAP30)); 184 innerPanel.add(facePanel); 185 innerPanel.add(Box.createRigidArea(HGAP20)); 186 187 addFace("brent", getString("ComboBoxDemo.brent")); 189 addFace("georges", getString("ComboBoxDemo.georges")); 190 addFace("hans", getString("ComboBoxDemo.hans")); 191 addFace("howard", getString("ComboBoxDemo.howard")); 192 addFace("james", getString("ComboBoxDemo.james")); 193 addFace("jeff", getString("ComboBoxDemo.jeff")); 194 addFace("jon", getString("ComboBoxDemo.jon")); 195 addFace("lara", getString("ComboBoxDemo.lara")); 196 addFace("larry", getString("ComboBoxDemo.larry")); 197 addFace("lisa", getString("ComboBoxDemo.lisa")); 198 addFace("michael", getString("ComboBoxDemo.michael")); 199 addFace("philip", getString("ComboBoxDemo.philip")); 200 addFace("scott", getString("ComboBoxDemo.scott")); 201 202 presetCB.setSelectedIndex(0); 204 205 getDemoPanel().invalidate(); 206 } 207 208 void addFace(String name, String i18n_name) { 209 ImageIcon i; 210 String i18n_hair = getString("ComboBoxDemo.hair"); 211 String i18n_eyes = getString("ComboBoxDemo.eyes"); 212 String i18n_mouth = getString("ComboBoxDemo.mouth"); 213 214 parts.put(i18n_name, name); parts.put(name, i18n_name); 217 i = createImageIcon("combobox/" + name + "hair.jpg", i18n_name + i18n_hair); 218 parts.put(name + "hair", i); 219 220 i = createImageIcon("combobox/" + name + "eyes.jpg", i18n_name + i18n_eyes); 221 parts.put(name + "eyes", i); 222 223 i = createImageIcon("combobox/" + name + "mouth.jpg", i18n_name + i18n_mouth); 224 parts.put(name + "mouth", i); 225 } 226 227 Face getFace() { 228 return face; 229 } 230 231 JComboBox createHairComboBox() { 232 JComboBox cb = new JComboBox(); 233 fillComboBox(cb); 234 cb.addActionListener(this); 235 return cb; 236 } 237 238 JComboBox createEyesComboBox() { 239 JComboBox cb = new JComboBox(); 240 fillComboBox(cb); 241 cb.addActionListener(this); 242 return cb; 243 } 244 245 JComboBox createNoseComboBox() { 246 JComboBox cb = new JComboBox(); 247 fillComboBox(cb); 248 cb.addActionListener(this); 249 return cb; 250 } 251 252 JComboBox createMouthComboBox() { 253 JComboBox cb = new JComboBox(); 254 fillComboBox(cb); 255 cb.addActionListener(this); 256 return cb; 257 } 258 259 JComboBox createPresetComboBox() { 260 JComboBox cb = new JComboBox(); 261 cb.addItem(getString("ComboBoxDemo.preset1")); 262 cb.addItem(getString("ComboBoxDemo.preset2")); 263 cb.addItem(getString("ComboBoxDemo.preset3")); 264 cb.addItem(getString("ComboBoxDemo.preset4")); 265 cb.addItem(getString("ComboBoxDemo.preset5")); 266 cb.addItem(getString("ComboBoxDemo.preset6")); 267 cb.addItem(getString("ComboBoxDemo.preset7")); 268 cb.addItem(getString("ComboBoxDemo.preset8")); 269 cb.addItem(getString("ComboBoxDemo.preset9")); 270 cb.addItem(getString("ComboBoxDemo.preset10")); 271 cb.addActionListener(this); 272 return cb; 273 } 274 275 void fillComboBox(JComboBox cb) { 276 cb.addItem(getString("ComboBoxDemo.brent")); 277 cb.addItem(getString("ComboBoxDemo.georges")); 278 cb.addItem(getString("ComboBoxDemo.hans")); 279 cb.addItem(getString("ComboBoxDemo.howard")); 280 cb.addItem(getString("ComboBoxDemo.james")); 281 cb.addItem(getString("ComboBoxDemo.jeff")); 282 cb.addItem(getString("ComboBoxDemo.jon")); 283 cb.addItem(getString("ComboBoxDemo.lara")); 284 cb.addItem(getString("ComboBoxDemo.larry")); 285 cb.addItem(getString("ComboBoxDemo.lisa")); 286 cb.addItem(getString("ComboBoxDemo.michael")); 287 cb.addItem(getString("ComboBoxDemo.philip")); 288 cb.addItem(getString("ComboBoxDemo.scott")); 289 } 290 291 public void actionPerformed(ActionEvent e) { 292 if(e.getSource() == hairCB) { 293 String name = (String ) parts.get((String ) hairCB.getSelectedItem()); 294 face.setHair((ImageIcon) parts.get(name + "hair")); 295 if (face.hasAllBits()) faceLabel.setIcon(face); 296 } else if(e.getSource() == eyesCB) { 297 String name = (String ) parts.get((String ) eyesCB.getSelectedItem()); 298 face.setEyes((ImageIcon) parts.get(name + "eyes")); 299 if (face.hasAllBits()) faceLabel.setIcon(face); 300 } else if(e.getSource() == mouthCB) { 301 String name = (String ) parts.get((String ) mouthCB.getSelectedItem()); 302 face.setMouth((ImageIcon) parts.get(name + "mouth")); 303 if (face.hasAllBits()) faceLabel.setIcon(face); 304 } else if(e.getSource() == presetCB) { 305 String hair = null; 306 String eyes = null; 307 String mouth = null; 308 switch(presetCB.getSelectedIndex()) { 309 case 0: 310 hair = (String ) parts.get("philip"); 311 eyes = (String ) parts.get("howard"); 312 mouth = (String ) parts.get("jeff"); 313 break; 314 case 1: 315 hair = (String ) parts.get("jeff"); 316 eyes = (String ) parts.get("larry"); 317 mouth = (String ) parts.get("philip"); 318 break; 319 case 2: 320 hair = (String ) parts.get("howard"); 321 eyes = (String ) parts.get("scott"); 322 mouth = (String ) parts.get("hans"); 323 break; 324 case 3: 325 hair = (String ) parts.get("philip"); 326 eyes = (String ) parts.get("jeff"); 327 mouth = (String ) parts.get("hans"); 328 break; 329 case 4: 330 hair = (String ) parts.get("brent"); 331 eyes = (String ) parts.get("jon"); 332 mouth = (String ) parts.get("scott"); 333 break; 334 case 5: 335 hair = (String ) parts.get("lara"); 336 eyes = (String ) parts.get("larry"); 337 mouth = (String ) parts.get("lisa"); 338 break; 339 case 6: 340 hair = (String ) parts.get("james"); 341 eyes = (String ) parts.get("philip"); 342 mouth = (String ) parts.get("michael"); 343 break; 344 case 7: 345 hair = (String ) parts.get("philip"); 346 eyes = (String ) parts.get("lisa"); 347 mouth = (String ) parts.get("brent"); 348 break; 349 case 8: 350 hair = (String ) parts.get("james"); 351 eyes = (String ) parts.get("philip"); 352 mouth = (String ) parts.get("jon"); 353 break; 354 case 9: 355 hair = (String ) parts.get("lara"); 356 eyes = (String ) parts.get("jon"); 357 mouth = (String ) parts.get("scott"); 358 break; 359 } 360 if(hair != null) { 361 hairCB.setSelectedItem(hair); 362 eyesCB.setSelectedItem(eyes); 363 mouthCB.setSelectedItem(mouth); 364 if (face.hasAllBits()) faceLabel.setIcon(face); 365 } 366 } 367 368 getDemoPanel().invalidate(); 369 } 370 371 class Face extends ImageIcon { 372 ImageIcon hair; 373 ImageIcon eyes; 374 ImageIcon mouth; 375 376 void setHair(ImageIcon i) { 377 hair = i; 378 } 379 380 void setEyes(ImageIcon i) { 381 eyes = i; 382 } 383 384 void setMouth(ImageIcon i) { 385 mouth = i; 386 } 387 388 public boolean hasAllBits() { 389 return hair != null && eyes != null && mouth != null; 390 } 391 392 public void paintIcon(Component c, Graphics g, int x, int y) { 393 int height = y; 394 x = c.getWidth()/2 - getIconWidth()/2; 395 396 if(hair != null) { 397 hair.paintIcon(c, g, x, height); height += hair.getIconHeight(); 398 } 399 400 if(eyes != null) { 401 eyes.paintIcon(c, g, x, height); height += eyes.getIconHeight(); 402 } 403 404 if(mouth != null) { 405 mouth.paintIcon(c, g, x, height); 406 } 407 } 408 409 public Image getImage() { 410 if (hair == null || eyes == null || mouth == null) return null; 411 BufferedImage b = new BufferedImage(getIconWidth(), getIconHeight(), BufferedImage.TYPE_INT_RGB); 412 Graphics g = b.getGraphics(); 413 g.drawImage( hair.getImage(), 0, 0, null ); 414 g.drawImage( eyes.getImage(), 0, hair.getIconHeight(), null ); 415 g.drawImage( mouth.getImage(), 0, hair.getIconHeight() + eyes.getIconHeight(), null); 416 g.dispose(); 417 return b; 418 } 419 420 public int getIconWidth() { 421 return 344; 422 } 423 424 public int getIconHeight() { 425 return 455; 426 } 427 } 428 } 429 430 | Popular Tags |