DemoGroup.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 
DemoGroup.java :  » Swing » java2d » Java-2D-Examples » Java Open Source
/*
 * @(#)DemoGroup.java  1.41 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.
 */

/*
 * @(#)DemoGroup.java  1.41 06/08/29
 */


package java2d;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


/**
 * DemoGroup handles multiple demos inside of a panel.  Demos are loaded
 * from the demos[][] string as listed in Java2Demo.java.
 * Demo groups can be loaded individually, for example : 
 *      java DemoGroup Fonts
 * Loads all the demos found in the demos/Fonts directory.
 */
public class DemoGroup extends JPanel
    implements MouseListener, ChangeListener, ActionListener {

    static int columns = 2;

    private static Font font = new Font("serif", Font.PLAIN, 10);
    private static EmptyBorder emptyB = new EmptyBorder(5,5,5,5);
    private static BevelBorder bevelB = new BevelBorder(BevelBorder.LOWERED);

    private String groupName;
    public JPanel clonePanels[];
    public JTabbedPane tabbedPane;


    public DemoGroup(String name) {

        groupName = name;

        setLayout(new BorderLayout());

        JPanel p = new JPanel(new GridLayout(0,2));
        p.setBorder(new CompoundBorder(emptyB, bevelB));

        // Find the named demo group in Java2Demo.demos[].
        int index = -1;
        while (!name.equals(Java2Demo.demos[++index][0])) {}
        String[] demos = Java2Demo.demos[index];

        // If there are an odd number of demos, use GridBagLayout.
        // Note that we don't use the first entry.
        int numDemos = demos.length - 1;
        if (numDemos%== 1) {
            p.setLayout(new GridBagLayout());
        

        // For each demo in the group, prepare a DemoPanel.
        for (int i = 1; i <= numDemos; i++) {
            DemoPanel dp = new DemoPanel("java2d.demos."+name+"."+demos[i]);
            dp.setDemoBorder(p);
            if (dp.surface != null) {
                dp.surface.addMouseListener(this);
                dp.surface.setMonitor(Java2Demo.performancemonitor != null);
            
            if (p.getLayout() instanceof GridBagLayout) {
                int x = p.getComponentCount() 2;
                int y = p.getComponentCount() 2;
                int w = (i == numDemos1;
                Java2Demo.addToGridBag(p,dp,x,y,w,1,1,1);
            else {
                p.add(dp);
            }
        }

        add(p);
    }


    public void mouseClicked(MouseEvent e) {

        if (tabbedPane == null) {
            shutDown(getPanel());
            JPanel p = new JPanel(new BorderLayout());
            p.setBorder(new CompoundBorder(emptyB, bevelB));

            tabbedPane = new JTabbedPane();
            tabbedPane.setFont(font);

            JPanel tmpP = (JPanelgetComponent(0);
            tabbedPane.addTab(groupName, tmpP);

            clonePanels = new JPanel[tmpP.getComponentCount()];
            for (int i = 0; i < clonePanels.length; i++) {
                clonePanels[inew JPanel(new BorderLayout());
                DemoPanel dp = (DemoPaneltmpP.getComponent(i);
                DemoPanel c = new DemoPanel(dp.className);
                c.setDemoBorder(clonePanels[i]);
                if (c.surface != null) {
                    c.surface.setMonitor(Java2Demo.performancemonitor != null);
                    Image cloneImg = DemoImages.getImage("clone.gif"this);
                    c.tools.cloneB = 
                        c.tools.addTool(cloneImg,"Clone the Surface",this);
                    Dimension d = c.tools.toolbar.getPreferredSize();
                    c.tools.toolbar.setPreferredSize(
                        new Dimension(d.width+27, d.height));
                    if (Java2Demo.backgroundColor != null) {
                        c.surface.setBackground(Java2Demo.backgroundColor);
                    }
                
                clonePanels[i].add(c);
                String s = dp.className.substring(dp.className.indexOf('.')+1);
                tabbedPane.addTab(s, clonePanels[i]);
            }
            p.add(tabbedPane);
            remove(tmpP);
            add(p);

            tabbedPane.addChangeListener(this);
            revalidate();
        }

        String className = e.getComponent().toString();
        className = className.substring(0, className.indexOf('['));

        for (int i = 0; i < tabbedPane.getTabCount(); i++) {
            String s1 = className.substring(className.indexOf('.')+1);
            if (tabbedPane.getTitleAt(i).equals(s1)) {
                tabbedPane.setSelectedIndex(i);
                break;
            }
        }

        revalidate();
    }

    public void mousePressed (MouseEvent e) { }
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered (MouseEvent e) { }
    public void mouseExited  (MouseEvent e) { }


    public void actionPerformed(ActionEvent e) {
        JButton b = (JButtone.getSource();
        if (b.getToolTipText().startsWith("Clone")) {
            cloneDemo();
        else {
            removeClone(b.getParent().getParent().getParent().getParent());
        }
    }


    private int index;

    public void stateChanged(ChangeEvent e) {
        shutDown((JPaneltabbedPane.getComponentAt(index));
        index = tabbedPane.getSelectedIndex();
        setup(false);
    }


    public JPanel getPanel() {
        if (tabbedPane != null) {
            return (JPaneltabbedPane.getSelectedComponent();
        else {
            return (JPanelgetComponent(0);
        }
    }


    public void setup(boolean issueRepaint) {

        JPanel p = getPanel();

        // Let PerformanceMonitor know which demos are running
        if (Java2Demo.performancemonitor != null) {
            Java2Demo.performancemonitor.surf.setPanel(p);
            Java2Demo.performancemonitor.surf.setSurfaceState();
        }

        GlobalControls c = Java2Demo.controls;
        // .. tools check against global controls settings ..
        // .. & start demo & custom control thread if need be ..
        for (int i = 0; i < p.getComponentCount(); i++) {
            DemoPanel dp = (DemoPanelp.getComponent(i);
            if (dp.surface != null && c != null) {
                Tools t = dp.tools;
                t.setVisible(isValid());
                t.issueRepaint = issueRepaint;
                JToggleButton b[] {t.toggleB, t.aliasB, t.renderB,
                               t.textureB, t.compositeB};
                JCheckBox cb[] {c.toolBarCB, c.aliasCB, c.renderCB,
                                  c.textureCB, c.compositeCB};
                for (int j = 0; j < b.length; j++) {
                    if (c.obj != null && c.obj.equals(cb[j])) {
                        if (b[j].isSelected() != cb[j].isSelected()) {
                            b[j].doClick();
                        }
                    else if (c.obj == null) {
                        if (b[j].isSelected() != cb[j].isSelected()) {
                            b[j].doClick();
                        }
                    }
                }
                t.setVisible(true);
                if (c.screenCombo.getSelectedIndex() != t.screenCombo.getSelectedIndex()) 
                {
                    t.screenCombo.setSelectedIndex(c.screenCombo.getSelectedIndex());
                }
                if (Java2Demo.verboseCB.isSelected()) {
                    dp.surface.verbose();
                }
                dp.surface.setSleepAmount(c.slider.getValue());
                if (Java2Demo.backgroundColor != null) {
                    dp.surface.setBackground(Java2Demo.backgroundColor);
                }
                t.issueRepaint = true;
            }
            dp.start();
        
        revalidate();
    }


    public void shutDown(JPanel p) {
        for (int i = 0; i < p.getComponentCount(); i++) {
            ((DemoPanelp.getComponent(i)).stop();
        
        System.gc();
    }


    public void cloneDemo() {
        JPanel panel = clonePanels[tabbedPane.getSelectedIndex() 1];
        if (panel.getComponentCount() == 1) {
            panel.invalidate();
            panel.setLayout(new GridLayout(0,columns,5,5));
            panel.revalidate();
        }
        DemoPanel original = (DemoPanelgetPanel().getComponent(0);
        DemoPanel clone = new DemoPanel(original.className);
        if (columns == 2) {
            clone.setDemoBorder(panel);
        }
        Image removeImg = DemoImages.getImage("remove.gif"this);
        clone.tools.cloneB = 
              clone.tools.addTool(removeImg,"Remove the Surface",this);
        Dimension d = clone.tools.toolbar.getPreferredSize();
        clone.tools.toolbar.setPreferredSize(
                        new Dimension(d.width+27, d.height));
        if (Java2Demo.backgroundColor != null) {
            clone.surface.setBackground(Java2Demo.backgroundColor);
        }
        if (Java2Demo.controls != null) {
            if (clone.tools.isExpanded 
                != Java2Demo.controls.toolBarCB.isSelected())
            {
                clone.tools.toggleB.doClick();
            
        }
        clone.start();
        clone.surface.setMonitor(Java2Demo.performancemonitor != null);
        panel.add(clone);
        panel.repaint();
        panel.revalidate();
    }


    public void removeClone(Component theClone) {
        JPanel panel = clonePanels[tabbedPane.getSelectedIndex() 1];
        if (panel.getComponentCount() == 2) {
            Component cmp = panel.getComponent(0);
            panel.removeAll();
            panel.setLayout(new BorderLayout());
            panel.revalidate();
            panel.add(cmp);
        else {
            panel.remove(theClone);
            int cmpCount = panel.getComponentCount();
            for (int j = 1; j < cmpCount; j++) {
                int top  =  (j+>= 3)      5;
                int left = ((j+12== 5;
                EmptyBorder eb = new EmptyBorder(top,left,5,5);
                SoftBevelBorder sbb = new SoftBevelBorder(BevelBorder.RAISED);
                JPanel p = (JPanelpanel.getComponent(j);
                p.setBorder(new CompoundBorder(eb, sbb));
            }
        }
        panel.repaint();
        panel.revalidate();
    }


    public static void main(String args[]) {
        final DemoGroup group = new DemoGroup(args[0]);
        JFrame f = new JFrame("Java2D Demo - DemoGroup");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
            public void windowDeiconified(WindowEvent e) { group.setup(false)}
            public void windowIconified(WindowEvent e) { 
                group.shutDown(group.getPanel())
            }
        });
        f.getContentPane().add("Center", group);
        f.pack();
        int WIDTH  = 620;
        int HEIGHT = 530;
        f.setSize(WIDTH, HEIGHT);
        f.setLocationRelativeTo(null);  // centers f on screen
        f.setVisible(true);
        for (String arg : args) {
            if (arg.startsWith("-ccthread")) {
                Java2Demo.ccthreadCB = new JCheckBoxMenuItem("CCThread"true);
            }
        }
        group.setup(false);
    }
}
 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_av___a__2__s._co_m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.