1 | package fr.mael.jiwigo.ui.browser; |
---|
2 | |
---|
3 | import java.awt.Dimension; |
---|
4 | import java.awt.Graphics; |
---|
5 | import java.awt.Graphics2D; |
---|
6 | import java.awt.RenderingHints; |
---|
7 | import java.awt.event.MouseEvent; |
---|
8 | import java.awt.event.MouseListener; |
---|
9 | import java.awt.event.MouseMotionListener; |
---|
10 | import java.awt.event.MouseWheelEvent; |
---|
11 | import java.awt.event.MouseWheelListener; |
---|
12 | import java.awt.geom.AffineTransform; |
---|
13 | import java.awt.image.BufferedImage; |
---|
14 | import java.awt.print.PageFormat; |
---|
15 | import java.awt.print.Printable; |
---|
16 | import java.awt.print.PrinterException; |
---|
17 | import java.util.Hashtable; |
---|
18 | |
---|
19 | import javax.swing.JLabel; |
---|
20 | import javax.swing.JOptionPane; |
---|
21 | import javax.swing.JPanel; |
---|
22 | import javax.swing.JSlider; |
---|
23 | import javax.swing.event.ChangeEvent; |
---|
24 | import javax.swing.event.ChangeListener; |
---|
25 | |
---|
26 | import fr.mael.jiwigo.transverse.ImagesManagement; |
---|
27 | import fr.mael.jiwigo.transverse.util.ImagesUtil; |
---|
28 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
29 | |
---|
30 | /** |
---|
31 | Copyright (c) 2010, Mael |
---|
32 | All rights reserved. |
---|
33 | |
---|
34 | Redistribution and use in source and binary forms, with or without |
---|
35 | modification, are permitted provided that the following conditions are met: |
---|
36 | * Redistributions of source code must retain the above copyright |
---|
37 | notice, this list of conditions and the following disclaimer. |
---|
38 | * Redistributions in binary form must reproduce the above copyright |
---|
39 | notice, this list of conditions and the following disclaimer in the |
---|
40 | documentation and/or other materials provided with the distribution. |
---|
41 | * Neither the name of jiwigo nor the |
---|
42 | names of its contributors may be used to endorse or promote products |
---|
43 | derived from this software without specific prior written permission. |
---|
44 | |
---|
45 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
46 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
47 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
48 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
49 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
50 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
51 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
52 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
53 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
54 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
55 | |
---|
56 | * Panel qui affiche une image |
---|
57 | * @author mael |
---|
58 | * |
---|
59 | */ |
---|
60 | public class BrowserImagePanel extends JPanel implements MouseWheelListener, Printable, MouseMotionListener, |
---|
61 | MouseListener { |
---|
62 | private BufferedImage image; |
---|
63 | private double scale = 1.0; |
---|
64 | boolean flipH = false; |
---|
65 | boolean flipV = false; |
---|
66 | private int valueSlider = 16; |
---|
67 | private boolean clicked = false; |
---|
68 | private int xTopPosition; |
---|
69 | private int yTopPosition; |
---|
70 | private int xDragPosition; |
---|
71 | private int yDragPosition; |
---|
72 | private boolean drawSelection = false; |
---|
73 | private ImagesManagement imagesManagement = ImagesManagement.getInstance(); |
---|
74 | |
---|
75 | /** |
---|
76 | * Constructor |
---|
77 | * @param image the image |
---|
78 | */ |
---|
79 | public BrowserImagePanel() { |
---|
80 | this.image = imagesManagement.getCurrentBufferedImage(); |
---|
81 | this.addMouseWheelListener(this); |
---|
82 | this.addMouseMotionListener(this); |
---|
83 | this.addMouseListener(this); |
---|
84 | } |
---|
85 | |
---|
86 | /* (non-Javadoc) |
---|
87 | * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) |
---|
88 | */ |
---|
89 | protected void paintComponent(Graphics g) { |
---|
90 | super.paintComponent(g); |
---|
91 | Graphics2D g2 = (Graphics2D) g; |
---|
92 | g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); |
---|
93 | double x = (getWidth() - scale * image.getWidth()) / 2; |
---|
94 | double y = (getHeight() - scale * image.getHeight()) / 2; |
---|
95 | AffineTransform at = AffineTransform.getTranslateInstance(x, y); |
---|
96 | if (flipV && !flipH) { |
---|
97 | at.scale(scale, -scale); |
---|
98 | at.translate(0, -getHeight() / 2); |
---|
99 | } else if (flipH && !flipV) { |
---|
100 | at.scale(-scale, scale); |
---|
101 | at.translate(-getWidth() / 2, 0); |
---|
102 | } else if (flipH && flipV) { |
---|
103 | at.scale(-scale, -scale); |
---|
104 | at.translate(-getWidth() / 2, -getHeight() / 2); |
---|
105 | } else { |
---|
106 | at.scale(scale, scale); |
---|
107 | } |
---|
108 | g2.drawRenderedImage(image, at); |
---|
109 | if (drawSelection) { |
---|
110 | // g2.drawRect(xTopPosition, yTopPosition, xDragPosition - xTopPosition, yDragPosition - yTopPosition); |
---|
111 | } |
---|
112 | |
---|
113 | } |
---|
114 | |
---|
115 | /* (non-Javadoc) |
---|
116 | * @see javax.swing.JComponent#getPreferredSize() |
---|
117 | */ |
---|
118 | public Dimension getPreferredSize() { |
---|
119 | int w = (int) (scale * image.getWidth()); |
---|
120 | int h = (int) (scale * image.getHeight()); |
---|
121 | return new Dimension(w, h); |
---|
122 | } |
---|
123 | |
---|
124 | public void rogner() { |
---|
125 | if (scale == 1.0) { |
---|
126 | //gets the dimensions of the image |
---|
127 | Double width = getSize().getWidth(); |
---|
128 | Double height = getSize().getHeight(); |
---|
129 | //get the top left corner |
---|
130 | Double xZeroImage = ((width - image.getWidth()) / 2); |
---|
131 | Double yZeroImage = ((height - image.getHeight()) / 2); |
---|
132 | //the position of the first click on the imagee |
---|
133 | //The previous positions where the ones on the panel |
---|
134 | //have to calculate the position on the image |
---|
135 | int positionX, positionY; |
---|
136 | //width and height of the clip |
---|
137 | int largeur, longueur; |
---|
138 | //if the selection begins before the beginning of the image in x |
---|
139 | //0 is taken. Otherwise the position of the click is calculated : |
---|
140 | //position on the image = position on the panel - position of the x of the image |
---|
141 | if (xTopPosition - xZeroImage.intValue() < 0) { |
---|
142 | positionX = 0; |
---|
143 | } else { |
---|
144 | positionX = xTopPosition - xZeroImage.intValue(); |
---|
145 | } |
---|
146 | //if the selection begins before the beginning of the image in y |
---|
147 | //0 is taken. Otherwise the position of the click is calculated : |
---|
148 | //position on the image = position on the panel - position of the y of the image |
---|
149 | if (yTopPosition - yZeroImage.intValue() < 0) { |
---|
150 | positionY = 0; |
---|
151 | } else { |
---|
152 | positionY = yTopPosition - yZeroImage.intValue(); |
---|
153 | } |
---|
154 | //calculates the width |
---|
155 | largeur = xDragPosition - xTopPosition; |
---|
156 | //if it goes past the image |
---|
157 | if ((positionX + largeur) > image.getWidth()) { |
---|
158 | //the width is recalculated |
---|
159 | largeur = image.getWidth() - positionX; |
---|
160 | } |
---|
161 | //calculate the height |
---|
162 | longueur = yDragPosition - yTopPosition; |
---|
163 | ////if it goes past the image |
---|
164 | if ((positionY + longueur) > image.getHeight()) { |
---|
165 | //the height is recalculated |
---|
166 | longueur = image.getHeight() - positionY; |
---|
167 | } |
---|
168 | //gets the cutted imagee |
---|
169 | image = image.getSubimage(positionX, positionY, largeur, longueur); |
---|
170 | repaint(); |
---|
171 | revalidate(); |
---|
172 | drawSelection = false; |
---|
173 | } else { |
---|
174 | JOptionPane.showMessageDialog(this, Messages.getMessage("clippingError"), Messages.getMessage("info"), |
---|
175 | JOptionPane.INFORMATION_MESSAGE); |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * gets the slider |
---|
181 | * @return the slider |
---|
182 | */ |
---|
183 | public JSlider getSlider() { |
---|
184 | int min = 1, max = 36, inc = 10; |
---|
185 | final JSlider slider = new JSlider(min, max, 16); |
---|
186 | slider.setMajorTickSpacing(5); |
---|
187 | slider.setMinorTickSpacing(1); |
---|
188 | slider.setPaintTicks(true); |
---|
189 | slider.setSnapToTicks(true); |
---|
190 | slider.setLabelTable(getLabelTable(min, max, inc)); |
---|
191 | slider.setPaintLabels(true); |
---|
192 | slider.addChangeListener(new ChangeListener() { |
---|
193 | public void stateChanged(ChangeEvent e) { |
---|
194 | int value = slider.getValue(); |
---|
195 | valueSlider = value; |
---|
196 | scale = (value + 4) / 20.0; |
---|
197 | revalidate(); |
---|
198 | repaint(); |
---|
199 | }; |
---|
200 | }); |
---|
201 | return slider; |
---|
202 | } |
---|
203 | |
---|
204 | public void zoomIn() { |
---|
205 | |
---|
206 | scale = (++valueSlider + 4) / 20.0; |
---|
207 | revalidate(); |
---|
208 | repaint(); |
---|
209 | } |
---|
210 | |
---|
211 | public void zoomOut() { |
---|
212 | if (scale > 0) { |
---|
213 | scale = (--valueSlider + 4) / 20.0; |
---|
214 | revalidate(); |
---|
215 | repaint(); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | public void zoomNormal() { |
---|
220 | |
---|
221 | scale = 1.0; |
---|
222 | revalidate(); |
---|
223 | repaint(); |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * Label of the slider |
---|
228 | * @param min |
---|
229 | * @param max |
---|
230 | * @param inc |
---|
231 | * @return |
---|
232 | */ |
---|
233 | private Hashtable getLabelTable(int min, int max, int inc) { |
---|
234 | Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>(); |
---|
235 | for (int j = min; j <= max; j += inc) { |
---|
236 | String s = String.format("%.2f", (j + 4) / 20.0); |
---|
237 | table.put(Integer.valueOf(j), new JLabel(s)); |
---|
238 | } |
---|
239 | return table; |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * right rotation |
---|
244 | */ |
---|
245 | public void rotationDroite() { |
---|
246 | image = (ImagesUtil.rotate(image, Math.PI / 2)); |
---|
247 | revalidate(); |
---|
248 | repaint(); |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * left rotation |
---|
253 | */ |
---|
254 | public void rotationGauche() { |
---|
255 | image = (ImagesUtil.rotate(image, -Math.PI / 2)); |
---|
256 | revalidate(); |
---|
257 | repaint(); |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * @return the image |
---|
262 | */ |
---|
263 | public BufferedImage getImage() { |
---|
264 | return image; |
---|
265 | } |
---|
266 | |
---|
267 | /** |
---|
268 | * @param image the image to set |
---|
269 | */ |
---|
270 | public void changeImage() { |
---|
271 | this.image = imagesManagement.getCurrentBufferedImage(); |
---|
272 | revalidate(); |
---|
273 | repaint(); |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * @return the drawSelection |
---|
278 | */ |
---|
279 | public boolean isDrawSelection() { |
---|
280 | return drawSelection; |
---|
281 | } |
---|
282 | |
---|
283 | /** |
---|
284 | * @param drawSelection the drawSelection to set |
---|
285 | */ |
---|
286 | public void setDrawSelection(boolean drawSelection) { |
---|
287 | this.drawSelection = drawSelection; |
---|
288 | } |
---|
289 | |
---|
290 | @Override |
---|
291 | public void mouseWheelMoved(MouseWheelEvent arg0) { |
---|
292 | int notches = arg0.getWheelRotation(); |
---|
293 | String message; |
---|
294 | String newline = "\n"; |
---|
295 | if (notches < 0) { |
---|
296 | zoomIn(); |
---|
297 | |
---|
298 | } else { |
---|
299 | zoomOut(); |
---|
300 | } |
---|
301 | |
---|
302 | } |
---|
303 | |
---|
304 | @Override |
---|
305 | public int print(Graphics arg0, PageFormat arg1, int arg2) throws PrinterException { |
---|
306 | if (arg2 != 0) |
---|
307 | return NO_SUCH_PAGE; |
---|
308 | Graphics2D g2 = (Graphics2D) arg0; |
---|
309 | g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); |
---|
310 | double x = (getWidth() - scale * image.getWidth()) / 2; |
---|
311 | double y = (getHeight() - scale * image.getHeight()) / 2; |
---|
312 | AffineTransform at = AffineTransform.getTranslateInstance(x, y); |
---|
313 | if (flipV && !flipH) { |
---|
314 | at.scale(scale, -scale); |
---|
315 | at.translate(0, -getHeight() / 2); |
---|
316 | } else if (flipH && !flipV) { |
---|
317 | at.scale(-scale, scale); |
---|
318 | at.translate(-getWidth() / 2, 0); |
---|
319 | } else if (flipH && flipV) { |
---|
320 | at.scale(-scale, -scale); |
---|
321 | at.translate(-getWidth() / 2, -getHeight() / 2); |
---|
322 | } else { |
---|
323 | at.scale(scale, scale); |
---|
324 | } |
---|
325 | |
---|
326 | g2.drawRenderedImage(image, at); |
---|
327 | return PAGE_EXISTS; |
---|
328 | } |
---|
329 | |
---|
330 | @Override |
---|
331 | public void mouseDragged(MouseEvent arg0) { |
---|
332 | if (clicked) { |
---|
333 | xDragPosition = arg0.getX(); |
---|
334 | yDragPosition = arg0.getY(); |
---|
335 | repaint(); |
---|
336 | revalidate(); |
---|
337 | } |
---|
338 | } |
---|
339 | |
---|
340 | @Override |
---|
341 | public void mouseMoved(MouseEvent arg0) { |
---|
342 | } |
---|
343 | |
---|
344 | public void mouseClicked(MouseEvent arg0) { |
---|
345 | } |
---|
346 | |
---|
347 | public void mouseEntered(MouseEvent arg0) { |
---|
348 | } |
---|
349 | |
---|
350 | public void mouseExited(MouseEvent arg0) { |
---|
351 | } |
---|
352 | |
---|
353 | public void mousePressed(MouseEvent arg0) { |
---|
354 | clicked = true; |
---|
355 | xTopPosition = arg0.getX(); |
---|
356 | yTopPosition = arg0.getY(); |
---|
357 | drawSelection = true; |
---|
358 | } |
---|
359 | |
---|
360 | public void mouseReleased(MouseEvent arg0) { |
---|
361 | clicked = false; |
---|
362 | } |
---|
363 | |
---|
364 | } |
---|