| 1 36 37 40 41 42 package java2d; 43 44 import java.awt.*; 45 import java.awt.event.*; 46 import javax.swing.*; 47 import javax.swing.border.*; 48 import javax.swing.event.ChangeEvent ; 49 import javax.swing.event.ChangeListener ; 50 51 52 59 public class DemoGroup extends JPanel 60 implements MouseListener, ChangeListener , ActionListener { 61 62 static int columns = 2; 63 64 private static Font font = new Font("serif", Font.PLAIN, 10); 65 private static EmptyBorder emptyB = new EmptyBorder(5,5,5,5); 66 private static BevelBorder bevelB = new BevelBorder(BevelBorder.LOWERED); 67 68 private String groupName; 69 public JPanel clonePanels[]; 70 public JTabbedPane tabbedPane; 71 72 73 public DemoGroup(String name) { 74 75 groupName = name; 76 77 setLayout(new BorderLayout()); 78 79 JPanel p = new JPanel(new GridLayout(0,2)); 80 p.setBorder(new CompoundBorder(emptyB, bevelB)); 81 82 int index = -1; 84 while (!name.equals(Java2Demo.demos[++index][0])) {} 85 String [] demos = Java2Demo.demos[index]; 86 87 int numDemos = demos.length - 1; 90 if (numDemos%2 == 1) { 91 p.setLayout(new GridBagLayout()); 92 } 93 94 for (int i = 1; i <= numDemos; i++) { 96 DemoPanel dp = new DemoPanel("java2d.demos."+name+"."+demos[i]); 97 dp.setDemoBorder(p); 98 if (dp.surface != null) { 99 dp.surface.addMouseListener(this); 100 dp.surface.setMonitor(Java2Demo.performancemonitor != null); 101 } 102 if (p.getLayout() instanceof GridBagLayout) { 103 int x = p.getComponentCount() % 2; 104 int y = p.getComponentCount() / 2; 105 int w = (i == numDemos) ? 2 : 1; 106 Java2Demo.addToGridBag(p,dp,x,y,w,1,1,1); 107 } else { 108 p.add(dp); 109 } 110 } 111 112 add(p); 113 } 114 115 116 public void mouseClicked(MouseEvent e) { 117 118 if (tabbedPane == null) { 119 shutDown(getPanel()); 120 JPanel p = new JPanel(new BorderLayout()); 121 p.setBorder(new CompoundBorder(emptyB, bevelB)); 122 123 tabbedPane = new JTabbedPane(); 124 tabbedPane.setFont(font); 125 126 JPanel tmpP = (JPanel) getComponent(0); 127 tabbedPane.addTab(groupName, tmpP); 128 129 clonePanels = new JPanel[tmpP.getComponentCount()]; 130 for (int i = 0; i < clonePanels.length; i++) { 131 clonePanels[i] = new JPanel(new BorderLayout()); 132 DemoPanel dp = (DemoPanel) tmpP.getComponent(i); 133 DemoPanel c = new DemoPanel(dp.className); 134 c.setDemoBorder(clonePanels[i]); 135 if (c.surface != null) { 136 c.surface.setMonitor(Java2Demo.performancemonitor != null); 137 Image cloneImg = DemoImages.getImage("clone.gif", this); 138 c.tools.cloneB = 139 c.tools.addTool(cloneImg,"Clone the Surface",this); 140 Dimension d = c.tools.toolbar.getPreferredSize(); 141 c.tools.toolbar.setPreferredSize( 142 new Dimension(d.width+27, d.height)); 143 if (Java2Demo.backgroundColor != null) { 144 c.surface.setBackground(Java2Demo.backgroundColor); 145 } 146 } 147 clonePanels[i].add(c); 148 String s = dp.className.substring(dp.className.indexOf('.')+1); 149 tabbedPane.addTab(s, clonePanels[i]); 150 } 151 p.add(tabbedPane); 152 remove(tmpP); 153 add(p); 154 155 tabbedPane.addChangeListener(this); 156 revalidate(); 157 } 158 159 String className = e.getComponent().toString(); 160 className = className.substring(0, className.indexOf('[')); 161 162 for (int i = 0; i < tabbedPane.getTabCount(); i++) { 163 String s1 = className.substring(className.indexOf('.')+1); 164 if (tabbedPane.getTitleAt(i).equals(s1)) { 165 tabbedPane.setSelectedIndex(i); 166 break; 167 } 168 } 169 170 revalidate(); 171 } 172 173 public void mousePressed (MouseEvent e) { } 174 public void mouseReleased(MouseEvent e) { } 175 public void mouseEntered (MouseEvent e) { } 176 public void mouseExited (MouseEvent e) { } 177 178 179 public void actionPerformed(ActionEvent e) { 180 JButton b = (JButton) e.getSource(); 181 if (b.getToolTipText().startsWith("Clone")) { 182 cloneDemo(); 183 } else { 184 removeClone(b.getParent().getParent().getParent().getParent()); 185 } 186 } 187 188 189 private int index; 190 191 public void stateChanged(ChangeEvent e) { 192 shutDown((JPanel) tabbedPane.getComponentAt(index)); 193 index = tabbedPane.getSelectedIndex(); 194 setup(false); 195 } 196 197 198 public JPanel getPanel() { 199 if (tabbedPane != null) { 200 return (JPanel) tabbedPane.getSelectedComponent(); 201 } else { 202 return (JPanel) getComponent(0); 203 } 204 } 205 206 207 public void setup(boolean issueRepaint) { 208 209 JPanel p = getPanel(); 210 211 if (Java2Demo.performancemonitor != null) { 213 Java2Demo.performancemonitor.surf.setPanel(p); 214 Java2Demo.performancemonitor.surf.setSurfaceState(); 215 } 216 217 GlobalControls c = Java2Demo.controls; 218 for (int i = 0; i < p.getComponentCount(); i++) { 221 DemoPanel dp = (DemoPanel) p.getComponent(i); 222 if (dp.surface != null && c != null) { 223 Tools t = dp.tools; 224 t.setVisible(isValid()); 225 t.issueRepaint = issueRepaint; 226 JToggleButton b[] = {t.toggleB, t.aliasB, t.renderB, 227 t.textureB, t.compositeB}; 228 JCheckBox cb[] = {c.toolBarCB, c.aliasCB, c.renderCB, 229 c.textureCB, c.compositeCB}; 230 for (int j = 0; j < b.length; j++) { 231 if (c.obj != null && c.obj.equals(cb[j])) { 232 if (b[j].isSelected() != cb[j].isSelected()) { 233 b[j].doClick(); 234 } 235 } else if (c.obj == null) { 236 if (b[j].isSelected() != cb[j].isSelected()) { 237 b[j].doClick(); 238 } 239 } 240 } 241 t.setVisible(true); 242 if (c.screenCombo.getSelectedIndex() != t.screenCombo.getSelectedIndex()) 243 { 244 t.screenCombo.setSelectedIndex(c.screenCombo.getSelectedIndex()); 245 } 246 if (Java2Demo.verboseCB.isSelected()) { 247 dp.surface.verbose(); 248 } 249 dp.surface.setSleepAmount(c.slider.getValue()); 250 if (Java2Demo.backgroundColor != null) { 251 dp.surface.setBackground(Java2Demo.backgroundColor); 252 } 253 t.issueRepaint = true; 254 } 255 dp.start(); 256 } 257 revalidate(); 258 } 259 260 261 public void shutDown(JPanel p) { 262 for (int i = 0; i < p.getComponentCount(); i++) { 263 ((DemoPanel) p.getComponent(i)).stop(); 264 } 265 System.gc(); 266 } 267 268 269 public void cloneDemo() { 270 JPanel panel = clonePanels[tabbedPane.getSelectedIndex() - 1]; 271 if (panel.getComponentCount() == 1) { 272 panel.invalidate(); 273 panel.setLayout(new GridLayout(0,columns,5,5)); 274 panel.revalidate(); 275 } 276 DemoPanel original = (DemoPanel) getPanel().getComponent(0); 277 DemoPanel clone = new DemoPanel(original.className); 278 if (columns == 2) { 279 clone.setDemoBorder(panel); 280 } 281 Image removeImg = DemoImages.getImage("remove.gif", this); 282 clone.tools.cloneB = 283 clone.tools.addTool(removeImg,"Remove the Surface",this); 284 Dimension d = clone.tools.toolbar.getPreferredSize(); 285 clone.tools.toolbar.setPreferredSize( 286 new Dimension(d.width+27, d.height)); 287 if (Java2Demo.backgroundColor != null) { 288 clone.surface.setBackground(Java2Demo.backgroundColor); 289 } 290 if (Java2Demo.controls != null) { 291 if (clone.tools.isExpanded 292 != Java2Demo.controls.toolBarCB.isSelected()) 293 { 294 clone.tools.toggleB.doClick(); 295 } 296 } 297 clone.start(); 298 clone.surface.setMonitor(Java2Demo.performancemonitor != null); 299 panel.add(clone); 300 panel.repaint(); 301 panel.revalidate(); 302 } 303 304 305 public void removeClone(Component theClone) { 306 JPanel panel = clonePanels[tabbedPane.getSelectedIndex() - 1]; 307 if (panel.getComponentCount() == 2) { 308 Component cmp = panel.getComponent(0); 309 panel.removeAll(); 310 panel.setLayout(new BorderLayout()); 311 panel.revalidate(); 312 panel.add(cmp); 313 } else { 314 panel.remove(theClone); 315 int cmpCount = panel.getComponentCount(); 316 for (int j = 1; j < cmpCount; j++) { 317 int top = (j+1 >= 3) ? 0 : 5; 318 int left = ((j+1) % 2) == 0 ? 0 : 5; 319 EmptyBorder eb = new EmptyBorder(top,left,5,5); 320 SoftBevelBorder sbb = new SoftBevelBorder(BevelBorder.RAISED); 321 JPanel p = (JPanel) panel.getComponent(j); 322 p.setBorder(new CompoundBorder(eb, sbb)); 323 } 324 } 325 panel.repaint(); 326 panel.revalidate(); 327 } 328 329 330 public static void main(String args[]) { 331 final DemoGroup group = new DemoGroup(args[0]); 332 JFrame f = new JFrame("Java2D Demo - DemoGroup"); 333 f.addWindowListener(new WindowAdapter() { 334 public void windowClosing(WindowEvent e) {System.exit(0);} 335 public void windowDeiconified(WindowEvent e) { group.setup(false); } 336 public void windowIconified(WindowEvent e) { 337 group.shutDown(group.getPanel()); 338 } 339 }); 340 f.getContentPane().add("Center", group); 341 f.pack(); 342 int WIDTH = 620; 343 int HEIGHT = 530; 344 f.setSize(WIDTH, HEIGHT); 345 f.setLocationRelativeTo(null); f.setVisible(true); 347 for (String arg : args) { 348 if (arg.startsWith("-ccthread")) { 349 Java2Demo.ccthreadCB = new JCheckBoxMenuItem("CCThread", true); 350 } 351 } 352 group.setup(false); 353 } 354 } 355 | Popular Tags |