FileDocCategorySizeDatePackage
Java2Demo.javaAPI DocExample20877Wed Aug 08 15:13:48 BST 2001None

Java2Demo

public class Java2Demo extends JPanel implements ActionListener, ItemListener
A demo that shows Java 2D(TM) features.
version
@(#)Java2Demo.java 1.33 99/08/16
author
Brian Lichtenwalter (Framework, Intro, demos)
author
Jim Graham (demos)

Fields Summary
static Java2Demo
demo
static GlobalControls
controls
static MemoryMonitor
memorymonitor
static PerformanceMonitor
performancemonitor
static JTabbedPane
tabbedPane
static JLabel
progressLabel
static JProgressBar
progressBar
static DemoGroup[]
group
static JCheckBoxMenuItem
verboseCB
static JCheckBoxMenuItem
ccthreadCB
static JCheckBoxMenuItem
printCB
static Color
backgroundColor
static JCheckBoxMenuItem
memoryCB
static JCheckBoxMenuItem
perfCB
static Intro
intro
static String[]
demos
private JCheckBoxMenuItem
controlsCB
private JMenuItem
runMI
private JMenuItem
cloneMI
private JMenuItem
fileMI
private JMenuItem
backgMI
private JMenuItem
ccthreadMI
private JMenuItem
verboseMI
private RunWindow
runwindow
private CloningFeature
cloningfeature
private JFrame
rf
private JFrame
cf
private GlobalPanel
gp
Constructors Summary
public Java2Demo()
Construct the Java2D Demo.




             
      

        setLayout(new BorderLayout());
        setBorder(new EtchedBorder());

        add(createMenuBar(), BorderLayout.NORTH);

        // hard coding 14 = 11 demo dirs + images + fonts + Intro
        progressBar.setMaximum(13);   
        progressLabel.setText("Loading images");
        new DemoImages();
        progressBar.setValue(progressBar.getValue() + 1);
        progressLabel.setText("Loading fonts");
        new DemoFonts();
        progressBar.setValue(progressBar.getValue() + 1);
        progressLabel.setText("Loading Intro");
        intro = new Intro();
        progressBar.setValue(progressBar.getValue() + 1);

        controls = new GlobalControls();
        memorymonitor = new MemoryMonitor();
        performancemonitor = new PerformanceMonitor();

        GlobalPanel gp = new GlobalPanel();

        tabbedPane = new JTabbedPane();
        tabbedPane.setFont(new Font("serif", Font.PLAIN, 12));
        tabbedPane.addTab("", new J2DIcon(), gp);
        tabbedPane.addChangeListener(gp);

        group = new DemoGroup[demos.length];
        for (int i = 0; i < demos.length; i++) {
            progressLabel.setText("Loading demos." + demos[i][0]);
            group[i] = new DemoGroup(demos[i][0]);
            tabbedPane.addTab(demos[i][0], null);
            progressBar.setValue(progressBar.getValue() + 1);
        }

        add(tabbedPane, BorderLayout.CENTER);
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

        if (e.getSource().equals(fileMI)) {
            System.exit(0);
        } else if (e.getSource().equals(runMI)) {
            createRunWindow();
        } else if (e.getSource().equals(cloneMI)) {
            if (cloningfeature == null) {
                cloningfeature = new CloningFeature();
                WindowListener l = new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        cloningfeature.stop(); cf.dispose();
                    }
                    public void windowClosed(WindowEvent e) {
                        cloningfeature = null;
                    }
                };
                cf = new JFrame("Cloning Demo");
                cf.addWindowListener(l);
                cf.getContentPane().add("Center", cloningfeature);
                cf.pack();
                cf.setSize(new Dimension(320,330));
                cf.setVisible(true);
            } else {
                cf.toFront();
            }
        } else if (e.getSource().equals(backgMI)) {
	    backgroundColor = 
                JColorChooser.showDialog(this, "Background Color", Color.white);
            for (int i = 1; i < tabbedPane.getTabCount(); i++) {
                JPanel p = group[i-1].getPanel();
                for (int j = 0; j < p.getComponentCount(); j++) {
                    DemoPanel dp = (DemoPanel) p.getComponent(j);
                    if (dp.surface != null) {
                        dp.surface.setBackground(backgroundColor);
                    }
                } 
            } 
        }
    
static voidaddToGridBag(javax.swing.JPanel panel, java.awt.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);
    
