home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / jfc / Java2D / src / java2d / Tools.java < prev   
Encoding:
Java Source  |  2002-09-06  |  15.1 KB  |  448 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)Tools.java    1.35 02/06/13
  38.  */
  39.  
  40. package java2d;
  41.  
  42. import java.awt.*;
  43. import java.awt.event.*;
  44. import java.awt.print.PrinterJob;
  45. import javax.print.attribute.*;
  46. import javax.swing.*;
  47. import javax.swing.event.*;
  48. import javax.swing.border.*;
  49. import java.net.URL;
  50.  
  51.  
  52. /**
  53.  * Tools to control individual demo graphic attributes.  Also, control for
  54.  * start & stop on animated demos; control for cloning the demo; control for
  55.  * printing the demo.  Expand and collapse the Tools panel with ToggleIcon.
  56.  */
  57. public class Tools extends JPanel implements ActionListener, ChangeListener, MouseListener, Runnable {
  58.  
  59.     private ImageIcon stopIcon, startIcon;
  60.     private Font font = new Font("serif", Font.PLAIN, 10);
  61.     private Color roColor = new Color(204, 204, 255);
  62.     private Surface surface;
  63.     private Thread thread;
  64.     private JPanel toolbarPanel;
  65.     private JPanel sliderPanel;
  66.     private JLabel label;
  67.     private ToggleIcon bumpyIcon, rolloverIcon;
  68.  
  69.     protected boolean focus;
  70.  
  71.     public JButton toggleB;
  72.     public JButton printB;
  73.     public JComboBox screenCombo;
  74.     public JButton renderB, aliasB;
  75.     public JButton textureB, compositeB;
  76.     public JButton startStopB;
  77.     public JButton cloneB;
  78.     public boolean issueRepaint = true;
  79.     public JToolBar toolbar;
  80.     public JSlider slider;
  81.     public boolean doSlider;
  82.     public boolean isExpanded;
  83.  
  84.     public Tools(Surface surface) {
  85.         this.surface = surface;
  86.         setBackground(Color.gray);
  87.         setLayout(new BorderLayout());
  88.  
  89.         stopIcon = new ImageIcon(DemoImages.getImage("stop.gif", this));
  90.         startIcon = new ImageIcon(DemoImages.getImage("start.gif",this));
  91.         bumpyIcon = new ToggleIcon(this, Color.lightGray);
  92.         rolloverIcon = new ToggleIcon(this, roColor);
  93.         toggleB = new JButton(bumpyIcon);
  94.         toggleB.addMouseListener(this);
  95.         isExpanded = false;
  96.         toggleB.addActionListener(this);
  97.         toggleB.setMargin(new Insets(0,0,-4,0));
  98.         toggleB.setBorderPainted(false);
  99.         toggleB.setFocusPainted(false);
  100.         toggleB.setContentAreaFilled(false);
  101.         toggleB.setRolloverIcon(rolloverIcon);
  102.         add("North", toggleB);
  103.  
  104.  
  105.         toolbar = new JToolBar();
  106.         toolbar.setPreferredSize(new Dimension(100, 26));
  107.         toolbar.setFloatable(false);
  108.  
  109.         String s = surface.AntiAlias == RenderingHints.VALUE_ANTIALIAS_ON 
  110.             ? "On" : "Off";
  111.         aliasB = addTool( "A", "Antialiasing " + s, this);
  112.  
  113.         s = surface.Rendering == RenderingHints.VALUE_RENDER_SPEED
  114.             ? "Speed" : "Quality";
  115.         renderB = addTool("R", "Rendering " + s, this);
  116.  
  117.         s = surface.texture != null ? "On" : "Off";
  118.         textureB = addTool("T", "Texture " + s, this);
  119.  
  120.         s = surface.composite != null ? "On" : "Off";
  121.         compositeB = addTool("C", "Composite " + s, this);
  122.  
  123.         printB = addTool("print.gif", "Print the Surface", this);
  124.  
  125.         if (surface instanceof AnimatingSurface) {
  126.             startStopB = addTool("stop.gif", "Stop Animation", this);
  127.             toolbar.setPreferredSize(new Dimension(122, 26));
  128.         }
  129.  
  130.         screenCombo = new JComboBox();
  131.         screenCombo.setPreferredSize(new Dimension(100, 18));
  132.         screenCombo.setFont(font);
  133.         for (int i = 0; i < GlobalControls.screenNames.length; i++) {
  134.             screenCombo.addItem(GlobalControls.screenNames[i]);
  135.         } 
  136.         screenCombo.addActionListener(this);
  137.         toolbarPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
  138.         toolbarPanel.setBackground(Color.gray);
  139.         toolbarPanel.setLocation(0,6);
  140.         toolbarPanel.setVisible(false);
  141.         toolbarPanel.add(toolbar);
  142.         toolbarPanel.add(screenCombo);
  143.         add(toolbarPanel);
  144.  
  145.         setPreferredSize(new Dimension(200,6));
  146.  
  147.         if (surface instanceof AnimatingSurface) {
  148.             sliderPanel = new JPanel(new GridLayout(0,2,5,5));
  149.             sliderPanel.setBackground(Color.gray);
  150.             label = new JLabel("sleep = 30 ms");
  151.             label.setForeground(Color.black);
  152.             sliderPanel.add(label);
  153.             slider = new JSlider(JSlider.HORIZONTAL, 0, 200, 30);
  154.             slider.setBackground(Color.gray);
  155.             slider.addChangeListener(this);
  156.             TitledBorder tb = new TitledBorder(new EtchedBorder());
  157.             tb.setTitleFont(new Font("serif", Font.PLAIN, 8));
  158.             tb.setTitle("sleep = 30 ms");
  159.             EmptyBorder eb = new EmptyBorder(4,5,4,5);
  160.             slider.setBorder(new CompoundBorder(eb, new EtchedBorder()));
  161.             sliderPanel.add(slider);
  162.  
  163.             addMouseListener(new MouseAdapter() {
  164.                 public void mouseClicked(MouseEvent e) {
  165.                    if (toolbarPanel.isVisible()) {
  166.                        invalidate();
  167.                        if ((doSlider = !doSlider)) {
  168.                            remove(toolbarPanel);
  169.                            add(sliderPanel);
  170.                        } else {
  171.                            remove(sliderPanel);
  172.                            add(toolbarPanel);
  173.                        }
  174.                        validate();
  175.                        repaint();
  176.                    }
  177.                 }
  178.             });
  179.         }
  180.     }
  181.  
  182.  
  183.     public JButton addTool(String name, 
  184.                            String toolTip,
  185.                            ActionListener al) {
  186.         JButton b = null;
  187.         if (name.indexOf(".") == -1) {
  188.             b = (JButton) toolbar.add(new JButton(name));
  189.             if (toolTip.equals("Rendering Quality") ||
  190.                     toolTip.equals("Antialiasing On") ||
  191.                         toolTip.equals("Texture On")  ||
  192.                             toolTip.equals("Composite On")) {
  193.                 b.setBackground(Color.green);
  194.                 b.setSelected(true);
  195.             } else {
  196.                 b.setBackground(Color.lightGray);
  197.                 b.setSelected(false);
  198.             }
  199.             b.setPreferredSize(new Dimension(18, 22));
  200.             b.setMaximumSize(new Dimension(18, 22));
  201.             b.setMinimumSize(new Dimension(18, 22));
  202.         } else {
  203.             Image img = DemoImages.getImage(name, this);
  204.             b = (JButton) toolbar.add(new JButton(new ImageIcon(img)));
  205.             b.setSelected(true);
  206.         }
  207.         b.setToolTipText(toolTip);
  208.         b.addActionListener(al);
  209.         return b;
  210.     }
  211.  
  212.  
  213.     public void actionPerformed(ActionEvent e) {
  214.         Object obj = e.getSource();
  215.     if (obj instanceof JButton) {
  216.             JButton b = (JButton) obj;
  217.             b.setSelected(!b.isSelected());
  218.             if (b.getIcon() == null) {
  219.                 b.setBackground(b.isSelected() ? Color.green : Color.lightGray);
  220.             }
  221.         }
  222.         if (obj.equals(toggleB)) {
  223.         isExpanded = !isExpanded;
  224.             if (isExpanded) {
  225.                 setPreferredSize(new Dimension(200,38));
  226.             } else {
  227.                 setPreferredSize(new Dimension(200,6));
  228.             }
  229.             toolbarPanel.setVisible(isExpanded);
  230.             if (sliderPanel != null) {
  231.                 sliderPanel.setVisible(isExpanded);
  232.             }
  233.             getParent().validate();
  234.             toggleB.getModel().setRollover(false);
  235.             return;
  236.         }
  237.         if (obj.equals(printB)) {
  238.             start();
  239.             return;
  240.         }
  241.        
  242.         if (obj.equals(startStopB)) {
  243.             if (startStopB.getToolTipText().equals("Stop Animation")) {
  244.                 startStopB.setIcon(startIcon);
  245.                 startStopB.setToolTipText("Start Animation");
  246.                 surface.animating.stop();
  247.             } else {
  248.                 startStopB.setIcon(stopIcon);
  249.                 startStopB.setToolTipText("Stop Animation");
  250.                 surface.animating.start();
  251.             }
  252.         } else if (obj.equals(aliasB)) {
  253.             if (aliasB.getToolTipText().equals("Antialiasing On")) {
  254.                 aliasB.setToolTipText("Antialiasing Off");
  255.             } else {
  256.                 aliasB.setToolTipText("Antialiasing On");
  257.             }
  258.             surface.setAntiAlias(aliasB.isSelected());
  259.         } else if (obj.equals(renderB)) {
  260.             if (renderB.getToolTipText().equals("Rendering Quality")) {
  261.                 renderB.setToolTipText("Rendering Speed");
  262.             } else {
  263.                 renderB.setToolTipText("Rendering Quality");
  264.             }
  265.             surface.setRendering(renderB.isSelected());
  266.         } else if (obj.equals(textureB)) {
  267.             Object texture = null;
  268.             if (textureB.getToolTipText().equals("Texture On")) {
  269.                 textureB.setToolTipText("Texture Off");
  270.                 surface.setTexture(null);
  271.                 surface.clearSurface = true;
  272.             } else {
  273.                 textureB.setToolTipText("Texture On");
  274.                 surface.setTexture(TextureChooser.texture);
  275.             }
  276.         } else if (obj.equals(compositeB)) {
  277.             if (compositeB.getToolTipText().equals("Composite On")) {
  278.                 compositeB.setToolTipText("Composite Off");
  279.             } else {
  280.                 compositeB.setToolTipText("Composite On");
  281.             }
  282.             surface.setComposite(compositeB.isSelected());
  283.         } else if (obj.equals(screenCombo)) {
  284.             surface.setImageType(screenCombo.getSelectedIndex());
  285.         }
  286.  
  287.         if (issueRepaint && surface.animating != null) {
  288.             if (surface.getSleepAmount() != 0) {
  289.                 if (surface.animating.thread != null) {
  290.                     surface.animating.thread.interrupt();
  291.                 }
  292.             }
  293.         } else if (issueRepaint) {
  294.             surface.repaint();
  295.         }
  296.     }
  297.  
  298.  
  299.     public void stateChanged(ChangeEvent e) {
  300.         int value = slider.getValue();
  301.         label.setText("sleep = " + String.valueOf(value) + " ms");
  302.         label.repaint();
  303.         surface.setSleepAmount(value);
  304.     }
  305.  
  306.  
  307.     public void mouseClicked(MouseEvent e) {
  308.     }
  309.  
  310.     public void mousePressed(MouseEvent e) {
  311.     }
  312.  
  313.     public void mouseReleased(MouseEvent e) {
  314.     }
  315.  
  316.     public void mouseEntered(MouseEvent e) {
  317.         focus = true;
  318.         bumpyIcon.start();
  319.     }
  320.  
  321.     public void mouseExited(MouseEvent e) {
  322.         focus = false;
  323.         bumpyIcon.stop();
  324.     }
  325.  
  326.  
  327.     public void start() {
  328.         thread = new Thread(this);
  329.         thread.setPriority(Thread.MAX_PRIORITY);
  330.         thread.setName("Printing " + surface.name);
  331.         thread.start();
  332.     }
  333.  
  334.  
  335.     public synchronized void stop() {
  336.         thread = null;
  337.         notifyAll();
  338.     }
  339.  
  340.  
  341.     public void run() {
  342.         boolean stopped = false;
  343.         if (surface.animating != null && surface.animating.thread != null) {
  344.             stopped = true;
  345.             startStopB.doClick();
  346.         }
  347.  
  348.         try {
  349.             PrinterJob printJob = PrinterJob.getPrinterJob();
  350.             printJob.setPrintable(surface);
  351.             boolean pDialogState = true;
  352.             PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
  353.  
  354.             if (!Java2Demo.printCB.isSelected()) {
  355.                 pDialogState = printJob.printDialog(aset);
  356.             }
  357.             if (pDialogState) {
  358.                 printJob.print(aset);
  359.             }
  360.         } catch (java.security.AccessControlException ace) {
  361.             String errmsg = "Applet access control exception; to allow " +
  362.                             "access to printer, run policytool and set\n" +
  363.                             "permission for \"queuePrintJob\" in " +
  364.                             "RuntimePermission.";
  365.             JOptionPane.showMessageDialog(this, errmsg, "Printer Access Error",
  366.                                           JOptionPane.ERROR_MESSAGE);
  367.         } catch (Exception ex) {
  368.             ex.printStackTrace();
  369.         }
  370.  
  371.         if (stopped) {
  372.             startStopB.doClick();
  373.         }
  374.         thread = null;
  375.     }
  376.  
  377.  
  378.     /**
  379.      * Expand and Collapse the Tools Panel with this bumpy button.
  380.      */
  381.     static class ToggleIcon implements Icon, Runnable {
  382.  
  383.         private Color topColor = new Color(153, 153, 204);
  384.         private Color shadowColor = new Color(102, 102, 153);
  385.         private Color backColor = new Color(204, 204, 255);
  386.         private Tools tools;
  387.         private Thread thread;
  388.         private Color fillColor;
  389.  
  390.  
  391.         public ToggleIcon(Tools tools, Color fillColor) {
  392.             this.tools = tools;
  393.             this.fillColor = fillColor;
  394.         }
  395.  
  396.     
  397.         public void paintIcon(Component c, Graphics g, int x, int y ) {
  398.         int w = getIconWidth();
  399.         int h = getIconHeight();
  400.         g.setColor(fillColor);
  401.         g.fillRect(0, 0, w, h);
  402.             for (; x < w-2; x+=4) {
  403.                 g.setColor(Color.white);
  404.                 g.fillRect(x, 1, 1, 1);
  405.                 g.fillRect(x+2, 3, 1, 1);
  406.                 g.setColor(shadowColor);
  407.                 g.fillRect(x+1, 2, 1, 1);
  408.                 g.fillRect(x+3, 4, 1, 1);
  409.             }
  410.         }
  411.  
  412.         public int getIconWidth() {
  413.             return tools.getSize().width;
  414.         }
  415.  
  416.         public int getIconHeight() {
  417.             return 6;
  418.         }
  419.  
  420.  
  421.         public void start() {
  422.             thread = new Thread(this);
  423.             thread.setPriority(Thread.MIN_PRIORITY);
  424.             thread.setName("ToggleIcon");
  425.             thread.start();
  426.         }
  427.  
  428.  
  429.         public synchronized void stop() {
  430.             if (thread != null) {
  431.                 thread.interrupt();
  432.             }
  433.             thread = null;
  434.         }
  435.  
  436.  
  437.         public void run() {
  438.             try {
  439.                thread.sleep(400);
  440.             } catch (InterruptedException e) { }
  441.             if (tools.focus && thread != null) {
  442.                 tools.toggleB.doClick();
  443.             }
  444.             thread = null;
  445.         }
  446.     }
  447. } // End Tools class
  448.