Coverage details for ui.panel.ImageDrawPanel

LineHitsSource
1 /*******************************************************************************
2  * InternetCafe is a software solution that helps the management of Cybercafes
3  * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4  * Copyright (C) 2006 Guido Angelo Ingenito
5  
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *******************************************************************************/
20 package ui.panel;
21  
22 import java.awt.Color;
23 import java.awt.Dimension;
24 import java.awt.Graphics;
25 import java.awt.Graphics2D;
26 import java.awt.image.BufferedImage;
27  
28 import javax.swing.JPanel;
29  
30 @SuppressWarnings("serial") //$NON-NLS-1$
31 public class ImageDrawPanel extends JPanel {
320    private double zoom = 1.0;
33  
34     private double zoomPercentage;
35  
36     private BufferedImage image;
37  
38     /**
39      * Constructor
40      *
41      * @param image
42      * @param zoomPercentage
43      */
440    public ImageDrawPanel(BufferedImage image, double zoomPercentage) {
450        this.image = image;
460        this.zoomPercentage = zoomPercentage / 100;
470    }
48  
49     /**
50      * This method is overriden to draw the image and scale the graphics
51      * accordingly
52      */
53     public void paintComponent(Graphics grp) {
54  
550        Graphics2D g2D = (Graphics2D) grp;
56  
57         // set the background color to white
580        g2D.setColor(Color.WHITE);
59         // fill the rect
600        g2D.fillRect(0, 0, getWidth(), getHeight());
61  
62         // scale the graphics to get the zoom effect
630        g2D.scale(zoom, zoom);
64  
65         // can't draw the image if it is null
660        if (image == null)
670            return;
68  
69         // draw the image
700        g2D.drawImage(image, 0, 0, this);
710    }
72  
73     /**
74      * This method is overriden to return the preferred size which will be the
75      * width and height of the image plus the zoomed width width and height.
76      * while zooming out the zoomed width and height is negative
77      */
78     public Dimension getPreferredSize() {
790        if (image == null)
800            return super.getPreferredSize();
810        return new Dimension(
820                (int) (image.getWidth(this) + (image.getWidth(this) * (zoom - 1))),
830                (int) (image.getHeight(this) + (image.getHeight(this) * (zoom - 1))));
84     }
85  
86     /**
87      * Sets the new zoomed percentage
88      *
89      * @param zoomPercentage
90      */
91     public void setZoomPercentage(int zoomPercentage) {
920        this.zoomPercentage = ((double) zoomPercentage) / 100;
930    }
94  
95     /**
96      * This method set the image to the original size by setting the zoom factor
97      * to 1. i.e. 100%
98      */
99     public void originalSize() {
1000        zoom = 1;
1010    }
102  
103     /**
104      * This method increments the zoom factor with the zoom percentage, to
105      * create the zoom in effect
106      */
107     public void zoomIn() {
1080        zoom += zoomPercentage;
1090    }
110  
111     /**
112      * This method decrements the zoom factor with the zoom percentage, to
113      * create the zoom out effect
114      */
115     public void zoomOut() {
1160        zoom -= zoomPercentage;
117  
1180        if (zoom < zoomPercentage) {
1190            if (zoomPercentage > 1.0) {
1200                zoom = 1.0;
1210            } else {
1220                zoomIn();
123             }
124         }
1250    }
126  
127     /**
128      * This method returns the currently zoomed percentage
129      *
130      * @return The currently zoomed percentage.
131      */
132     public double getZoomedTo() {
1330        return zoom * 100;
134     }
135  
136     /**
137      * @return Returns the image.
138      */
139     public BufferedImage getImage() {
1400        return image;
141     }
142  
143     /**
144      * This method will set the current displayed image. If the panel was
145      * already in use with another image the zoom factor will be setted to 100%,
146      * the original size.
147      *
148      * @param image
149      * The image to set.
150      */
151     public void setImage(BufferedImage image) {
1520        this.image = image;
1530        originalSize();
1540    }
155  
156 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.