|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ImagePreview.java | 33.3% | 44.8% | 62.5% | 44.9% |
|
1 |
/*
|
|
2 |
* SimplyHTML, a word processor based on Java, HTML and CSS
|
|
3 |
* Copyright (C) 2002 Ulrich Hilger
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU General Public License
|
|
7 |
* as published by the Free Software Foundation; either version 2
|
|
8 |
* of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 |
*/
|
|
19 |
|
|
20 |
import javax.swing.*;
|
|
21 |
import java.awt.*;
|
|
22 |
|
|
23 |
/**
|
|
24 |
* An <code>ImagePreview</code> is a component to preview
|
|
25 |
* GIF and JPEG images.
|
|
26 |
*
|
|
27 |
* <p>The preview adapts (shrinks) images to its size upon their
|
|
28 |
* assignment. When the preview is resized, its image is adapted again.</p>
|
|
29 |
*
|
|
30 |
* <p>Alternately, when altering the image width, height and scale properties
|
|
31 |
* with respective setters, the image is resized within the preview without
|
|
32 |
* the preview itself adapting in size.</p>
|
|
33 |
*
|
|
34 |
* <p>Scroll bars are displayed as appropriate.</p>
|
|
35 |
*
|
|
36 |
* @author Ulrich Hilger
|
|
37 |
* @author Light Development
|
|
38 |
* @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
|
|
39 |
* @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
|
|
40 |
* @author published under the terms and conditions of the
|
|
41 |
* GNU General Public License,
|
|
42 |
* for details see file gpl.txt in the distribution
|
|
43 |
* package of this software
|
|
44 |
*
|
|
45 |
* @version stage 11, April 27, 2003
|
|
46 |
*/
|
|
47 |
public class ImagePreview extends JComponent implements Scrollable { |
|
48 |
|
|
49 |
/**
|
|
50 |
* scroll increment (for Scrollable implementation)
|
|
51 |
*/
|
|
52 |
private int maxUnitIncrement = 1; |
|
53 |
|
|
54 |
/**
|
|
55 |
* the image to be previewed
|
|
56 |
*/
|
|
57 |
private ImageIcon pic;
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Construct an <CODE>ImagePreview</CODE>.
|
|
61 |
*
|
|
62 |
* @param pic - the image to be previewed
|
|
63 |
*/
|
|
64 | 2 |
public ImagePreview(ImageIcon pic) {
|
65 | 2 |
setImage(pic); |
66 |
} |
|
67 |
|
|
68 |
/**
|
|
69 |
* Construct an <CODE>ImagePreview</CODE> without an image
|
|
70 |
* associated.
|
|
71 |
*/
|
|
72 | 2 |
public ImagePreview() {
|
73 | 2 |
this(null); |
74 |
} |
|
75 |
|
|
76 |
/**
|
|
77 |
* Get the original width of the image previewed in this component
|
|
78 |
*
|
|
79 |
* @return the original width of the image previewed in this component
|
|
80 |
* or -1 if no image is assigned
|
|
81 |
*/
|
|
82 | 4 |
public int getOriginalWidth() { |
83 | 4 |
if(pic != null) { |
84 | 4 |
return pic.getIconWidth();
|
85 |
} |
|
86 |
else {
|
|
87 | 0 |
return -1;
|
88 |
} |
|
89 |
} |
|
90 |
|
|
91 |
/**
|
|
92 |
* Get the original height of the image previewed in this component
|
|
93 |
*
|
|
94 |
* @return the original height of the image previewed in this component
|
|
95 |
* or -1 if no image is assigned
|
|
96 |
*/
|
|
97 | 4 |
public int getOriginalHeight() { |
98 | 4 |
if(pic != null) { |
99 | 4 |
return pic.getIconHeight();
|
100 |
} |
|
101 |
else {
|
|
102 | 0 |
return -1;
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
/**
|
|
107 |
* Set the image to be previewed.
|
|
108 |
*/
|
|
109 | 4 |
public void setImage(ImageIcon pic) { |
110 | 4 |
this.pic = pic;
|
111 | 4 |
if(pic != null) { |
112 | 2 |
this.getGraphics().clearRect(0, 0, getWidth(), getHeight());
|
113 | 2 |
this.paint(this.getGraphics()); |
114 |
} |
|
115 |
} |
|
116 |
|
|
117 |
/**
|
|
118 |
* Paints this component.
|
|
119 |
* If the image associated with this component is smaller than the size
|
|
120 |
* of the component, the image is painted in its original size. Otherwise,
|
|
121 |
* the image is scaled down to the size of this component.
|
|
122 |
*
|
|
123 |
* @param g - The graphics context to use for painting.
|
|
124 |
*/
|
|
125 | 4 |
public void paint(Graphics g) { |
126 | 4 |
if(pic != null) { |
127 | 2 |
int dWidth = pic.getIconWidth();
|
128 | 2 |
int dHeight = pic.getIconHeight();
|
129 | 2 |
int scale = getScale();
|
130 | 2 |
dWidth = dWidth * scale / 100; |
131 | 2 |
dHeight = dHeight * scale/ 100; |
132 | 2 |
g.drawImage( pic.getImage(), |
133 |
0, |
|
134 |
0, |
|
135 |
dWidth, |
|
136 |
dHeight, |
|
137 |
this);
|
|
138 |
} |
|
139 |
} |
|
140 |
|
|
141 |
/**
|
|
142 |
* Gets the size adjustment necessary for the image to fit into this
|
|
143 |
* component and returns the resulting scale percentage.
|
|
144 |
*
|
|
145 |
* @return the scale percentage of the image
|
|
146 |
*/
|
|
147 | 4 |
public int getScale() { |
148 | 4 |
int scale = 100;
|
149 | 4 |
if(pic != null) { |
150 | 4 |
int vPct = 100;
|
151 | 4 |
int hPct = 100;
|
152 | 4 |
Dimension ps = getPreferredSize(); |
153 | 4 |
hPct = (int) ((double) ps.getWidth() / |
154 |
((double) pic.getIconWidth() / (double) 100)); |
|
155 |
//System.out.println("ImagePreview getScale ps.getWidth " + ps.getWidth());
|
|
156 |
//System.out.println("ImagePreview getScale pic.getIconWidth() " + pic.getIconWidth());
|
|
157 |
//System.out.println("ImagePreview getScale hPct " + hPct + "\r\n\r\n");
|
|
158 | 4 |
vPct = (int) ((double) ps.getHeight() / |
159 |
((double) pic.getIconHeight() / (double) 100)); |
|
160 |
//System.out.println("ImagePreview getScale ps.getHeight() " + ps.getHeight());
|
|
161 |
//System.out.println("ImagePreview getScale pic.getIconHeight() " + pic.getIconHeight());
|
|
162 |
//System.out.println("ImagePreview getScale vPct " + vPct + "\r\n\r\n");
|
|
163 | 4 |
if(hPct < vPct) {
|
164 | 0 |
scale = hPct; |
165 |
} |
|
166 |
else {
|
|
167 | 4 |
scale = vPct; |
168 |
} |
|
169 |
} |
|
170 |
//System.out.println("ImagePreview getScale=" + scale + "\r\n\r\n");
|
|
171 | 4 |
return scale;
|
172 |
} |
|
173 |
|
|
174 |
/**
|
|
175 |
* set the preview to a new width maintaining the image proportions
|
|
176 |
*
|
|
177 |
* @param newWidth the new width for the image preview
|
|
178 |
*/
|
|
179 | 0 |
public void setPreviewWidth(int newWidth) { |
180 |
//System.out.println("ImagePreview setPreviewWidth newWidth=" + newWidth);
|
|
181 | 0 |
if(pic != null) { |
182 | 0 |
try {
|
183 | 0 |
int hPct = (int) ((double) newWidth / (double) ((double) getOriginalWidth() / (double) 100)); |
184 | 0 |
int newHeight = getOriginalHeight() * hPct / 100;
|
185 | 0 |
setPreferredSize(new Dimension(newWidth, newHeight));
|
186 |
} |
|
187 |
catch(Exception e) {
|
|
188 | 0 |
e.printStackTrace(); |
189 | 0 |
setPreferredSize(new Dimension(20, 20));
|
190 |
} |
|
191 | 0 |
revalidate(); |
192 |
} |
|
193 |
} |
|
194 |
|
|
195 |
/**
|
|
196 |
* set the preview to a new height maintaining the image proportions
|
|
197 |
*
|
|
198 |
* @param newHeight the new height for the image preview
|
|
199 |
*/
|
|
200 | 0 |
public void setPreviewHeight(int newHeight) { |
201 | 0 |
if( pic != null) { |
202 | 0 |
try {
|
203 | 0 |
int vPct = (int) ((double) newHeight / ((double) getOriginalHeight() / (double) 100)); |
204 | 0 |
int newWidth = getOriginalWidth() * vPct / 100;
|
205 | 0 |
setPreferredSize(new Dimension(newWidth, newHeight));
|
206 |
} |
|
207 |
catch(Exception e) {
|
|
208 | 0 |
e.printStackTrace(); |
209 | 0 |
setPreferredSize(new Dimension(20, 20));
|
210 |
} |
|
211 | 0 |
revalidate(); |
212 |
} |
|
213 |
} |
|
214 |
|
|
215 |
/**
|
|
216 |
* Adapt the size of the image previewed by this component to a new
|
|
217 |
* scale.
|
|
218 |
*
|
|
219 |
* @param newScale the new scale the image shall adapt to in size
|
|
220 |
*/
|
|
221 | 0 |
public void setScale(int newScale) { |
222 | 0 |
int newWidth;
|
223 | 0 |
int newHeight;
|
224 | 0 |
newWidth = getOriginalWidth() * newScale / 100 ; |
225 | 0 |
newHeight = getOriginalHeight() * newScale / 100; |
226 | 0 |
setPreferredSize(new Dimension(newWidth, newHeight));
|
227 | 0 |
revalidate(); |
228 |
} |
|
229 |
|
|
230 |
/*
|
|
231 |
------------ Scrollable implementation start ----------------------
|
|
232 |
*/
|
|
233 | 4 |
public Dimension getPreferredScrollableViewportSize() {
|
234 | 4 |
return getPreferredSize();
|
235 |
} |
|
236 |
|
|
237 | 0 |
public int getScrollableUnitIncrement(Rectangle visibleRect, |
238 |
int orientation,
|
|
239 |
int direction) {
|
|
240 |
//Get the current position.
|
|
241 | 0 |
int currentPosition = 0;
|
242 | 0 |
if (orientation == SwingConstants.HORIZONTAL)
|
243 | 0 |
currentPosition = visibleRect.x; |
244 |
else
|
|
245 | 0 |
currentPosition = visibleRect.y; |
246 |
|
|
247 |
//Return the number of pixels between currentPosition
|
|
248 |
//and the nearest tick mark in the indicated direction.
|
|
249 | 0 |
if (direction < 0) {
|
250 | 0 |
int newPosition = currentPosition -
|
251 |
(currentPosition / maxUnitIncrement) * |
|
252 |
maxUnitIncrement; |
|
253 | 0 |
return (newPosition == 0) ? maxUnitIncrement : newPosition;
|
254 |
} else {
|
|
255 | 0 |
return ((currentPosition / maxUnitIncrement) + 1) *
|
256 |
maxUnitIncrement - currentPosition; |
|
257 |
} |
|
258 |
} |
|
259 |
|
|
260 | 0 |
public int getScrollableBlockIncrement(Rectangle visibleRect, |
261 |
int orientation,
|
|
262 |
int direction) {
|
|
263 | 0 |
if (orientation == SwingConstants.HORIZONTAL)
|
264 | 0 |
return visibleRect.width - maxUnitIncrement;
|
265 |
else
|
|
266 | 0 |
return visibleRect.height - maxUnitIncrement;
|
267 |
} |
|
268 |
|
|
269 | 22 |
public boolean getScrollableTracksViewportWidth() { |
270 | 22 |
return false; |
271 |
} |
|
272 |
|
|
273 | 22 |
public boolean getScrollableTracksViewportHeight() { |
274 | 22 |
return false; |
275 |
} |
|
276 |
|
|
277 | 0 |
public void setMaxUnitIncrement(int pixels) { |
278 | 0 |
maxUnitIncrement = pixels; |
279 |
} |
|
280 |
/*
|
|
281 |
--------- Scrollable implementation end ---------------------------
|
|
282 |
*/
|
|
283 |
} |
|