1 19 20 package org.netbeans.paint; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Dimension ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.awt.image.BufferedImage ; 27 import java.io.File ; 28 import java.io.IOException ; 29 import javax.imageio.ImageIO ; 30 import javax.swing.JButton ; 31 import javax.swing.JComponent ; 32 import javax.swing.JFileChooser ; 33 import javax.swing.JLabel ; 34 import javax.swing.JOptionPane ; 35 import javax.swing.JSlider ; 36 import javax.swing.JToolBar ; 37 import javax.swing.event.ChangeEvent ; 38 import javax.swing.event.ChangeListener ; 39 import org.netbeans.swing.colorchooser.ColorChooser; 40 import org.openide.awt.StatusDisplayer; 41 import org.openide.util.NbBundle; 42 import org.openide.windows.TopComponent; 43 44 public class PaintTopComponent extends TopComponent implements ActionListener , ChangeListener { 45 46 private static int tcCount = 0; private static int ct = 0; 49 static int getPaintTCCount() { 50 return tcCount; 51 } 52 53 private final PaintCanvas canvas = new PaintCanvas(); private JComponent preview; 56 57 public PaintTopComponent() { 58 initComponents(); 59 String displayName = NbBundle.getMessage( 60 PaintTopComponent.class, 61 "UnsavedImageNameFormat", 62 new Object [] { new Integer (ct++) } 63 ); 64 tcCount++; 65 setName(displayName); 66 setDisplayName(displayName); 67 } 68 69 public void actionPerformed(ActionEvent e) { 70 if (e.getSource() instanceof JButton ) { 71 canvas.clear(); 72 } else if (e.getSource() instanceof ColorChooser) { 73 ColorChooser cc = (ColorChooser) e.getSource(); 74 canvas.setPaint(cc.getColor()); 75 } 76 preview.paintImmediately(0, 0, preview.getWidth(), preview.getHeight()); 77 } 78 79 public void stateChanged(ChangeEvent e) { 80 JSlider js = (JSlider ) e.getSource(); 81 canvas.setDiam(js.getValue()); 82 preview.paintImmediately(0, 0, preview.getWidth(), preview.getHeight()); 83 } 84 85 public int getPersistenceType() { 86 return PERSISTENCE_NEVER; 87 } 88 89 private void initComponents() { 90 setLayout(new BorderLayout ()); 91 JToolBar bar = new JToolBar (); 92 93 ColorChooser fg = new ColorChooser(); 94 preview = canvas.createBrushSizeView(); 95 96 98 Dimension min = new Dimension (32, 32); 100 preview.setMaximumSize(min); 101 fg.setPreferredSize(new Dimension (16, 16)); 102 fg.setMinimumSize(min); 103 fg.setMaximumSize(min); 104 105 JButton clear = new JButton ( 106 NbBundle.getMessage(PaintTopComponent.class, "LBL_Clear")); 107 108 JLabel fore = new JLabel ( 109 NbBundle.getMessage(PaintTopComponent.class, "LBL_Foreground")); 110 111 fg.addActionListener(this); 112 clear.addActionListener(this); 113 114 JSlider js = new JSlider (); 115 js.setMinimum(1); 116 js.setMaximum(24); 117 js.setValue(canvas.getDiam()); 118 js.addChangeListener(this); 119 120 fg.setColor(canvas.getColor()); 121 122 bar.add(clear); 123 bar.add(fore); 124 bar.add(fg); 125 JLabel bsize = new JLabel ( 126 NbBundle.getMessage(PaintTopComponent.class, "LBL_BrushSize")); 127 128 bar.add(bsize); 129 bar.add(js); 130 bar.add(preview); 131 132 JLabel spacer = new JLabel (" "); 136 spacer.setPreferredSize(new Dimension (400, 24)); 137 bar.add(spacer); 138 139 add(bar, BorderLayout.NORTH); 141 add(canvas, BorderLayout.CENTER); 142 } 143 144 public void saveAs() throws IOException { 145 JFileChooser ch = new JFileChooser (); 146 if (ch.showSaveDialog(this) == JFileChooser.APPROVE_OPTION && 147 ch.getSelectedFile() != null) { 148 149 File f = ch.getSelectedFile(); 150 if (!f.getPath().endsWith(".png")) { 151 f = new File (f.getPath() + ".png"); 152 } 153 if (!f.exists()) { 154 if (!f.createNewFile()) { 155 String failMsg = NbBundle.getMessage( 156 PaintTopComponent.class, 157 "MSG_SaveFailed", new Object [] { f.getPath() } 158 ); 159 JOptionPane.showMessageDialog(this, failMsg); 160 return; 161 } 162 } else { 163 String overwriteMsg = NbBundle.getMessage( 164 PaintTopComponent.class, 165 "MSG_Overwrite", new Object [] { f.getPath() } 166 ); 167 if (JOptionPane.showConfirmDialog(this, overwriteMsg) 168 != JOptionPane.OK_OPTION) { 169 170 return; 171 } 172 } 173 doSave(f); 174 } 175 } 176 177 private void doSave(File f) throws IOException { 178 BufferedImage img = canvas.getImage(); 179 ImageIO.write(img, "png", f); 180 String statusMsg = NbBundle.getMessage(PaintTopComponent.class, 181 "MSG_Saved", new Object [] { f.getPath() }); 182 StatusDisplayer.getDefault().setStatusText(statusMsg); 183 setDisplayName(f.getName()); 184 } 185 186 protected void componentClosed() { 187 super.componentClosed(); 188 tcCount--; 189 } 190 191 } 192 | Popular Tags |