source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/browser/NavigateurImagePanel.java @ 6821

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

Premier commit. Actuellement, l'application gère :
-Listing des catégories
-Affichage des miniatures
-Ajout de commentaires
-gestion d'un cache pour ne pas télécharger les images plusieurs fois
-gestion d'un zoom dans le navigateur d'images
-navigateur d'images complètement refait, je viens de tester sur un grand écran, à priori, c'est beaucoup mieux
-meilleure gestion des exceptions, avec affichage de messages d'erreurs
-ajout d'un "logger" qui enregistre toutes les exceptions dans un fichier de log
-possibilité de ne pas mettre le "http://" dans l'écran de connexion
-gestion de transformations d'images dans le navigateur d'images : "flip" horizontal et vertical, rotations
-menu dans le navigateur d'images, qui permet d'effectuer toutes les actions, avec en plus, la possibilité d'imprimer une image

en cours d'implémentation : gestion des préférences de l'utilisateur. Actuellement, le fichier xml permettant d'écrire les préférences est géré,
il reste à faire un écran de gestion des préférences, avec un maximum d'options, afin que l'appli soit configurable.

File size: 7.1 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 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}
Note: See TracBrowser for help on using the repository browser.