1 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 ; 25 26 import javax.swing.*; 27 28 29 40 public class ScaledIcon extends ImageIcon { 41 51 private ScaledIcon( Image image ) { 52 super( image ); 53 } 54 55 64 public static Icon create( ImageIcon icon ) { 65 return create( icon, NbIcon.SIZE_16x16, true ); 66 } 67 68 69 80 public static Icon create( ImageIcon icon, int width ) { 81 return create( icon, width, true ); 82 } 83 84 85 100 public static Icon create( ImageIcon icon, int size, boolean keepWidth ) { 101 return create( icon, size, keepWidth, false ); 102 } 103 104 105 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 image = icon.getImage( ); 133 134 if( magnify || ( width > size ) || ( height > size ) ) { 135 image = scaleImage( image, width, height, size, scaleWidth ); 136 } 137 else { 138 BufferedImage buffer = 139 new BufferedImage ( 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 170 public void paintIcon( Component c, Graphics g, int x, int y ) { 171 if( getImageLoadStatus( ) == MediaTracker.COMPLETE ) { 175 super.paintIcon( c, g, x, y ); 176 } 177 } 178 179 180 198 private static Image scaleImage( Image image, int originalWidth, int originalHeight, int size, boolean scaleWidth ) { 199 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 |