View Javadoc

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 {
32  	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  	 */
44  	public ImageDrawPanel(BufferedImage image, double zoomPercentage) {
45  		this.image = image;
46  		this.zoomPercentage = zoomPercentage / 100;
47  	}
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  
55  		Graphics2D g2D = (Graphics2D) grp;
56  
57  		// set the background color to white
58  		g2D.setColor(Color.WHITE);
59  		// fill the rect
60  		g2D.fillRect(0, 0, getWidth(), getHeight());
61  
62  		// scale the graphics to get the zoom effect
63  		g2D.scale(zoom, zoom);
64  
65  		// can't draw the image if it is null
66  		if (image == null)
67  			return;
68  
69  		// draw the image
70  		g2D.drawImage(image, 0, 0, this);
71  	}
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() {
79  		if (image == null)
80  			return super.getPreferredSize();
81  		return new Dimension(
82  				(int) (image.getWidth(this) + (image.getWidth(this) * (zoom - 1))),
83  				(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) {
92  		this.zoomPercentage = ((double) zoomPercentage) / 100;
93  	}
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() {
100 		zoom = 1;
101 	}
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() {
108 		zoom += zoomPercentage;
109 	}
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() {
116 		zoom -= zoomPercentage;
117 
118 		if (zoom < zoomPercentage) {
119 			if (zoomPercentage > 1.0) {
120 				zoom = 1.0;
121 			} else {
122 				zoomIn();
123 			}
124 		}
125 	}
126 
127 	/***
128 	 * This method returns the currently zoomed percentage
129 	 * 
130 	 * @return The currently zoomed percentage.
131 	 */
132 	public double getZoomedTo() {
133 		return zoom * 100;
134 	}
135 
136 	/***
137 	 * @return Returns the image.
138 	 */
139 	public BufferedImage getImage() {
140 		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) {
152 		this.image = image;
153 		originalSize();
154 	}
155 
156 }