KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > Tools


1 /*
2  * @(#)Tools.java 1.44 06/08/29
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)Tools.java 1.44 06/08/29
39  */

40
41 package java2d;
42
43 import static java.awt.Color JavaDoc.*;
44 import java.awt.*;
45 import java.awt.event.*;
46 import java.awt.print.PrinterJob JavaDoc;
47 import javax.print.attribute.*;
48 import javax.swing.*;
49 import javax.swing.event.*;
50 import javax.swing.border.*;
51 import java.net.URL JavaDoc;
52 import java.text.DecimalFormat JavaDoc;
53
54
55 /**
56  * Tools to control individual demo graphic attributes. Also, control for
57  * start & stop on animated demos; control for cloning the demo; control for
58  * printing the demo. Expand and collapse the Tools panel with ToggleIcon.
59  */

60 public class Tools extends JPanel implements ActionListener, ChangeListener, MouseListener, Runnable JavaDoc {
61
62     private ImageIcon stopIcon, startIcon;
63     private Font font = new Font("serif", Font.PLAIN, 10);
64     private Color JavaDoc roColor = new Color JavaDoc(187, 213, 238);
65     private Surface surface;
66     private Thread JavaDoc thread;
67     private JPanel toolbarPanel;
68     private JPanel sliderPanel;
69     private JLabel label;
70     private ToggleIcon bumpyIcon, rolloverIcon;
71     
72     private DecimalFormat JavaDoc decimalFormat = new DecimalFormat JavaDoc("000");
73
74     protected boolean focus;
75
76     public JToggleButton toggleB;
77     public JButton printB;
78     public JComboBox screenCombo;
79     public JToggleButton renderB, aliasB;
80     public JToggleButton textureB, compositeB;
81     public JButton startStopB;
82     public JButton cloneB;
83     public boolean issueRepaint = true;
84     public JToolBar toolbar;
85     public JSlider slider;
86     public boolean doSlider;
87     public boolean isExpanded;
88
89     public Tools(Surface surface) {
90         this.surface = surface;
91         setLayout(new BorderLayout());
92
93         stopIcon = new ImageIcon(DemoImages.getImage( "stop.gif",this));
94         startIcon = new ImageIcon(DemoImages.getImage("start.gif",this));
95         bumpyIcon = new ToggleIcon(this, LIGHT_GRAY);
96         rolloverIcon = new ToggleIcon(this, roColor);
97         toggleB = new JToggleButton(bumpyIcon);
98         toggleB.addMouseListener(this);
99         isExpanded = false;
100         toggleB.addActionListener(this);
101         toggleB.setMargin(new Insets(0,0,-4,0));
102         toggleB.setBorderPainted(false);
103         toggleB.setFocusPainted(false);
104         toggleB.setContentAreaFilled(false);
105         toggleB.setRolloverIcon(rolloverIcon);
106         add("North", toggleB);
107
108         toolbar = new JToolBar();
109         toolbar.setPreferredSize(new Dimension(112, 26));
110         toolbar.setFloatable(false);
111
112         String JavaDoc s = surface.AntiAlias == RenderingHints.VALUE_ANTIALIAS_ON
113             ? "On" : "Off";
114         aliasB = addTool( "A", "Antialiasing " + s, this);
115
116         s = surface.Rendering == RenderingHints.VALUE_RENDER_SPEED
117             ? "Speed" : "Quality";
118         renderB = addTool("R", "Rendering " + s, this);
119
120         s = surface.texture != null ? "On" : "Off";
121         textureB = addTool("T", "Texture " + s, this);
122
123         s = surface.composite != null ? "On" : "Off";
124         compositeB = addTool("C", "Composite " + s, this);
125
126         Image printBImg = DemoImages.getImage("print.gif", this);
127         printB = addTool(printBImg, "Print the Surface", this);
128
129         if (surface instanceof AnimatingSurface) {
130             Image stopImg = DemoImages.getImage("stop.gif", this);
131             startStopB = addTool(stopImg, "Stop Animation", this);
132             toolbar.setPreferredSize(new Dimension(132, 26));
133         }
134
135         screenCombo = new JComboBox();
136         screenCombo.setPreferredSize(new Dimension(100, 18));
137         screenCombo.setFont(font);
138         for (String JavaDoc name : GlobalControls.screenNames) {
139             screenCombo.addItem(name);
140         }
141         screenCombo.addActionListener(this);
142         toolbarPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
143         toolbarPanel.setLocation(0,6);
144         toolbarPanel.setVisible(false);
145         toolbarPanel.add(toolbar);
146         toolbarPanel.add(screenCombo);
147         toolbarPanel.setBorder(new EtchedBorder());
148         add(toolbarPanel);
149
150         setPreferredSize(new Dimension(200,8));
151
152         if (surface instanceof AnimatingSurface) {
153             sliderPanel = new JPanel(new BorderLayout());
154             label = new JLabel(" Sleep = 030 ms");
155             label.setForeground(BLACK);
156             sliderPanel.add(label, BorderLayout.WEST);
157             slider = new JSlider(JSlider.HORIZONTAL, 0, 200, 30);
158             slider.addChangeListener(this);
159             sliderPanel.setBorder(new EtchedBorder());
160             sliderPanel.add(slider);
161
162             addMouseListener(new MouseAdapter() {
163                 public void mouseClicked(MouseEvent e) {
164                    if (toolbarPanel.isVisible()) {
165                        invalidate();
166                        if ((doSlider = !doSlider)) {
167                            remove(toolbarPanel);
168                            add(sliderPanel);
169                        } else {
170                            remove(sliderPanel);
171                            add(toolbarPanel);
172                        }
173                        validate();
174                        repaint();
175                    }
176                 }
177             });
178         }
179     }
180
181
182     public JButton addTool(Image img,
183                            String JavaDoc toolTip,
184                            ActionListener al) {
185         JButton b = new JButton(new ImageIcon(img)) {
186             Dimension prefSize = new Dimension(21, 22);
187             public Dimension getPreferredSize() {
188                 return prefSize;
189             }
190             public Dimension getMaximumSize() {
191                 return prefSize;
192             }
193             public Dimension getMinimumSize() {
194                 return prefSize;
195             }
196         };
197         toolbar.add(b);
198         b.setFocusPainted(false);
199         b.setSelected(true);
200         b.setToolTipText(toolTip);
201         b.addActionListener(al);
202         return b;
203     }
204
205     public JToggleButton addTool(String JavaDoc name,
206                                  String JavaDoc toolTip,
207                                  ActionListener al) {
208         JToggleButton b = new JToggleButton(name) {
209             Dimension prefSize = new Dimension(21, 22);
210             public Dimension getPreferredSize() {
211                 return prefSize;
212             }
213             public Dimension getMaximumSize() {
214                 return prefSize;
215             }
216             public Dimension getMinimumSize() {
217                 return prefSize;
218             }
219         };
220         toolbar.add(b);
221         b.setFocusPainted(false);
222         if (toolTip.equals("Rendering Quality") ||
223             toolTip.equals("Antialiasing On") ||
224             toolTip.equals("Texture On") ||
225             toolTip.equals("Composite On")) {
226             b.setSelected(true);
227         } else {
228             b.setSelected(false);
229         }
230         b.setToolTipText(toolTip);
231         b.addActionListener(al);
232         return b;
233     }
234
235
236     public void actionPerformed(ActionEvent e) {
237         Object JavaDoc obj = e.getSource();
238     if (obj instanceof JButton) {
239             JButton b = (JButton) obj;
240             b.setSelected(!b.isSelected());
241             if (b.getIcon() == null) {
242                 b.setBackground(b.isSelected() ? GREEN : LIGHT_GRAY);
243             }
244         }
245         if (obj.equals(toggleB)) {
246         isExpanded = !isExpanded;
247             if (isExpanded) {
248                 setPreferredSize(new Dimension(200,38));
249             } else {
250                 setPreferredSize(new Dimension(200,6));
251             }
252             toolbarPanel.setVisible(isExpanded);
253             if (sliderPanel != null) {
254                 sliderPanel.setVisible(isExpanded);
255             }
256             getParent().validate();
257             toggleB.getModel().setRollover(false);
258             return;
259         }
260         if (obj.equals(printB)) {
261             start();
262             return;
263         }
264        
265         if (obj.equals(startStopB)) {
266             if (startStopB.getToolTipText().equals("Stop Animation")) {
267                 startStopB.setIcon(startIcon);
268                 startStopB.setToolTipText("Start Animation");
269                 surface.animating.stop();
270             } else {
271                 startStopB.setIcon(stopIcon);
272                 startStopB.setToolTipText("Stop Animation");
273                 surface.animating.start();
274             }
275         } else if (obj.equals(aliasB)) {
276             if (aliasB.getToolTipText().equals("Antialiasing On")) {
277                 aliasB.setToolTipText("Antialiasing Off");
278             } else {
279                 aliasB.setToolTipText("Antialiasing On");
280             }
281             surface.setAntiAlias(aliasB.isSelected());
282         } else if (obj.equals(renderB)) {
283             if (renderB.getToolTipText().equals("Rendering Quality")) {
284                 renderB.setToolTipText("Rendering Speed");
285             } else {
286                 renderB.setToolTipText("Rendering Quality");
287             }
288             surface.setRendering(renderB.isSelected());
289         } else if (obj.equals(textureB)) {
290             Object JavaDoc texture = null;
291             if (textureB.getToolTipText().equals("Texture On")) {
292                 textureB.setToolTipText("Texture Off");
293                 surface.setTexture(null);
294                 surface.clearSurface = true;
295             } else {
296                 textureB.setToolTipText("Texture On");
297                 surface.setTexture(TextureChooser.texture);
298             }
299         } else if (obj.equals(compositeB)) {
300             if (compositeB.getToolTipText().equals("Composite On")) {
301                 compositeB.setToolTipText("Composite Off");
302             } else {
303                 compositeB.setToolTipText("Composite On");
304             }
305             surface.setComposite(compositeB.isSelected());
306         } else if (obj.equals(screenCombo)) {
307             surface.setImageType(screenCombo.getSelectedIndex());
308         }
309
310         if (issueRepaint && surface.animating != null) {
311             if (surface.getSleepAmount() != 0) {
312                 if (surface.animating.thread != null) {
313                     surface.animating.thread.interrupt();
314                 }
315             }
316         } else if (issueRepaint) {
317             surface.repaint();
318         }
319     }
320
321
322     public void stateChanged(ChangeEvent e) {
323         int value = slider.getValue();
324         label.setText(" Sleep = " + decimalFormat.format(value) + " ms");
325         label.repaint();
326         surface.setSleepAmount(value);
327     }
328
329
330     public void mouseClicked(MouseEvent e) {
331     }
332
333     public void mousePressed(MouseEvent e) {
334     }
335
336     public void mouseReleased(MouseEvent e) {
337     }
338
339     public void mouseEntered(MouseEvent e) {
340         focus = true;
341         bumpyIcon.start();
342     }
343
344     public void mouseExited(MouseEvent e) {
345         focus = false;
346         bumpyIcon.stop();
347     }
348
349
350     public void start() {
351         thread = new Thread JavaDoc(this);
352         thread.setPriority(Thread.MAX_PRIORITY);
353         thread.setName("Printing " + surface.name);
354         thread.start();
355     }
356
357
358     public synchronized void stop() {
359         thread = null;
360         notifyAll();
361     }
362
363
364     public void run() {
365         boolean stopped = false;
366         if (surface.animating != null && surface.animating.thread != null) {
367             stopped = true;
368             startStopB.doClick();
369         }
370
371         try {
372             PrinterJob JavaDoc printJob = PrinterJob.getPrinterJob();
373             printJob.setPrintable(surface);
374             boolean pDialogState = true;
375             PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
376
377             if (!Java2Demo.printCB.isSelected()) {
378                 pDialogState = printJob.printDialog(aset);
379             }
380             if (pDialogState) {
381                 printJob.print(aset);
382             }
383         } catch (java.security.AccessControlException JavaDoc ace) {
384             String JavaDoc errmsg = "Applet access control exception; to allow " +
385                             "access to printer, run policytool and set\n" +
386                             "permission for \"queuePrintJob\" in " +
387                             "RuntimePermission.";
388             JOptionPane.showMessageDialog(this, errmsg, "Printer Access Error",
389                                           JOptionPane.ERROR_MESSAGE);
390         } catch (Exception JavaDoc ex) {
391             ex.printStackTrace();
392         }
393
394         if (stopped) {
395             startStopB.doClick();
396         }
397         thread = null;
398     }
399
400
401     /**
402      * Expand and Collapse the Tools Panel with this bumpy button.
403      */

404     static class ToggleIcon implements Icon, Runnable JavaDoc {
405
406         private Color JavaDoc topColor = new Color JavaDoc(153, 153, 204);
407         private Color JavaDoc shadowColor = new Color JavaDoc(102, 102, 153);
408         private Color JavaDoc backColor = new Color JavaDoc(204, 204, 255);
409         private Color JavaDoc fillColor;
410         private Tools tools;
411         private Thread JavaDoc thread;
412
413
414         public ToggleIcon(Tools tools, Color JavaDoc fillColor) {
415             this.tools = tools;
416             this.fillColor = fillColor;
417         }
418
419     
420         public void paintIcon(Component c, Graphics g, int x, int y ) {
421         int w = getIconWidth();
422         int h = getIconHeight();
423         g.setColor(fillColor);
424         g.fillRect(0, 0, w, h);
425             for (; x < w-2; x+=4) {
426                 g.setColor(WHITE);
427                 g.fillRect(x , 1, 1, 1);
428                 g.fillRect(x+2, 3, 1, 1);
429                 g.setColor(shadowColor);
430                 g.fillRect(x+1, 2, 1, 1);
431                 g.fillRect(x+3, 4, 1, 1);
432             }
433         }
434
435         public int getIconWidth() {
436             return tools.getSize().width;
437         }
438
439         public int getIconHeight() {
440             return 6;
441         }
442
443
444         public void start() {
445             thread = new Thread JavaDoc(this);
446             thread.setPriority(Thread.MIN_PRIORITY);
447             thread.setName("ToggleIcon");
448             thread.start();
449         }
450
451
452         public synchronized void stop() {
453             if (thread != null) {
454                 thread.interrupt();
455             }
456             thread = null;
457         }
458
459
460         public void run() {
461             try {
462                thread.sleep(400);
463             } catch (InterruptedException JavaDoc e) { }
464             if (tools.focus && thread != null) {
465                 tools.toggleB.doClick();
466             }
467             thread = null;
468         }
469     }
470 } // End Tools class
471
Popular Tags