Java2Demo.java :  » Swing » java2d » Java-2D-Examples » Java Open Source

Java Open Source
1. Apache Common
2. Aspect Oriented
3. Barcode
4. Byte Code
5. Cache
6. Chart
7. Collections Libraries
8. Content Management Systems CMS
9. Crawlers
10. CSV File
11. Database Connection Pools
12. Database Engines
13. Database JDBC Drivers
14. Database Persistence Frameworks
15. Development
16. Development Build Systems
17. Development Code Analyzers
18. Development Code Beautifiers Formatters
19. Development Code Coverage Tools
20. Development IDEs
21. Development Java Source Obfuscators
22. Development JavaDoc
23. Development Text Editor
24. Email Clients
25. Enterprise Service Bus
26. Game Graphic
27. HTML Parsers
28. Installers
29. Inversion of Control Container
30. Issue Tracking
31. J2EE EJB Servers
32. J2EE Frameworks
33. JMS
34. JMX Tools
35. Job Schedulers
36. Logging Tools
37. Network Clients
38. Network Clients Chat
39. Network Servers
40. Open Source System Bloggers
41. Open Source System Financial ERP CRM
42. Open Source System Forum
43. Open Source System Groupware
44. Parser Generators
45. PDF Libraries
46. Portals
47. Profilers
48. Project Management
49. Report
50. RSS RDF
51. Rule Engines
52. Science Library
53. Scripting Languages
54. Search Engines
55. Security
56. Source Control Tools
57. SQL Clients Interface
58. Swing
59. Template Engines
60. Testing Tools
61. UML Modeling
62. Web Development JSP Tag Libraries
63. Web Frameword Ajax
64. Web Frameworks
65. Web Mail Clients
66. Web Servers
67. Web Services Tools
68. Web Testing Tools
69. Wiki Engines
70. Windows Parm
71. Workflow Engines
72. XML
73. XML UI Toolkits
74. Zip Tar
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java Open Source » Swing » java2d 
Java2Demo.java :  » Swing » java2d » Java-2D-Examples » Java Open Source
/*
 * @(#)Java2Demo.java  1.50 06/08/29
 
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 
 * -Redistribution in binary form must reproduce the above copyright notice, 
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission.
 
 * This software is provided "AS IS," without a warranty of any kind. ALL 
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST 
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, 
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */

/*
 * @(#)Java2Demo.java  1.50 06/08/29
 */


package java2d;

import java.awt.*;
import java.awt.event.*;
import java.awt.font.TextLayout;
import java.awt.font.FontRenderContext;
import javax.swing.*;
import javax.swing.border.*;

import static java2d.CustomControlsContext.State.*;


/**
 * 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)
 */
public class Java2Demo extends JPanel implements ItemListener, ActionListener
{

    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 = new JCheckBoxMenuItem("Default Printer");
    static Color backgroundColor;
    static JCheckBoxMenuItem memoryCB, perfCB;
    static Intro intro;
    static String[][] demos = 
    
