Source Code Cross Referenced for PaintTopComponent.java in  » IDE-Netbeans » api » org » netbeans » paint » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » api » org.netbeans.paint 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2007, Sun Microsystems, Inc. All rights reserved.
003:         *
004:         * Redistribution and use in source and binary forms, with or without
005:         * modification, are permitted provided that the following conditions are met:
006:         * 
007:         * * Redistributions of source code must retain the above copyright notice,
008:         *   this list of conditions and the following disclaimer.
009:         * 
010:         * * Redistributions in binary form must reproduce the above copyright notice,
011:         *   this list of conditions and the following disclaimer in the documentation
012:         *   and/or other materials provided with the distribution.
013:         *
014:         * * Neither the name of Sun Microsystems, Inc. nor the names of its contributors
015:         *   may be used to endorse or promote products derived from this software without
016:         *   specific prior written permission.
017:         * 
018:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019:         * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020:         * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021:         * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022:         * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023:         * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024:         * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025:         * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026:         * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027:         * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028:         * THE POSSIBILITY OF SUCH DAMAGE.
029:         */
030:
031:        package org.netbeans.paint;
032:
033:        import java.awt.BorderLayout;
034:        import java.awt.Dimension;
035:        import java.awt.event.ActionEvent;
036:        import java.awt.event.ActionListener;
037:        import java.awt.image.BufferedImage;
038:        import java.io.File;
039:        import java.io.IOException;
040:        import javax.imageio.ImageIO;
041:        import javax.swing.JButton;
042:        import javax.swing.JComponent;
043:        import javax.swing.JFileChooser;
044:        import javax.swing.JLabel;
045:        import javax.swing.JOptionPane;
046:        import javax.swing.JSlider;
047:        import javax.swing.JToolBar;
048:        import javax.swing.event.ChangeEvent;
049:        import javax.swing.event.ChangeListener;
050:        import org.netbeans.swing.colorchooser.ColorChooser;
051:        import org.openide.awt.StatusDisplayer;
052:        import org.openide.util.NbBundle;
053:        import org.openide.windows.TopComponent;
054:
055:        public class PaintTopComponent extends TopComponent implements 
056:                ActionListener, ChangeListener {
057:
058:            private static int tcCount = 0; //A counter to limit number of simultaneously existing images
059:            private static int ct = 0; //A counter we use to provide names for new images
060:
061:            static int getPaintTCCount() {
062:                return tcCount;
063:            }
064:
065:            private final PaintCanvas canvas = new PaintCanvas(); //The component the user draws on
066:            private JComponent preview; //A component in the toolbar that shows the paintbrush size
067:
068:            /** Creates a new instance of PaintTopComponent */
069:            public PaintTopComponent() {
070:                initComponents();
071:                String displayName = NbBundle.getMessage(
072:                        PaintTopComponent.class, "UnsavedImageNameFormat",
073:                        new Object[] { new Integer(ct++) });
074:                tcCount++;
075:                setName(displayName);
076:                setDisplayName(displayName);
077:            }
078:
079:            public void actionPerformed(ActionEvent e) {
080:                if (e.getSource() instanceof  JButton) {
081:                    canvas.clear();
082:                } else if (e.getSource() instanceof  ColorChooser) {
083:                    ColorChooser cc = (ColorChooser) e.getSource();
084:                    canvas.setPaint(cc.getColor());
085:                }
086:                preview.paintImmediately(0, 0, preview.getWidth(), preview
087:                        .getHeight());
088:            }
089:
090:            public void stateChanged(ChangeEvent e) {
091:                JSlider js = (JSlider) e.getSource();
092:                canvas.setDiam(js.getValue());
093:                preview.paintImmediately(0, 0, preview.getWidth(), preview
094:                        .getHeight());
095:            }
096:
097:            public int getPersistenceType() {
098:                return PERSISTENCE_NEVER;
099:            }
100:
101:            private void initComponents() {
102:                setLayout(new BorderLayout());
103:                JToolBar bar = new JToolBar();
104:
105:                ColorChooser fg = new ColorChooser();
106:                preview = canvas.createBrushSizeView();
107:
108:                //Now build our toolbar:
109:
110:                //Make sure components don't get squished:
111:                Dimension min = new Dimension(32, 32);
112:                preview.setMaximumSize(min);
113:                fg.setPreferredSize(new Dimension(16, 16));
114:                fg.setMinimumSize(min);
115:                fg.setMaximumSize(min);
116:
117:                JButton clear = new JButton(NbBundle.getMessage(
118:                        PaintTopComponent.class, "LBL_Clear"));
119:
120:                JLabel fore = new JLabel(NbBundle.getMessage(
121:                        PaintTopComponent.class, "LBL_Foreground"));
122:
123:                fg.addActionListener(this );
124:                clear.addActionListener(this );
125:
126:                JSlider js = new JSlider();
127:                js.setMinimum(1);
128:                js.setMaximum(24);
129:                js.setValue(canvas.getDiam());
130:                js.addChangeListener(this );
131:
132:                fg.setColor(canvas.getColor());
133:
134:                bar.add(clear);
135:                bar.add(fore);
136:                bar.add(fg);
137:                JLabel bsize = new JLabel(NbBundle.getMessage(
138:                        PaintTopComponent.class, "LBL_BrushSize"));
139:
140:                bar.add(bsize);
141:                bar.add(js);
142:                bar.add(preview);
143:
144:                JLabel spacer = new JLabel("   "); //Just a spacer so the brush preview
145:                //isn't stretched to the end of the
146:                //toolbar
147:
148:                spacer.setPreferredSize(new Dimension(400, 24));
149:                bar.add(spacer);
150:
151:                //And install the toolbar and the painting component:
152:                add(bar, BorderLayout.NORTH);
153:                add(canvas, BorderLayout.CENTER);
154:            }
155:
156:            public void saveAs() throws IOException {
157:                JFileChooser ch = new JFileChooser();
158:                if (ch.showSaveDialog(this ) == JFileChooser.APPROVE_OPTION
159:                        && ch.getSelectedFile() != null) {
160:
161:                    File f = ch.getSelectedFile();
162:                    if (!f.getPath().endsWith(".png")) {
163:                        f = new File(f.getPath() + ".png");
164:                    }
165:                    if (!f.exists()) {
166:                        if (!f.createNewFile()) {
167:                            String failMsg = NbBundle.getMessage(
168:                                    PaintTopComponent.class, "MSG_SaveFailed",
169:                                    new Object[] { f.getPath() });
170:                            JOptionPane.showMessageDialog(this , failMsg);
171:                            return;
172:                        }
173:                    } else {
174:                        String overwriteMsg = NbBundle.getMessage(
175:                                PaintTopComponent.class, "MSG_Overwrite",
176:                                new Object[] { f.getPath() });
177:                        if (JOptionPane.showConfirmDialog(this , overwriteMsg) != JOptionPane.OK_OPTION) {
178:
179:                            return;
180:                        }
181:                    }
182:                    doSave(f);
183:                }
184:            }
185:
186:            private void doSave(File f) throws IOException {
187:                BufferedImage img = canvas.getImage();
188:                ImageIO.write(img, "png", f);
189:                String statusMsg = NbBundle.getMessage(PaintTopComponent.class,
190:                        "MSG_Saved", new Object[] { f.getPath() });
191:                StatusDisplayer.getDefault().setStatusText(statusMsg);
192:                setDisplayName(f.getName());
193:            }
194:
195:            protected void componentClosed() {
196:                super.componentClosed();
197:                tcCount--;
198:            }
199:
200:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.