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.MouseWheelEvent; |
---|
8 | import java.awt.event.MouseWheelListener; |
---|
9 | import java.awt.geom.AffineTransform; |
---|
10 | import java.awt.image.BufferedImage; |
---|
11 | import java.awt.print.PageFormat; |
---|
12 | import java.awt.print.Printable; |
---|
13 | import java.awt.print.PrinterException; |
---|
14 | import java.util.Hashtable; |
---|
15 | |
---|
16 | import javax.swing.JLabel; |
---|
17 | import javax.swing.JPanel; |
---|
18 | import javax.swing.JSlider; |
---|
19 | import javax.swing.event.ChangeEvent; |
---|
20 | import javax.swing.event.ChangeListener; |
---|
21 | |
---|
22 | import fr.mael.jiwigo.transverse.util.ImagesUtil; |
---|
23 | |
---|
24 | /** |
---|
25 | Copyright (c) 2010, Mael |
---|
26 | All rights reserved. |
---|
27 | |
---|
28 | Redistribution and use in source and binary forms, with or without |
---|
29 | modification, are permitted provided that the following conditions are met: |
---|
30 | * Redistributions of source code must retain the above copyright |
---|
31 | notice, this list of conditions and the following disclaimer. |
---|
32 | * Redistributions in binary form must reproduce the above copyright |
---|
33 | notice, this list of conditions and the following disclaimer in the |
---|
34 | documentation and/or other materials provided with the distribution. |
---|
35 | * Neither the name of jiwigo nor the |
---|
36 | names of its contributors may be used to endorse or promote products |
---|
37 | derived from this software without specific prior written permission. |
---|
38 | |
---|
39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
40 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
41 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
42 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
43 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
44 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
45 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
46 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
47 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
48 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
49 | |
---|
50 | * Panel qui affiche une image |
---|
51 | * @author mael |
---|
52 | * |
---|
53 | */ |
---|
54 | public class NavigateurImagePanel extends JPanel implements MouseWheelListener, Printable { |
---|
55 | private BufferedImage image; |
---|
56 | private double scale = 1.0; |
---|
57 | boolean flipH = false; |
---|
58 | boolean flipV = false; |
---|
59 | private int valueSlider = 16; |
---|
60 | |
---|
61 | /** |
---|
62 | * Constructeur |
---|
63 | * @param image |
---|
64 | */ |
---|
65 | public NavigateurImagePanel(BufferedImage image) { |
---|
66 | this.image = image; |
---|
67 | this.addMouseWheelListener(this); |
---|
68 | } |
---|
69 | |
---|
70 | /* (non-Javadoc) |
---|
71 | * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) |
---|
72 | */ |
---|
73 | protected void paintComponent(Graphics g) { |
---|
74 | super.paintComponent(g); |
---|
75 | Graphics2D g2 = (Graphics2D) g; |
---|
76 | g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); |
---|
77 | double x = (getWidth() - scale * image.getWidth()) / 2; |
---|
78 | double y = (getHeight() - scale * image.getHeight()) / 2; |
---|
79 | AffineTransform at = AffineTransform.getTranslateInstance(x, y); |
---|
80 | if (flipV && !flipH) { |
---|
81 | at.scale(scale, -scale); |
---|
82 | at.translate(0, -getHeight() / 2); |
---|
83 | } else if (flipH && !flipV) { |
---|
84 | at.scale(-scale, scale); |
---|
85 | at.translate(-getWidth() / 2, 0); |
---|
86 | } else if (flipH && flipV) { |
---|
87 | at.scale(-scale, -scale); |
---|
88 | at.translate(-getWidth() / 2, -getHeight() / 2); |
---|
89 | } else { |
---|
90 | at.scale(scale, scale); |
---|
91 | } |
---|
92 | System.out.println(scale); |
---|
93 | |
---|
94 | g2.drawRenderedImage(image, at); |
---|
95 | |
---|
96 | } |
---|
97 | |
---|
98 | /* (non-Javadoc) |
---|
99 | * @see javax.swing.JComponent#getPreferredSize() |
---|
100 | */ |
---|
101 | public Dimension getPreferredSize() { |
---|
102 | int w = (int) (scale * image.getWidth()); |
---|
103 | int h = (int) (scale * image.getHeight()); |
---|
104 | return new Dimension(w, h); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Récupération du slider |
---|
109 | * @return le slider |
---|
110 | */ |
---|
111 | public JSlider getSlider() { |
---|
112 | int min = 1, max = 36, inc = 10; |
---|
113 | final JSlider slider = new JSlider(min, max, 16); |
---|
114 | slider.setMajorTickSpacing(5); |
---|
115 | slider.setMinorTickSpacing(1); |
---|
116 | slider.setPaintTicks(true); |
---|
117 | slider.setSnapToTicks(true); |
---|
118 | slider.setLabelTable(getLabelTable(min, max, inc)); |
---|
119 | slider.setPaintLabels(true); |
---|
120 | slider.addChangeListener(new ChangeListener() { |
---|
121 | public void stateChanged(ChangeEvent e) { |
---|
122 | int value = slider.getValue(); |
---|
123 | valueSlider = value; |
---|
124 | scale = (value + 4) / 20.0; |
---|
125 | revalidate(); |
---|
126 | repaint(); |
---|
127 | }; |
---|
128 | }); |
---|
129 | return slider; |
---|
130 | } |
---|
131 | |
---|
132 | public void zoomIn() { |
---|
133 | |
---|
134 | scale = (++valueSlider + 4) / 20.0; |
---|
135 | revalidate(); |
---|
136 | repaint(); |
---|
137 | } |
---|
138 | |
---|
139 | public void zoomOut() { |
---|
140 | |
---|
141 | scale = (--valueSlider + 4) / 20.0; |
---|
142 | revalidate(); |
---|
143 | repaint(); |
---|
144 | } |
---|
145 | |
---|
146 | public void zoomNormal() { |
---|
147 | |
---|
148 | scale = 1.0; |
---|
149 | revalidate(); |
---|
150 | repaint(); |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Label du slider |
---|
155 | * @param min |
---|
156 | * @param max |
---|
157 | * @param inc |
---|
158 | * @return |
---|
159 | */ |
---|
160 | private Hashtable getLabelTable(int min, int max, int inc) { |
---|
161 | Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>(); |
---|
162 | for (int j = min; j <= max; j += inc) { |
---|
163 | String s = String.format("%.2f", (j + 4) / 20.0); |
---|
164 | table.put(Integer.valueOf(j), new JLabel(s)); |
---|
165 | } |
---|
166 | return table; |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * Rotation vers la droite |
---|
171 | */ |
---|
172 | public void rotationDroite() { |
---|
173 | setImage(ImagesUtil.rotate(image, Math.PI / 2)); |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * Rotation vers la gauche |
---|
178 | */ |
---|
179 | public void rotationGauche() { |
---|
180 | setImage(ImagesUtil.rotate(image, -Math.PI / 2)); |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * @return the image |
---|
185 | */ |
---|
186 | public BufferedImage getImage() { |
---|
187 | return image; |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * @param image the image to set |
---|
192 | */ |
---|
193 | public void setImage(BufferedImage image) { |
---|
194 | this.image = image; |
---|
195 | revalidate(); |
---|
196 | repaint(); |
---|
197 | } |
---|
198 | |
---|
199 | @Override |
---|
200 | public void mouseWheelMoved(MouseWheelEvent arg0) { |
---|
201 | int notches = arg0.getWheelRotation(); |
---|
202 | String message; |
---|
203 | String newline = "\n"; |
---|
204 | if (notches < 0) { |
---|
205 | message = "Mouse wheel moved UP " + -notches + " notch(es)" + newline; |
---|
206 | } else { |
---|
207 | message = "Mouse wheel moved DOWN " + notches + " notch(es)" + newline; |
---|
208 | } |
---|
209 | if (arg0.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) { |
---|
210 | message += " Scroll amount: " + arg0.getScrollAmount() + " unit increments per notch" + newline; |
---|
211 | message += " Units to scroll: " + arg0.getUnitsToScroll() + " unit increments" + newline; |
---|
212 | } else { //scroll type == MouseWheelEvent.WHEEL_BLOCK_SCROLL |
---|
213 | message += " Scroll type: WHEEL_BLOCK_SCROLL" + newline; |
---|
214 | } |
---|
215 | |
---|
216 | } |
---|
217 | |
---|
218 | @Override |
---|
219 | public int print(Graphics arg0, PageFormat arg1, int arg2) throws PrinterException { |
---|
220 | if (arg2 != 0) |
---|
221 | return NO_SUCH_PAGE; |
---|
222 | Graphics2D g2 = (Graphics2D) arg0; |
---|
223 | g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); |
---|
224 | double x = (getWidth() - scale * image.getWidth()) / 2; |
---|
225 | double y = (getHeight() - scale * image.getHeight()) / 2; |
---|
226 | AffineTransform at = AffineTransform.getTranslateInstance(x, y); |
---|
227 | if (flipV && !flipH) { |
---|
228 | at.scale(scale, -scale); |
---|
229 | at.translate(0, -getHeight() / 2); |
---|
230 | } else if (flipH && !flipV) { |
---|
231 | at.scale(-scale, scale); |
---|
232 | at.translate(-getWidth() / 2, 0); |
---|
233 | } else if (flipH && flipV) { |
---|
234 | at.scale(-scale, -scale); |
---|
235 | at.translate(-getWidth() / 2, -getHeight() / 2); |
---|
236 | } else { |
---|
237 | at.scale(scale, scale); |
---|
238 | } |
---|
239 | |
---|
240 | g2.drawRenderedImage(image, at); |
---|
241 | return PAGE_EXISTS; |
---|
242 | } |
---|
243 | |
---|
244 | } |
---|