private javax.swing.JMenuBarcreateMenuBar()


        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
        JMenuBar menuBar = new JMenuBar();

        if (Java2DemoApplet.applet == null) {
            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());

        ccthreadCB = (JCheckBoxMenuItem) options.add(
                new JCheckBoxMenuItem("Custom Controls Thread"));
        ccthreadCB.addItemListener(this);

        printCB = (JCheckBoxMenuItem) options.add(printCB);

        verboseCB = (JCheckBoxMenuItem) options.add(
                new JCheckBoxMenuItem("Verbose"));

        options.add(new JSeparator());

        backgMI = (JMenuItem) options.add(new JMenuItem("Background Color"));
	backgMI.addActionListener(this);

        runMI = (JMenuItem) options.add(new JMenuItem("Run Window"));
        runMI.addActionListener(this);

        cloneMI = (JMenuItem) options.add(new JMenuItem("Cloning Feature"));
        cloneMI.addActionListener(this);

        return menuBar;
    
public voidcreateRunWindow()

        if (rf != null) {
            rf.toFront();
            return;
        }
        runwindow = new RunWindow();
        WindowListener l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                runwindow.stop(); rf.dispose();
            }
            public void windowClosed(WindowEvent e) { 
                rf = null;
            }
        };
        rf = new JFrame("Run");
        rf.addWindowListener(l);
        rf.getContentPane().add("Center", runwindow);
        rf.pack();
        if (Java2DemoApplet.applet == null) {
            rf.setSize(new Dimension(200,125));
        } else {
            rf.setSize(new Dimension(200,150));
        }
        rf.setVisible(true);
    
public voiditemStateChanged(java.awt.event.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.surf.setVisible(false);
                memorymonitor.surf.stop();
            } else {
                memorymonitor.setVisible(true);
                memorymonitor.surf.setVisible(true);
                memorymonitor.surf.start();
            }
        } else if (e.getSource().equals(perfCB)) {
            if (performancemonitor.isVisible()) {
                performancemonitor.setVisible(false);
                performancemonitor.surf.setVisible(false);
                performancemonitor.surf.stop();
            } else {
                performancemonitor.setVisible(true);
                performancemonitor.surf.setVisible(true);
                performancemonitor.surf.start();
            }
        } else if (e.getSource().equals(ccthreadCB)) {
            int state = ccthreadCB.isSelected() ? 0 : 1;
            if (tabbedPane.getSelectedIndex() != 0) {
                JPanel p = group[tabbedPane.getSelectedIndex()-1].getPanel();
                for (int i = 0; i < p.getComponentCount(); i++) {
                    DemoPanel dp = (DemoPanel) p.getComponent(i);
                    if (dp.ccc != null) {
                        dp.ccc.handleThread(state);
                    }
                } 
            }
        }
        validate();
    
