source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/browser/BrowserImagePanel.java @ 6833

Last change on this file since 6833 was 6833, checked in by mlg, 14 years ago

Features implementation :
feature:0001829 : the user is now informed that the application is performing an action. It displays what action it is and also a progress bar for time consuming actions like sending images. (calls to jiwigo's webservice are done in threads).

File size: 6.6 KB
Line 
1package fr.mael.jiwigo.ui.browser;
2
3import java.awt.Dimension;
4import java.awt.Graphics;
5import java.awt.Graphics2D;
6import java.awt.RenderingHints;
7import java.awt.event.MouseWheelEvent;
8import java.awt.event.MouseWheelListener;
9import java.awt.geom.AffineTransform;
10import java.awt.image.BufferedImage;
11import java.awt.print.PageFormat;
12import java.awt.print.Printable;
13import java.awt.print.PrinterException;
14import java.util.Hashtable;
15
16import javax.swing.JLabel;
17import javax.swing.JPanel;
18import javax.swing.JSlider;
19import javax.swing.event.ChangeEvent;
20import javax.swing.event.ChangeListener;
21
22import 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 */
54public class BrowserImagePanel 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 BrowserImagePanel(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
93        g2.drawRenderedImage(image, at);
94
95    }
96
97    /* (non-Javadoc)
98     * @see javax.swing.JComponent#getPreferredSize()
99     */
100    public Dimension getPreferredSize() {
101        int w = (int) (scale * image.getWidth());
102        int h = (int) (scale * image.getHeight());
103        return new Dimension(w, h);
104    }
105
106    /**
107     * Récupération du slider
108     * @return le slider
109     */
110    public JSlider getSlider() {
111        int min = 1, max = 36, inc = 10;
112        final JSlider slider = new JSlider(min, max, 16);
113        slider.setMajorTickSpacing(5);
114        slider.setMinorTickSpacing(1);
115        slider.setPaintTicks(true);
116        slider.setSnapToTicks(true);
117        slider.setLabelTable(getLabelTable(min, max, inc));
118        slider.setPaintLabels(true);
119        slider.addChangeListener(new ChangeListener() {
120            public void stateChanged(ChangeEvent e) {
121                int value = slider.getValue();
122                valueSlider = value;
123                scale = (value + 4) / 20.0;
124                revalidate();
125                repaint();
126            };
127        });
128        return slider;
129    }
130
131    public void zoomIn() {
132
133        scale = (++valueSlider + 4) / 20.0;
134        revalidate();
135        repaint();
136    }
137
138    public void zoomOut() {
139        if (scale > 0) {
140            scale = (--valueSlider + 4) / 20.0;
141            revalidate();
142            repaint();
143        }
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            zoomIn();
206
207        } else {
208            zoomOut();
209        }
210
211    }
212
213    @Override
214    public int print(Graphics arg0, PageFormat arg1, int arg2) throws PrinterException {
215        if (arg2 != 0)
216            return NO_SUCH_PAGE;
217        Graphics2D g2 = (Graphics2D) arg0;
218        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
219        double x = (getWidth() - scale * image.getWidth()) / 2;
220        double y = (getHeight() - scale * image.getHeight()) / 2;
221        AffineTransform at = AffineTransform.getTranslateInstance(x, y);
222        if (flipV && !flipH) {
223            at.scale(scale, -scale);
224            at.translate(0, -getHeight() / 2);
225        } else if (flipH && !flipV) {
226            at.scale(-scale, scale);
227            at.translate(-getWidth() / 2, 0);
228        } else if (flipH && flipV) {
229            at.scale(-scale, -scale);
230            at.translate(-getWidth() / 2, -getHeight() / 2);
231        } else {
232            at.scale(scale, scale);
233        }
234
235        g2.drawRenderedImage(image, at);
236        return PAGE_EXISTS;
237    }
238
239}
Note: See TracBrowser for help on using the repository browser.