KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > awt > ImageCanvas


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
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 of
9  * the License, or (at your option) any later version.
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
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sshtools.ui.awt;
21
22 import java.awt.Canvas JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.Image JavaDoc;
27 import java.awt.MediaTracker JavaDoc;
28
29 /**
30  * Simple component to draw an image. The image can be either centered in the
31  * container or
32  *
33  * @author $author$
34  */

35
36 public class ImageCanvas extends Canvas JavaDoc {
37
38     /**
39      * Centered in container
40      */

41     public final static int CENTERED = 0;
42
43     /**
44      * Resize to take up all space allocated to component
45      */

46     public final static int STRETCH = 1;
47
48     // Private instance variables
49
private MediaTracker JavaDoc tracker;
50     private Image JavaDoc image;
51     private int scale;
52     private int border;
53     private Color JavaDoc borderColor;
54     private float valign = CENTER_ALIGNMENT;
55     private boolean paintBackground;
56     private float halign = CENTER_ALIGNMENT;
57     private boolean doubleBuffered;
58     private int bufferWidth;
59     private int bufferHeight;
60     private Image JavaDoc bufferImage;
61     private Graphics JavaDoc bufferGraphics;
62
63     /**
64      * <p>
65      * Construct a new image canvas
66      * </p>
67      *
68      */

69     public ImageCanvas() {
70         super();
71     }
72
73     /**
74      * <p>
75      * Construct a new image canvas
76      * </p>
77      *
78      * @param iamge image
79      *
80      */

81     public ImageCanvas(Image JavaDoc image) {
82         super();
83         setImage(image);
84         repaint();
85     }
86
87     /**
88      * <p>
89      * Construct a new image canvas given a <code>Class</code> from which the
90      * <code>Classloader</code> can be determined, and the resource name. The
91      * image will be centered in the container.
92      * </p>
93      *
94      * @param cls name of image
95      * @param scale scale
96      *
97      */

98     public ImageCanvas(Class JavaDoc cls, String JavaDoc image) {
99         super();
100         setImage(UIUtil.loadImage(cls, image));
101     }
102
103     /**
104      * Set the vertical alignment
105      *
106      * @param f vertical alignment
107      */

108     public void setValign(float valign) {
109         this.valign = valign;
110     }
111
112     /**
113      * Set the horizontal alignment
114      *
115      * @param f horizontal alignment
116      */

117     public void setHalign(float halign) {
118         this.halign = halign;
119     }
120
121     /**
122      * Set the border width around the image
123      *
124      * @param border border
125      */

126     public void setBorder(int border) {
127         this.border = border;
128     }
129
130     /**
131      * Get the border width around the image
132      *
133      * @return border
134      */

135     public int getBorder() {
136         return border;
137     }
138
139     /**
140      * Get the border color
141      *
142      * @return border color
143      */

144     public Color JavaDoc getBorderColor() {
145         return borderColor;
146     }
147
148     /**
149      * Set the border color
150      *
151      * @param borderColor border color
152      */

153     public void setBorderColor(Color JavaDoc borderColor) {
154         this.borderColor = borderColor;
155     }
156
157     /**
158      * <p>
159      * Set the scale. Can be one of
160      * </p>
161      *
162      * <ul>
163      * <li><code>ImageCanvas.STRETCH</code></li>
164      * <li><code>ImageCanvas.CENTERED</code></li>
165      * </ul>
166      *
167      * @param scale scale
168      */

169     public void setScale(int scale) {
170         this.scale = scale;
171         repaint();
172     }
173
174     /**
175      * <p>
176      * Get the scale. Can be one of
177      * </p>
178      *
179      * <ul>
180      * <li><code>ImageCanvas.STRETCH</code></li>
181      * <li><code>ImageCanvas.CENTERED</code></li>
182      * </ul>
183      *
184      * @return scale
185      */

186     public int getScale() {
187         return scale;
188     }
189
190     /**
191      * Set the image to display
192      *
193      * @param image
194      */

195     public void setImage(Image JavaDoc image) {
196         // Prompt a layout if the image size changes
197
if ((image == null && this.image != null) || (image != null && this.image == null)
198             || (image != null && this.image != null && (image.getWidth(this) != this.image.getWidth(this) || image.getHeight(this) != this.image.getHeight(this)))) {
199             this.image = image;
200             doLayout();
201         } else {
202             this.image = image;
203         }
204         paintBackground = true;
205         repaint();
206     }
207
208     /**
209      * Set the image to display
210      *
211      * @return image
212      */

213     public Image JavaDoc getImage() {
214         return image;
215     }
216
217     /*
218      * Prevent flicker
219      *
220      * @see java.awt.Component#update(java.awt.Graphics)
221      */

222     public void update(Graphics JavaDoc g) {
223         paint(g);
224     }
225
226     public boolean isDoubleBuffered() {
227         return doubleBuffered;
228     }
229
230     public void setDoubleBuffered(boolean doubleBuffered) {
231         this.doubleBuffered = doubleBuffered;
232     }
233
234     public void paint(Graphics JavaDoc g) {
235         if (!doubleBuffered) {
236             paintBuffer(g);
237         } else {
238             // checks the buffersize with the current panelsize
239
// or initialises the image with the first paint
240
if (bufferWidth != getSize().width || bufferHeight != getSize().height || bufferImage == null || bufferGraphics == null)
241                 resetBuffer();
242
243             if (bufferGraphics != null) {
244                 // this clears the offscreen image, not the onscreen one
245
bufferGraphics.clearRect(0, 0, bufferWidth, bufferHeight);
246
247                 // calls the paintbuffer method with
248
// the offscreen graphics as a param
249
paintBuffer(bufferGraphics);
250
251                 // we finaly paint the offscreen image onto the onscreen image
252
g.drawImage(bufferImage, 0, 0, this);
253             }
254         }
255     }
256
257     private void paintBuffer(Graphics JavaDoc g) {
258
259         Dimension JavaDoc d = getSize();
260         if (paintBackground) {
261             g.setColor(getBackground());
262             g.fillRect(0, 0, d.width, d.height);
263         }
264         Dimension JavaDoc f = new Dimension JavaDoc(d.width - (border * 2), d.height - (border * 2));
265         if (image != null) {
266             if (scale == STRETCH) {
267                 g.drawImage(image, border, border, f.width, f.height, this);
268             } else {
269                 int x = Math.max((f.width - image.getWidth(this)) / 2, 0);
270                 if (halign == Canvas.LEFT_ALIGNMENT) {
271                     x = 0;
272                 } else if (halign == Canvas.RIGHT_ALIGNMENT) {
273                     x = f.width - image.getWidth(this);
274                 }
275                 int y = Math.max((f.height - image.getHeight(this)) / 2, 0);
276                 if (valign == Canvas.TOP_ALIGNMENT) {
277                     y = 0;
278                 } else if (valign == Canvas.BOTTOM_ALIGNMENT) {
279                     y = f.height - image.getHeight(this);
280                 }
281                 g.drawImage(image, x + border, y + border, this);
282             }
283         }
284
285     }
286
287     private void resetBuffer() {
288         // always keep track of the image size
289
bufferWidth = getSize().width;
290         bufferHeight = getSize().height;
291
292         // clean up the previous image
293
if (bufferGraphics != null) {
294             bufferGraphics.dispose();
295             bufferGraphics = null;
296         }
297         if (bufferImage != null) {
298             bufferImage.flush();
299             bufferImage = null;
300         }
301         System.gc();
302
303         // create the new image with the size of the panel
304
bufferImage = createImage(bufferWidth, bufferHeight);
305         bufferGraphics = bufferImage.getGraphics();
306     }
307
308     public Dimension JavaDoc getPreferredSize() {
309         if (image == null) {
310             return new Dimension JavaDoc(border * 2, border * 2);
311         } else {
312             return new Dimension JavaDoc(image.getWidth(this) + (border * 2), image.getHeight(this) + (border * 2));
313         }
314     }
315
316     public Dimension JavaDoc getMinimumSize() {
317         return getPreferredSize();
318     }
319 }
320
Popular Tags