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