        {"Arcs_Curves""Arcs""BezierAnim""Curves""Ellipses"},
        {"Clipping""Areas""ClipAnim""Intersection""Text"},
        {"Colors""BullsEye""ColorConvert""Rotator3D"},
        {"Composite""ACimages""ACrules""FadeAnim"},
        {"Fonts""AttributedStr""Highlighting""Outline""Tree"},
        {"Images""DukeAnim""ImageOps""JPEGFlip""WarpImage"},
        {"Lines""Caps""Dash""Joins""LineAnim"},
        {"Mix""Balls""BezierScroller""Stars3D"},
        {"Paint""GradAnim""Gradient""Texture""TextureAnim"},
        {"Paths""Append""CurveQuadTo""FillStroke""WindingRule"},
        {"Transforms""Rotate""SelectTx""TransformAnim"}
    };

    private JCheckBoxMenuItem controlsCB;
    private JMenuItem runMI, cloneMI, fileMI, backgMI;
    private JMenuItem ccthreadMI, verboseMI;
    private RunWindow runwindow;
    private CloningFeature cloningfeature;
    private JFrame rf, cf;
    private GlobalPanel gp;



    /**
     * Construct the Java2D Demo.
     */
    public Java2Demo() {

        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);
        UIManager.put("Button.margin"new Insets(0,0,0,0));
       
        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[inew DemoGroup(demos[i][0]);
            tabbedPane.addTab(demos[i][0]null);
            progressBar.setValue(progressBar.getValue() 1);
        }

        add(tabbedPane, BorderLayout.CENTER);
    }

 
    private JMenuBar createMenuBar() {

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

        if (Java2DemoApplet.applet == null) {
            JMenu file = (JMenumenuBar.add(new JMenu("File"));
            fileMI = (JMenuItemfile.add(new JMenuItem("Exit"));
            fileMI.addActionListener(this);
        }

        JMenu options = (JMenumenuBar.add(new JMenu("Options"));

        controlsCB = (JCheckBoxMenuItemoptions.add(
                new JCheckBoxMenuItem("Global Controls"true));
        controlsCB.addItemListener(this);

        memoryCB = (JCheckBoxMenuItemoptions.add(
                new JCheckBoxMenuItem("Memory Monitor"true));
        memoryCB.addItemListener(this);

        perfCB = (JCheckBoxMenuItemoptions.add(
                new JCheckBoxMenuItem("Performance Monitor"true));
        perfCB.addItemListener(this);

        options.add(new JSeparator());

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

        printCB = (JCheckBoxMenuItemoptions.add(printCB);

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

        options.add(new JSeparator());

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

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

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

        return menuBar;
    }


    public void createRunWindow() {
        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 void startRunWindow() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                runwindow.doRunAction();
            }
        });
    }


    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 (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 = (DemoPanelp.getComponent(j);
                    if (dp.surface != null) {
                        dp.surface.setBackground(backgroundColor);
                    }
                
            
        }
    }


    public void itemStateChanged(ItemEvent e) {
        if (e.getSource().equals(controlsCB)) {
            boolean newVisibility = !controls.isVisible();
            controls.setVisible(newVisibility);
            for (Component cmp : controls.texturechooser.getComponents()) {
                cmp.setVisible(newVisibility);
            }
        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)) {
            CustomControlsContext.State state =
                ccthreadCB.isSelected() ? START : STOP;
            if (tabbedPane.getSelectedIndex() != 0) {
                JPanel p = group[tabbedPane.getSelectedIndex()-1].getPanel();
                for (int i = 0; i < p.getComponentCount(); i++) {
                    DemoPanel dp = (DemoPanelp.getComponent(i);
                    if (dp.ccc != null) {
                        dp.ccc.handleThread(state);
                    }
                
            }
        }
        revalidate();
    }


    public void start() {
        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 void stop() {
        if (tabbedPane.getSelectedIndex() == 0) {
            intro.stop();
        else {
            memorymonitor.surf.stop();
            performancemonitor.surf.stop();
            int i = tabbedPane.getSelectedIndex()-1;
            group[i].shutDown(group[i].getPanel());
        }
    }


    static void addToGridBag(JPanel panel, Component comp,
            int x, int y, int w, int h, double weightx, double weighty) {

        GridBagLayout gbl = (GridBagLayoutpanel.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);
    }


    /**
     * The Icon for the Intro tab.
     */
    static class J2DIcon implements Icon {

        private static Color myBlue  = new Color(94105176)
        private static Color myBlack = new Color(20,  20,  20)
        private static Font font = new Font("serif", Font.BOLD, 12);
        private FontRenderContext frc = new FontRenderContext(null,true,true);
        private TextLayout tl = new TextLayout("Java2D", font, frc);

        public void paintIcon(Component c, Graphics g, int x, int ) {
            Graphics2D g2 = (Graphics2Dg;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setFont(font);
            if (tabbedPane.getSelectedIndex() == 0) {
                g2.setColor(myBlue);
            else {
                g2.setColor(myBlack);
            }
            tl.draw(g2, x, y + 15);
        }

        public int getIconWidth() {
            return 40;
        }

        public int getIconHeight() {
            return 22;
        }
    }



    public static void main(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/- WIDTH/2, d.height/- 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(40020);
        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/- WIDTH/2, d.height/- HEIGHT/2);
        frame.setSize(WIDTH, HEIGHT);
        frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            String s = arg.substring(arg.indexOf('=')+1);
            if (arg.startsWith("-runs=")) {
                RunWindow.numRuns = Integer.parseInt(s);
                RunWindow.exit = true;
                demo.createRunWindow();
            else if (arg.startsWith("-screen=")) {
                demo.controls.screenCombo.setSelectedIndex(Integer.parseInt(s));
            else if (arg.startsWith("-antialias=")) {
                demo.controls.aliasCB.setSelected(s.endsWith("true"));
            else if (arg.startsWith("-rendering=")) {
                demo.controls.renderCB.setSelected(s.endsWith("true"));
            else if (arg.startsWith("-texture=")) {
                demo.controls.textureCB.setSelected(s.endsWith("true"));
            else if (arg.startsWith("-composite=")) {
                demo.controls.compositeCB.setSelected(s.endsWith("true"));
            else if (arg.startsWith("-verbose")) {
                demo.verboseCB.setSelected(true);
            else if (arg.startsWith("-print")) {
                demo.printCB.setSelected(true);
                RunWindow.printCB.setSelected(true);
            else if (arg.startsWith("-columns=")) {
                DemoGroup.columns = Integer.parseInt(s);
            else if (arg.startsWith("-buffers=")) {
                // usage -buffers=3,10
                RunWindow.buffersFlag = true;
                int i1 = arg.indexOf('=')+1;
                int i2 = arg.indexOf(',');
                String s1 = arg.substring(i1, i2);
                RunWindow.bufBeg = Integer.parseInt(s1);
                s1 = arg.substring(i2+1, arg.length());
                RunWindow.bufEnd = Integer.parseInt(s1);
            else if (arg.startsWith("-ccthread")) {
                demo.ccthreadCB.setSelected(true);
            else if (arg.startsWith("-zoom")) {
                RunWindow.zoomCB.setSelected(true);
            else if (arg.startsWith("-maxscreen")) {
                frame.setLocation(00);
                frame.setSize(d.width, d.height);
            }
        }

        frame.validate();
        frame.repaint();
        frame.getFocusTraversalPolicy()
             .getDefaultComponent(frame)
             .requestFocus();
        demo.start();

        if (RunWindow.exit) {
            demo.startRunWindow();
        }
    }
}
 Java-2D-Examples
 Mix
Name Size Time
 demos  2007-11-23
AnimatingControlsSurface.java4 k   2007-08-08
AnimatingSurface.java4 k   2007-08-08
CloningFeature.java7 k   2007-08-08
ControlsSurface.java3 k   2007-08-08
CustomControls.java5 k   2007-08-08
CustomControlsContext.java3 k   2007-08-08
DemoFonts.java4 k   2007-08-28
DemoGroup.java14 k   2007-08-08
DemoImages.java5 k   2007-08-08
DemoPanel.java5 k   2007-08-28
GlobalControls.java6 k   2007-08-08
GlobalPanel.java5 k   2007-08-08
Intro.java63 k   2007-08-08
Java2Demo.java22 k   2007-08-28
Java2DemoApplet.java8 k   2007-08-08
MemoryMonitor.java12 k   2007-08-08
PerformanceMonitor.java7 k   2007-08-08
RunWindow.java13 k   2007-08-08
Surface.java16 k   2007-08-08
TextureChooser.java8 k   2007-08-08
Tools.java16 k   2007-08-08
w__w__w_.j___a_v___a2s___._c___om | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.