public static voidmain(java.lang.String[] args)

        for (int i = 0; i < args.length; i++) {
            if (args[i].startsWith("-h") || args[i].startsWith("-help")) {
                String s = "\njava -jar Java2Demo.jar -runs=5 -delay=5 -screen=5 " +
                           "-antialias=true -rendering=true -texture=true " +
                           "-composite=true -verbose -print -columns=3 " +
                           "-buffers=5,10 -ccthread -zoom -maxscreen \n";
                System.out.println(s);
                s = "    -runs=5       Number of runs to execute\n" +
                    "    -delay=5      Sleep amount between tabs\n" +
                    "    -antialias=   true or false for antialiasing\n" +
                    "    -rendering=   true or false for quality or speed\n" + 
                    "    -texture=     true or false for texturing\n" +
                    "    -composite=   true or false for compositing\n" +
                    "    -verbose      output Surface graphic states \n" +
                    "    -print        during run print the Surface, " +
                    "use the Default Printer\n" +
                    "    -columns=3    # of columns to use in clone layout \n" +
                    "    -screen=3     demos all use this screen type \n" +
                    "    -buffers=5,10 during run - clone to see screens " +
                    "five through ten\n" +
                    "    -ccthread     Invoke the Custom Controls Thread \n" +
                    "    -zoom         mouseClick on surface for zoom in  \n" +
                    "    -maxscreen    take up the entire monitor screen \n";
                System.out.println(s);
                s = "Examples : \n" +
                    "    Print all of the demos : \n" +
                    "        java -jar Java2Demo.jar -runs=1 -delay=60 -print \n" +
                    "    Run zoomed in with custom control thread \n" +
                    "        java -jar Java2Demo.jar -runs=10 -zoom -ccthread\n"; 
                System.out.println(s);
                System.exit(0);
            } else if (args[i].startsWith("-delay=")) {
                String s = args[i].substring(args[i].indexOf('=")+1);
                RunWindow.delay = Integer.parseInt(s);
            }
        }

        JFrame frame = new JFrame("Java 2D(TM) Demo");
        frame.getAccessibleContext().setAccessibleDescription("A sample application to demonstrate Java2D features");
        int WIDTH = 400, HEIGHT = 200;
        frame.setSize(WIDTH, HEIGHT);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(d.width/2 - WIDTH/2, d.height/2 - HEIGHT/2);
        frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
            public void windowDeiconified(WindowEvent e) { 
                if (demo != null) { demo.start(); }
            }
            public void windowIconified(WindowEvent e) { 
                if (demo != null) { demo.stop(); }
            }
        });
        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 labelSize = new Dimension(400, 20);
        progressLabel = new JLabel("Loading, please wait...");
        progressLabel.setAlignmentX(CENTER_ALIGNMENT);
        progressLabel.setMaximumSize(labelSize);
        progressLabel.setPreferredSize(labelSize);
        progressPanel.add(progressLabel);
        progressPanel.add(Box.createRigidArea(new Dimension(1,20)));

        progressBar = new JProgressBar();
        progressBar.setStringPainted(true);
        progressLabel.setLabelFor(progressBar);
        progressBar.setAlignmentX(CENTER_ALIGNMENT);
        progressBar.setMinimum(0);
        progressBar.setValue(0);
        progressBar.getAccessibleContext().setAccessibleName("Java2D loading progress");
        progressPanel.add(progressBar);

        frame.setVisible(true);

        demo = new Java2Demo();

        frame.getContentPane().removeAll();
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(demo, BorderLayout.CENTER);
        WIDTH = 730; HEIGHT = 570;
        frame.setLocation(d.width/2 - WIDTH/2, d.height/2 - HEIGHT/2);
        frame.setSize(WIDTH, HEIGHT);
        frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

        for (int i = 0; i < args.length; i++) {
            String s = args[i].substring(args[i].indexOf('=")+1);
            if (args[i].startsWith("-runs=")) {
                RunWindow.numRuns = Integer.parseInt(s);
                RunWindow.exit = true;
                demo.createRunWindow();
            } 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.verboseCB.setSelected(true);
            } else if (args[i].startsWith("-print")) {
                demo.printCB.setSelected(true);
                RunWindow.printCB.setSelected(true);
            } else if (args[i].startsWith("-columns=")) {
                DemoGroup.columns = Integer.parseInt(s);
            } else if (args[i].startsWith("-buffers=")) {
                // usage -buffers=3,10
                RunWindow.buffersFlag = true;
                int i1 = args[i].indexOf('=")+1;
                int i2 = args[i].indexOf(',");
                String s1 = args[i].substring(i1, i2);
                RunWindow.bufBeg = Integer.parseInt(s1);
                s1 = args[i].substring(i2+1, args[i].length());
                RunWindow.bufEnd = Integer.parseInt(s1);
            } else if (args[i].startsWith("-ccthread")) {
                demo.ccthreadCB.setSelected(true);
            } else if (args[i].startsWith("-zoom")) {
                RunWindow.zoomCB.setSelected(true);
            } else if (args[i].startsWith("-maxscreen")) {
                frame.setLocation(0, 0);
                frame.setSize(d.width, d.height);
            }
        }

        frame.validate();
        frame.repaint();
        demo.requestDefaultFocus();
        demo.start();

        if (RunWindow.exit) {
            RunWindow.runB.doClick();
        }
    
public voidstart()

        if (tabbedPane.getSelectedIndex() == 0) {
            intro.start();
        } else {
            group[tabbedPane.getSelectedIndex()-1].setup(false);
            if (memorymonitor.surf.thread == null && memoryCB.getState()) {
                memorymonitor.surf.start();
            }
            if (performancemonitor.surf.thread == null && perfCB.getState()) {
                performancemonitor.surf.start();
            }
        }
    
public voidstop()

        if (tabbedPane.getSelectedIndex() == 0) {
            intro.stop();
        } else {
            memorymonitor.surf.stop();
            performancemonitor.surf.stop();
            int i = tabbedPane.getSelectedIndex()-1;
            group[i].shutDown(group[i].getPanel());
        }