/* * @(#)Java2Demo.java 1.13 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ import java.awt.*; import com.sun.java.swing.*; import java.awt.event.*; import com.sun.java.swing.border.*; import java.net.URL; import java.io.File; /** * A demo that shows Java2D features. * * @version @(#)Java2Demo.java 1.13 98/06/29 * @author Brian Lichtenwalter */ public class Java2Demo extends JPanel implements ItemListener, ActionListener, ComponentListener { static Controls controls; static MemoryMonitor memorymonitor; static PerformanceMonitor performancemonitor; static JTabbedPane tabbedPane; static JLabel progressLabel; static JProgressBar progressBar; static TheLayout layouts[]; private JCheckBoxMenuItem memoryCB, perfCB; private JCheckBoxMenuItem controlsCB; private JMenuItem runMI, cloneMI, fileMI; private RunWindow runwindow; private CloneDemo clonedemo; private JFrame rf, cf; private GlobalPanel gp; /** * Construct the Java2D Demo. */ public Java2Demo() { setLayout(new BorderLayout()); setBorder(new EtchedBorder()); add(createMenuBar(), BorderLayout.NORTH); controls = new Controls(); memorymonitor = new MemoryMonitor(); performancemonitor = new PerformanceMonitor(); gp = new GlobalPanel(); tabbedPane = new JTabbedPane(); tabbedPane.setFont(new Font("Times New Roman", Font.PLAIN, 12)); tabbedPane.addChangeListener(gp); // Retrieve directory names in demos directory URL url = Java2Demo.class.getResource("demos"); File demosDir = new File(url.getFile()); if (demosDir != null && demosDir.isDirectory()) { String list[] = demosDir.list(); layouts = new TheLayout[list.length]; progressBar.setMaximum(list.length); sort(list); for (int i = 0; i < list.length; i++) { File dirs = new File(demosDir.getPath(), list[i]); if (dirs != null && dirs.isDirectory()) { progressLabel.setText("Loading demos." + dirs.getName()); layouts[i] = new TheLayout(dirs.getName()); if (i == 0) // Make the "always visible" global panel the first child so // that it always remains "on top" of the z-order tabbedPane.addTab(dirs.getName(), gp); else tabbedPane.addTab(dirs.getName(), null); progressBar.setValue(progressBar.getValue() + 1); } } } else { System.out.println("Fatal, couldn't find demos directory. "); System.exit(1); } add(tabbedPane, BorderLayout.CENTER); addComponentListener(this); } private JMenuBar createMenuBar() { JPopupMenu.setDefaultLightWeightPopupEnabled(false); JMenuBar menuBar = new JMenuBar(); JMenu file = (JMenu) menuBar.add(new JMenu("File")); fileMI = (JMenuItem) file.add(new JMenuItem("Exit")); fileMI.addActionListener(this); JMenu options = (JMenu) menuBar.add(new JMenu("Options")); controlsCB = (JCheckBoxMenuItem) options.add( new JCheckBoxMenuItem("Global Controls", true)); controlsCB.addItemListener(this); memoryCB = (JCheckBoxMenuItem) options.add( new JCheckBoxMenuItem("Memory Monitor", true)); memoryCB.addItemListener(this); perfCB = (JCheckBoxMenuItem) options.add( new JCheckBoxMenuItem("Performance Monitor", true)); perfCB.addItemListener(this); options.add(new JSeparator()); runMI = (JMenuItem) options.add(new JMenuItem("Run Window")); runMI.addActionListener(this); cloneMI = (JMenuItem) options.add(new JMenuItem("Cloning Demo")); cloneMI.addActionListener(this); return menuBar; } /** * sort directory names */ private void sort(String a[]) { for (int i = a.length; --i>=0; ) { boolean swapped = false; for (int j = 0; j 0) { String T = a[j]; a[j] = a[j+1]; a[j+1] = T; swapped = true; } } if (!swapped) return; } } private void createRunWindow() { if (runwindow != null) return; runwindow = new RunWindow(); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { runwindow.stop(); rf.dispose(); } public void windowClosed(WindowEvent e) { runwindow = null; } }; rf = new JFrame("Run"); rf.addWindowListener(l); rf.getContentPane().add("Center", runwindow); rf.pack(); rf.setSize(new Dimension(240,140)); rf.show(); } public void actionPerformed(ActionEvent e) { if (e.getSource().equals(fileMI)) { System.exit(0); } else if (e.getSource().equals(runMI)) { createRunWindow(); } else if (e.getSource().equals(cloneMI)) { if (clonedemo == null) { clonedemo = new CloneDemo(); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { clonedemo.stop(); cf.dispose(); } public void windowClosed(WindowEvent e) { clonedemo = null; } }; cf = new JFrame("Cloning Demo"); cf.addWindowListener(l); cf.getContentPane().add("Center", clonedemo); cf.pack(); cf.setSize(new Dimension(320,300)); cf.show(); } } } public void itemStateChanged(ItemEvent e) { if (e.getSource().equals(controlsCB)) { if (controls.isVisible()) { controls.setVisible(false); Component cmps[] = controls.texturechooser.getComponents(); for (int i = 0; i < cmps.length; i++) cmps[i].setVisible(false); } else { controls.setVisible(true); Component cmps[] = controls.texturechooser.getComponents(); for (int i = 0; i < cmps.length; i++) cmps[i].setVisible(true); } } else if (e.getSource().equals(memoryCB)) { if (memorymonitor.isVisible()) { memorymonitor.setVisible(false); memorymonitor.mmc.setVisible(false); } else { memorymonitor.setVisible(true); memorymonitor.mmc.setVisible(true); memorymonitor.mmc.start(); } } else if (e.getSource().equals(perfCB)) { if (performancemonitor.isVisible()) { performancemonitor.setVisible(false); performancemonitor.pmc.setVisible(false); } else { performancemonitor.setVisible(true); performancemonitor.pmc.setVisible(true); performancemonitor.pmc.start(); } } validate(); } private Dimension oldD; // Resize necessary for tabs 1-N do not resizing correctly // when TheLayout's layout equals GridBagLayout. public void componentResized(ComponentEvent e) { if (tabbedPane.getSelectedIndex() == 0) { oldD = getSize(); return; } Dimension newD = getSize(); Dimension gpD = gp.getSize(); gp.setSize(new Dimension(gpD.width-(oldD.width-newD.width), gpD.height-(oldD.height-newD.height))); gp.validate(); oldD = newD; } public void componentMoved(ComponentEvent e) {} public void componentShown(ComponentEvent e) {} public void componentHidden(ComponentEvent e) {} public void start() { TheLayout tl = layouts[tabbedPane.getSelectedIndex()]; tl.setup(); if (memorymonitor.mmc.thread == null && memoryCB.getState()) memorymonitor.mmc.start(); if (performancemonitor.pmc.thread == null && perfCB.getState()) performancemonitor.pmc.start(); } public void stop() { memorymonitor.mmc.stop(); performancemonitor.pmc.stop(); JPanel panel = layouts[tabbedPane.getSelectedIndex()].getPanel(); for (int i = 0; i < panel.getComponentCount(); i++) { Component cmp = panel.getComponent(i); if (cmp instanceof CanvasPanel) { CanvasPanel cp = (CanvasPanel) cmp; if (cp.canvas != null && cp.canvas.runnable) cp.canvas.stop(); } } } // .. Java2Demo isn't fully repainting itself after removing clone // .. and changing to BorderLayout .. static Java2Demo demo; static void repaintFix() { demo.repaint(); } static void addToGridBag(JPanel panel, Component comp, int x, int y, int w, int h, double weightx, double weighty) { GridBagLayout gbl = (GridBagLayout) panel.getLayout(); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; c.gridx = x; c.gridy = y; c.gridwidth = w; c.gridheight = h; c.weightx = weightx; c.weighty = weighty; panel.add(comp); gbl.setConstraints(comp, c); } public static void main(String args[]) { JFrame frame = new JFrame("Java2D Demo"); frame.getAccessibleContext().setAccessibleDescription("A sample application to demonstrate Java2D features"); JOptionPane.setRootFrame(frame); JPanel progressPanel = new JPanel() { public Insets getInsets() { return new Insets(40,30,20,30); } }; progressPanel.setLayout(new BoxLayout(progressPanel, BoxLayout.Y_AXIS)); frame.getContentPane().add(progressPanel, BorderLayout.CENTER); Dimension d = new Dimension(400, 20); progressLabel = new JLabel("Loading, please wait..."); progressLabel.setAlignmentX(CENTER_ALIGNMENT); progressLabel.setMaximumSize(d); progressLabel.setPreferredSize(d); progressPanel.add(progressLabel); progressPanel.add(Box.createRigidArea(new Dimension(1,20))); progressBar = new JProgressBar(); progressLabel.setLabelFor(progressBar); progressBar.setAlignmentX(CENTER_ALIGNMENT); progressBar.setMinimum(0); progressBar.setValue(0); progressBar.getAccessibleContext().setAccessibleName("Java2D loading progress"); progressPanel.add(progressBar); int INITIAL_WIDTH = 400; int INITIAL_HEIGHT = 200; frame.setSize(INITIAL_WIDTH, INITIAL_HEIGHT); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation(screenSize.width/2 - INITIAL_WIDTH/2, screenSize.height/2 - INITIAL_HEIGHT/2); frame.show(); frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); demo = new Java2Demo(); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeiconified(WindowEvent e) { demo.start(); } public void windowIconified(WindowEvent e) { demo.stop(); } }; frame.addWindowListener(l); frame.getContentPane().removeAll(); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(demo, BorderLayout.CENTER); int WIDTH = 730; int HEIGHT = 560; frame.setLocation(screenSize.width/2 - WIDTH/2, screenSize.height/2 - HEIGHT/2); frame.setSize(WIDTH, HEIGHT); frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); frame.validate(); frame.repaint(); demo.requestDefaultFocus(); for (int i = 0; i < args.length; i++) { String s = args[i].substring(args[i].indexOf('=')+1); if (args[i].startsWith("-runs=")) { demo.createRunWindow(); RunWindow.numRuns = Integer.parseInt(s); RunWindow.runB.doClick(); } else if (args[i].startsWith("-delay=")) { RunWindow.setDelay(Integer.parseInt(s)); } else if (args[i].startsWith("-screen=")) { demo.controls.screenCombo.setSelectedIndex(Integer.parseInt(s)); } else if (args[i].startsWith("-antialias=")) { demo.controls.aliasCB.setSelected(s.endsWith("true")); } else if (args[i].startsWith("-rendering=")) { demo.controls.renderCB.setSelected(s.endsWith("true")); } else if (args[i].startsWith("-texture=")) { demo.controls.textureCB.setSelected(s.endsWith("true")); } else if (args[i].startsWith("-composite=")) { demo.controls.compositeCB.setSelected(s.endsWith("true")); } else if (args[i].startsWith("-verbose")) { demo.controls.verboseCB.setSelected(true); } } } }