Tools.java
Upload User: cdlibang
Upload Date: 2016-07-17
Package Size: 774k
Code Size: 14k
Category:

2D Graphic

Development Platform:

Java

  1. /*
  2.  * @(#)Tools.java 1.27 99/08/16
  3.  *
  4.  * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  * 
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  * 
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30. import java.awt.*;
  31. import java.awt.event.*;
  32. import java.awt.print.PrinterJob;
  33. import javax.swing.*;
  34. import javax.swing.event.*;
  35. import javax.swing.border.*;
  36. import java.net.URL;
  37. /**
  38.  * Tools to control individual demo graphic attributes.  Also, control for
  39.  * start & stop on animated demos; control for cloning the demo; control for
  40.  * printing the demo.  Expand and collapse the Tools panel with ToggleIcon.
  41.  */
  42. public class Tools extends JPanel implements ActionListener, ChangeListener, MouseListener, Runnable {
  43.     private ImageIcon stopIcon, startIcon;
  44.     private Font font = new Font("serif", Font.PLAIN, 10);
  45.     private Color roColor = new Color(204, 204, 255);
  46.     private DemoSurface surface;
  47.     private Thread thread;
  48.     private JPanel toolbarPanel;
  49.     private JPanel sliderPanel;
  50.     private JLabel label;
  51.     private ToggleIcon closedIcon;
  52.     protected boolean focus;
  53.     public JButton toggleB;
  54.     public JButton printB;
  55.     public JComboBox screenCombo;
  56.     public JButton renderB, aliasB;
  57.     public JButton textureB, compositeB;
  58.     public JButton startStopB;
  59.     public JButton cloneB;
  60.     public boolean issueRepaint = true;
  61.     public JToolBar toolbar;
  62.     public JSlider slider;
  63.     public boolean doSlider;
  64.     public Tools(DemoSurface surface) {
  65.         this.surface = surface;
  66.         setBackground(Color.gray);
  67.         setLayout(new BorderLayout());
  68.         stopIcon = new ImageIcon(DemoImages.getImage("stop.gif", this));
  69.         startIcon = new ImageIcon(DemoImages.getImage("start.gif",this));
  70.         closedIcon = new ToggleIcon(this, Color.lightGray);
  71.         toggleB = new JButton(closedIcon);
  72.         toggleB.addMouseListener(this);
  73.         toggleB.setSelected(false);
  74.         toggleB.addActionListener(this);
  75.         toggleB.setMargin(new Insets(0,0,-4,0));
  76.         toggleB.setBorderPainted(false);
  77.         toggleB.setFocusPainted(false);
  78.         toggleB.setContentAreaFilled(false);
  79.         toggleB.setRolloverIcon(new ToggleIcon(this, roColor));
  80.         add("North", toggleB);
  81.         toolbar = new JToolBar();
  82.         toolbar.setPreferredSize(new Dimension(100, 26));
  83.         toolbar.setFloatable(false);
  84.         String s = surface.AntiAlias == RenderingHints.VALUE_ANTIALIAS_ON 
  85.             ? "On" : "Off";
  86.         aliasB = addTool( "A", "Antialiasing " + s, this);
  87.         s = surface.Rendering == RenderingHints.VALUE_RENDER_SPEED
  88.             ? "Speed" : "Quality";
  89.         renderB = addTool("R", "Rendering " + s, this);
  90.         s = surface.texture != null ? "On" : "Off";
  91.         textureB = addTool("T", "Texture " + s, this);
  92.         s = surface.composite != null ? "On" : "Off";
  93.         compositeB = addTool("C", "Composite " + s, this);
  94.         printB = addTool("print.gif", "Print the Surface", this);
  95.         if (Java2DemoApplet.applet != null) {
  96.             printB.setEnabled(false);
  97.         }
  98.         if (surface instanceof AnimatingContext) {
  99.             startStopB = addTool("stop.gif", "Stop Animation", this);
  100.             toolbar.setPreferredSize(new Dimension(122, 26));
  101.         }
  102.         screenCombo = new JComboBox();
  103.         screenCombo.setPreferredSize(new Dimension(100, 18));
  104.         screenCombo.setFont(font);
  105.         for (int i = 0; i < Controls.screenNames.length; i++) {
  106.             screenCombo.addItem(Controls.screenNames[i]);
  107.         } 
  108.         screenCombo.addActionListener(this);
  109.         toolbarPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
  110.         toolbarPanel.setBackground(Color.gray);
  111.         toolbarPanel.setLocation(0,6);
  112.         toolbarPanel.setVisible(false);
  113.         toolbarPanel.add(toolbar);
  114.         toolbarPanel.add(screenCombo);
  115.         add(toolbarPanel);
  116.         setPreferredSize(new Dimension(200,6));
  117.         if (surface instanceof AnimatingContext) {
  118.             sliderPanel = new JPanel(new GridLayout(0,2,5,5));
  119.             sliderPanel.setBackground(Color.gray);
  120.             label = new JLabel("sleep = 30 ms");
  121.             label.setForeground(Color.black);
  122.             sliderPanel.add(label);
  123.             slider = new JSlider(JSlider.HORIZONTAL, 0, 200, 30);
  124.             slider.setBackground(Color.gray);
  125.             slider.addChangeListener(this);
  126.             TitledBorder tb = new TitledBorder(new EtchedBorder());
  127.             tb.setTitleFont(new Font("serif", Font.PLAIN, 8));
  128.             tb.setTitle("sleep = 30 ms");
  129.             EmptyBorder eb = new EmptyBorder(4,5,4,5);
  130.             slider.setBorder(new CompoundBorder(eb, new EtchedBorder()));
  131.             sliderPanel.add(slider);
  132.             addMouseListener(new MouseAdapter() {
  133.                 public void mouseClicked(MouseEvent e) {
  134.                    if (toolbarPanel.isVisible()) {
  135.                        invalidate();
  136.                        if ((doSlider = !doSlider)) {
  137.                            remove(toolbarPanel);
  138.                            add(sliderPanel);
  139.                        } else {
  140.                            remove(sliderPanel);
  141.                            add(toolbarPanel);
  142.                        }
  143.                        validate();
  144.                        repaint();
  145.                    }
  146.                 }
  147.             });
  148.         }
  149.     }
  150.     public JButton addTool(String name, 
  151.                            String toolTip,
  152.                            ActionListener al) {
  153.         JButton b = null;
  154.         if (name.indexOf(".") == -1) {
  155.             b = (JButton) toolbar.add(new JButton(name));
  156.             if (toolTip.equals("Rendering Quality") ||
  157.                     toolTip.equals("Antialiasing On") ||
  158.                         toolTip.equals("Texture On")  ||
  159.                             toolTip.equals("Composite On")) {
  160.                 b.setBackground(Color.green);
  161.                 b.setSelected(true);
  162.             } else {
  163.                 b.setBackground(Color.lightGray);
  164.                 b.setSelected(false);
  165.             }
  166.             b.setPreferredSize(new Dimension(18, 22));
  167.             b.setMaximumSize(new Dimension(18, 22));
  168.             b.setMinimumSize(new Dimension(18, 22));
  169.         } else {
  170.             Image img = DemoImages.getImage(name, this);
  171.             b = (JButton) toolbar.add(new JButton(new ImageIcon(img)));
  172.             b.setSelected(true);
  173.         }
  174.         b.setToolTipText(toolTip);
  175.         b.addActionListener(al);
  176.         return b;
  177.     }
  178.     public void actionPerformed(ActionEvent e) {
  179.         Object obj = e.getSource();
  180.         if (obj.equals(toggleB)) {
  181.             toggleB.setSelected(!toggleB.isSelected());
  182.             if (toggleB.isSelected()) {
  183.                 setPreferredSize(new Dimension(200,38));
  184.             } else {
  185.                 setPreferredSize(new Dimension(200,6));
  186.             }
  187.             toolbarPanel.setVisible(toggleB.isSelected());
  188.             if (sliderPanel != null) {
  189.                 sliderPanel.setVisible(toggleB.isSelected());
  190.             }
  191.             getParent().validate();
  192.             toggleB.getModel().setRollover(false);
  193.             return;
  194.         }
  195.         if (obj.equals(printB)) {
  196.             start();
  197.             return;
  198.         }
  199.         if (obj instanceof JButton) {
  200.             JButton b = (JButton) obj;
  201.             b.setSelected(!b.isSelected());
  202.             if (b.getIcon() == null) {
  203.                 b.setBackground(b.isSelected() ? Color.green : Color.lightGray);
  204.             }
  205.         }
  206.         if (obj.equals(startStopB)) {
  207.             if (startStopB.getToolTipText().equals("Stop Animation")) {
  208.                 startStopB.setIcon(startIcon);
  209.                 startStopB.setToolTipText("Start Animation");
  210.                 surface.stop();
  211.             } else {
  212.                 startStopB.setIcon(stopIcon);
  213.                 startStopB.setToolTipText("Stop Animation");
  214.                 surface.start();
  215.             }
  216.         } else if (obj.equals(aliasB)) {
  217.             if (aliasB.getToolTipText().equals("Antialiasing On")) {
  218.                 aliasB.setToolTipText("Antialiasing Off");
  219.             } else {
  220.                 aliasB.setToolTipText("Antialiasing On");
  221.             }
  222.             surface.setAntiAlias(aliasB.isSelected());
  223.         } else if (obj.equals(renderB)) {
  224.             if (renderB.getToolTipText().equals("Rendering Quality")) {
  225.                 renderB.setToolTipText("Rendering Speed");
  226.             } else {
  227.                 renderB.setToolTipText("Rendering Quality");
  228.             }
  229.             surface.setRendering(renderB.isSelected());
  230.         } else if (obj.equals(textureB)) {
  231.             Object texture = null;
  232.             if (textureB.getToolTipText().equals("Texture On")) {
  233.                 textureB.setToolTipText("Texture Off");
  234.                 surface.setTexture(null);
  235.                 surface.clearSurface = true;
  236.             } else {
  237.                 textureB.setToolTipText("Texture On");
  238.                 surface.setTexture(TextureChooser.texture);
  239.             }
  240.         } else if (obj.equals(compositeB)) {
  241.             if (compositeB.getToolTipText().equals("Composite On")) {
  242.                 compositeB.setToolTipText("Composite Off");
  243.             } else {
  244.                 compositeB.setToolTipText("Composite On");
  245.             }
  246.             surface.setComposite(compositeB.isSelected());
  247.         } else if (obj.equals(screenCombo)) {
  248.             surface.setImageType(screenCombo.getSelectedIndex());
  249.         }
  250.         if (issueRepaint && surface.thread != null) {
  251.             if (surface.getSleepAmount() != 0) {
  252.                 surface.thread.interrupt();
  253.             }
  254.         } else if (issueRepaint) {
  255.             surface.repaint();
  256.         }
  257.     }
  258.     public void stateChanged(ChangeEvent e) {
  259.         int value = slider.getValue();
  260.         label.setText("sleep = " + String.valueOf(value) + " ms");
  261.         label.repaint();
  262.         surface.setSleepAmount(value);
  263.     }
  264.     public void mouseClicked(MouseEvent e) {
  265.     }
  266.     public void mousePressed(MouseEvent e) {
  267.     }
  268.     public void mouseReleased(MouseEvent e) {
  269.     }
  270.     public void mouseEntered(MouseEvent e) {
  271.         focus = true;
  272.         closedIcon.start();
  273.     }
  274.     public void mouseExited(MouseEvent e) {
  275.         focus = false;
  276.         closedIcon.stop();
  277.     }
  278.     public void start() {
  279.         thread = new Thread(this);
  280.         thread.setPriority(Thread.MAX_PRIORITY);
  281.         thread.setName("Printing " + surface.name);
  282.         thread.start();
  283.     }
  284.     public synchronized void stop() {
  285.         thread = null;
  286.         notifyAll();
  287.     }
  288.     public void run() {
  289.         boolean stopped = false;
  290.         if (surface.thread != null) {
  291.             stopped = true;
  292.             startStopB.doClick();
  293.         }
  294.         PrinterJob printJob = PrinterJob.getPrinterJob();
  295.         printJob.setPrintable(surface);
  296.         boolean pDialogState = true;
  297.         if (!Java2Demo.printCB.isSelected()) {
  298.             pDialogState = printJob.printDialog();
  299.         }
  300.         if (pDialogState) {
  301.             try {
  302.                 printJob.print();
  303.             } catch (Exception ex) {
  304.                 ex.printStackTrace();
  305.             }
  306.         }
  307.         if (stopped) {
  308.             startStopB.doClick();
  309.         }
  310.         thread = null;
  311.     }
  312.     /**
  313.      * Expand and Collapse the Tools Panel with this bumpy button.
  314.      */
  315.     static class ToggleIcon implements Icon, Runnable {
  316.         private Color topColor = new Color(153, 153, 204);
  317.         private Color shadowColor = new Color(102, 102, 153);
  318.         private Color backColor = new Color(204, 204, 255);
  319.         private Tools tools;
  320.         private Thread thread;
  321.         private Color fillColor;
  322.         public ToggleIcon(Tools tools, Color fillColor) {
  323.             this.tools = tools;
  324.             this.fillColor = fillColor;
  325.         }
  326.     
  327.         public void paintIcon(Component c, Graphics g, int x, int y ) {
  328.     int w = getIconWidth();
  329.     int h = getIconHeight();
  330.     g.setColor(fillColor);
  331.     g.fillRect(0, 0, w, h);
  332.             for (; x < w-2; x+=4) {
  333.                 g.setColor(Color.white);
  334.                 g.fillRect(x, 1, 1, 1);
  335.                 g.fillRect(x+2, 3, 1, 1);
  336.                 g.setColor(shadowColor);
  337.                 g.fillRect(x+1, 2, 1, 1);
  338.                 g.fillRect(x+3, 4, 1, 1);
  339.             }
  340.         }
  341.         public int getIconWidth() {
  342.             return tools.getSize().width;
  343.         }
  344.         public int getIconHeight() {
  345.             return 6;
  346.         }
  347.         public void start() {
  348.             thread = new Thread(this);
  349.             thread.setPriority(Thread.MIN_PRIORITY);
  350.             thread.setName("ToggleIcon");
  351.             thread.start();
  352.         }
  353.         public synchronized void stop() {
  354.             if (thread != null) {
  355.                 thread.interrupt();
  356.             }
  357.             thread = null;
  358.         }
  359.         public void run() {
  360.             try {
  361.                thread.sleep(400);
  362.             } catch (InterruptedException e) { }
  363.             if (tools.focus && thread != null) {
  364.                 tools.toggleB.doClick();
  365.             }
  366.             thread = null;
  367.         }
  368.     }
  369. } // End Tools class