KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > enode > ScaledIcon


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Nokia. Portions Copyright 2003-2004 Nokia.
17  * All Rights Reserved.
18  */

19
20 package org.netbeans.modules.enode;
21
22 import org.netbeans.spi.enode.NbIcon;
23 import java.awt.*;
24 import java.awt.image.BufferedImage JavaDoc;
25
26 import javax.swing.*;
27
28
29 /**
30  * Scales an icon to a given width or height. By default icons are scaled to
31  * a width of 20 pixels. Such icons are needed for menu items.
32  * <p>
33  * The aspect ratio of the icon is kept if the height is less than the width
34  * (or if the width is less than the height, depending on which property shall
35  * be kept). Otherwise the height is adjusted to the width (or the other way
36  * round).
37  *
38  * @author John Stuwe
39  */

40 public class ScaledIcon extends ImageIcon {
41     /**
42      * Constructor for creating scaled icons. To create a <tt>ScaledIcon</tt>,
43      * use the public static methods in this class.
44      *
45      * @param image The image that shall be returned.
46      *
47      * @see #scale(Icon)
48      * @see #scale(Icon,int)
49      * @see #scale(Icon,int,boolean)
50      */

51     private ScaledIcon( Image JavaDoc image ) {
52         super( image );
53     }
54     
55     /**
56      * Scales the icon to a width of 16 pixels. The aspect ratio of the icon is
57      * kept if the resulting height would be less than 16 pixels, otherwise the
58      * height is truncated to 16 pixels.
59      * <p>
60      * @param The icon that shall be scaled.
61      * <p>
62      * @return The scaled icon. This is a new instance of <tt>ScaledIcon</tt>.
63      */

64     public static Icon create( ImageIcon icon ) {
65         return create( icon, NbIcon.SIZE_16x16, true );
66     }
67     
68     
69     /**
70      * Scales the icon to a given width. The aspect ratio of the icon is kept if
71      * the resulting height would be less than the given width, otherwise the
72      * height is truncated to the given width.
73      * <p>
74      * @param The icon that shall be scaled.
75      * <p>
76      * @param width The width of the scaled icon.
77      * <p>
78      * @return The scaled icon. This is a new instance of <tt>ScaledIcon</tt>.
79      */

80     public static Icon create( ImageIcon icon, int width ) {
81         return create( icon, width, true );
82     }
83     
84     
85     /**
86      * Scales the icon to a given width or height. The aspect ratio of the icon
87      * is kept if the resulting height (or width) would be less than the given
88      * size, otherwise the height (or width) is truncated to the given size.
89      * <p>
90      * @param The icon that shall be scaled.
91      * <p>
92      * @param size The width or height of the scaled icon, depending on the flag
93      * <tt>keepWidth</tt>.
94      * <p>
95      * @param keepWidth If <tt>true</tt>, the width is scaled to the given size,
96      * otherwise the height of the icon is scaled to the given size.
97      * <p>
98      * @return The scaled icon. This is a new instance of <tt>ScaledIcon</tt>.
99      */

100     public static Icon create( ImageIcon icon, int size, boolean keepWidth ) {
101         return create( icon, size, keepWidth, false );
102     }
103     
104     
105     /**
106      * Scales the icon to a given width or height. The aspect ratio of the icon
107      * is kept if the resulting height (or width) would be less than the given
108      * size, otherwise the height (or width) is truncated to the given size.
109      * <p>
110      * @param The icon that shall be scaled.
111      * <p>
112      * @param size The width or height of the scaled icon, depending on the flag
113      * <tt>keepWidth</tt>.
114      * <p>
115      * @param scaleWidth If <tt>true</tt>, the width is scaled to the given size,
116      * otherwise the height of the icon is scaled to the given size.
117      * <p>
118      * @param magnify If <tt>true</tt>, the icons gets magnified if it is too
119      * small. Otherwise the small icon is centered on a transparent
120      * background.
121      * <p>
122      * @return The scaled icon. This is a new instance of <tt>ScaledIcon</tt>.
123      */

124     public static Icon create( ImageIcon icon, int size, boolean scaleWidth,
125     boolean magnify ) {
126         Icon scaled = icon;
127         
128         if( icon.getImageLoadStatus( ) == MediaTracker.COMPLETE ) {
129             int width = icon.getIconWidth( );
130             int height = icon.getIconHeight( );
131             
132             Image JavaDoc image = icon.getImage( );
133             
134             if( magnify || ( width > size ) || ( height > size ) ) {
135                 image = scaleImage( image, width, height, size, scaleWidth );
136             }
137             else {
138                 BufferedImage JavaDoc buffer =
139                 new BufferedImage JavaDoc( size, size, BufferedImage.TYPE_INT_ARGB );
140                 
141                 Graphics g = buffer.getGraphics( );
142                 int x = ( size - width ) / 2;
143                 int y = ( size - height ) / 2;
144                 g.drawImage( image, x, y, null );
145                 g.dispose( );
146                 
147                 image = buffer;
148             }
149             
150             scaled = new ScaledIcon( image );
151         }
152         
153         return scaled;
154     }
155     
156     
157     /**
158      * Paints the scaled icon. To avoid a {@link NullPointerException}, the
159      * method uses a {@link MediaTracker} to check if the scaled image is
160      * available. If the image is not available nothing will be painted.
161      * <p>
162      * @param c The component that wants to paint the icon.
163      * <p>
164      * @param g The current graphics context.
165      * <p>
166      * @param x The x position for painting the icon.
167      * <p>
168      * @param y The y position for painting the icon.
169      */

170     public void paintIcon( Component c, Graphics g, int x, int y ) {
171         //
172
// Ensure that the scaled image is really available!
173
//
174
if( getImageLoadStatus( ) == MediaTracker.COMPLETE ) {
175             super.paintIcon( c, g, x, y );
176         }
177     }
178     
179     
180     /**
181      * Scales the image, so that it fits into a square with the given size as
182      * width and height.
183      *
184      * @param image The image that shall be scaled.
185      * <p>
186      * @param originalWidth The original width of the image.
187      * <p>
188      * @param originalHeight The original height of the image.
189      * <p>
190      * @param size The new width and height of the image.
191      * <p>
192      * @param scaleWidth If <tt>true</tt>, the width is scaled to the given
193      * size, otherwise the height of the icon is scaled to the given
194      * size.
195      * <p>
196      * @return The scaled image.
197      */

198     private static Image JavaDoc scaleImage( Image JavaDoc image, int originalWidth, int originalHeight, int size, boolean scaleWidth ) {
199         //
200
// Keep aspect ratio if possible. If the height exceeds the
201
// width, the icon gets distorted to avoid gaps between the
202
// menu items.
203
//
204
int height = -1;
205         int width = -1;
206         
207         if( scaleWidth ) {
208             width = size;
209             
210             if( originalHeight > originalWidth ) {
211                 height = width;
212             }
213         }
214         else {
215             height = size;
216             
217             if( originalWidth > originalHeight ) {
218                 width = height;
219             }
220         }
221         
222         return image.getScaledInstance( width, height, Image.SCALE_SMOOTH );
223     }
224 }
225
226
Popular Tags