下面列出了javax.swing.ImageIcon#getIconHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns a scaled down image if the height or width is smaller than the
* image size.
*
* @param icon
* the image icon.
* @param newHeight
* the preferred height.
* @param newWidth
* the preferred width.
* @return the icon.
*/
public static ImageIcon scaleImageIcon(ImageIcon icon, int newHeight,
int newWidth) {
Image img = icon.getImage();
int height = icon.getIconHeight();
int width = icon.getIconWidth();
if (height > newHeight) {
height = newHeight;
}
if (width > newWidth) {
width = newWidth;
}
img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
return new ImageIcon(img);
}
/**
* Paints the given overlay icon over the base icon. This can be used for example to cross out
* icons. Provided the overlay icon has a transparent background, the base icon will still be
* visible.
*
* @param baseIcon
* @param overlayIcon
* @return
*/
public static ImageIcon createOverlayIcon(final ImageIcon baseIcon, final ImageIcon overlayIcon) {
if (baseIcon == null) {
throw new IllegalArgumentException("baseIcon must not be null!");
}
if (overlayIcon == null) {
throw new IllegalArgumentException("overlayIcon must not be null!");
}
BufferedImage bufferedImg = new BufferedImage(baseIcon.getIconWidth(), baseIcon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D) bufferedImg.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform identityAT = new AffineTransform();
g2.drawImage(baseIcon.getImage(), identityAT, null);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
g2.drawImage(overlayIcon.getImage(), identityAT, null);
g2.dispose();
return new ImageIcon(bufferedImg);
}
/**
* Returns a scaled down image if the height or width is smaller than the
* image size.
*
* @param icon
* the image icon.
* @param newHeight
* the preferred height.
* @param newWidth
* the preferred width.
* @return the icon.
*/
public static ImageIcon scaleImageIcon(ImageIcon icon, int newHeight,
int newWidth) {
Image img = icon.getImage();
int height = icon.getIconHeight();
int width = icon.getIconWidth();
if (height > newHeight) {
height = newHeight;
}
if (width > newWidth) {
width = newWidth;
}
img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
return new ImageIcon(img);
}
private Icon readIconFromFile( File iconFile ) {
try {
Image img = ImageIO.read( iconFile.toURI().toURL() );
if( null != img ) {
ImageIcon res = new ImageIcon( img );
if( res.getIconWidth() > 32 || res.getIconHeight() > 32 ) {
JOptionPane.showMessageDialog(this, NbBundle.getMessage(TextImporterUI.class, "Err_IconTooBig"), //NOI18N
NbBundle.getMessage(TextImporterUI.class, "Err_Title"), JOptionPane.ERROR_MESSAGE ); //NOI18N
return null;
}
return res;
}
} catch( ThreadDeath td ) {
throw td;
} catch( Throwable ioE ) {
//ignore
}
JOptionPane.showMessageDialog(this,
NbBundle.getMessage(TextImporterUI.class, "Err_CannotLoadIconFromFile", iconFile.getName()), //NOI18N
NbBundle.getMessage(TextImporterUI.class, "Err_Title"), JOptionPane.ERROR_MESSAGE ); //NOI18N
return null;
}
/**
* Returns an icon with custom size with another icon centered in it.
* @param anotherIcon another icon to be centered in it
* @param width width of the icon
* @param height height of the icon
* @param zoom zoom factor of <code>anotherIcon</code>
* @return an icon with custom size with another icon centered in it
*/
public static Icon getCustomIcon( final ImageIcon anotherIcon, final int width, final int height, final int zoom ) {
return new Icon() {
@Override
public void paintIcon( final Component c, final Graphics g, final int x, final int y ) {
if ( zoom == 1 ) {
g.drawImage( anotherIcon.getImage(), x + ( ( width - anotherIcon.getIconWidth() ) >> 1 ), y + ( ( height - anotherIcon.getIconHeight() ) >> 1 ), null );
}
else {
final int iconWidth = anotherIcon.getIconWidth () * zoom;
final int iconHeight = anotherIcon.getIconHeight() * zoom;
g.drawImage( anotherIcon.getImage(), x + ( ( width - iconWidth ) >> 1 ), y + ( ( height - iconHeight ) >> 1 ), iconWidth, iconHeight, null );
}
}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return height;
}
};
}
@Override
public void serialize(ImageIcon vi, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
synchronized (vi) {
BufferedImage v = new BufferedImage(
vi.getIconWidth(),
vi.getIconHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics g = v.createGraphics();
// paint the Icon to the BufferedImage.
vi.paintIcon(null, g, 0, 0);
g.dispose();
byte [] data = Images.encode(v);
jgen.writeStartObject();
jgen.writeStringField("type", "ImageIcon");
jgen.writeObjectField("imageData", data);
jgen.writeNumberField("width", v.getWidth());
jgen.writeNumberField("height", v.getHeight());
jgen.writeEndObject();
}
}
/**
* 使用RescaleOp对图片进行滤镜处理.
*
* @param iconBottom 原图
* @param redFilter 红色通道滤镜值,1.0f表示保持不变
* @param greenFilter 绿色通道滤镜值,1.0f表示保持不变
* @param blueFilter 蓝色通道滤镜值,1.0f表示保持不变
* @param alphaFilter alpha通道滤镜值,1.0f表示保持不变
* @return 处理后的图片新对象
* @author Jack Jiang, 2013-04-05
* @since 3.5
*/
public static ImageIcon filterWithRescaleOp(ImageIcon iconBottom
, float redFilter, float greenFilter, float blueFilter, float alphaFilter)
{
try
{
int w = iconBottom.getIconWidth(), h = iconBottom.getIconHeight();
//原图
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D gg = (Graphics2D)bi.getGraphics();
gg.drawImage(iconBottom.getImage(), 0, 0,w, h,null);
//设置滤镜效果
float[] scales = { redFilter, greenFilter, blueFilter,alphaFilter };
float[] offsets = new float[4];
RescaleOp rop = new RescaleOp(scales, offsets, null);
//执行
// gg.drawImage(bi, rop, 0, 0);//用这一行代码没效果,用下一行代码即可!
rop.filter(bi, bi);
return new ImageIcon(bi);
}
catch (Exception e)
{
LogHelper.error("filterWithRescaleOp出错了,"+e.getMessage()+",iconBottom="+iconBottom);
return new ImageIcon();
}
}
private static ImageIcon createPngIcon(URL url) throws Exception {
ImageIcon appIcon = new ImageIcon(url);
if (appIcon.getIconHeight() > SIZE || appIcon.getIconWidth() > SIZE) {
BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = addRenderingHints(image);
g2d.drawImage(appIcon.getImage(), 0, 0, SIZE, SIZE, null);
g2d.dispose();
return new ImageIcon(image);
}
return appIcon;
}
/**
* Constructs a new field for displaying an image.
* @param icon the image icon to display
* @param metrics the font metrics
* @param startX the starting x coordinate of the field.
* @param startY the starting y coordinate of the field.
* @param width the width of the field.
* @param center flag to center the image in the field.
*/
public SimpleImageField(ImageIcon icon, FontMetrics metrics, int startX, int startY, int width,
boolean center) {
this.heightAbove = metrics.getMaxAscent() + metrics.getLeading();
this.height = heightAbove + metrics.getMaxDescent();
this.icon = icon;
this.metrics = metrics;
this.startX = startX;
this.width = width;
this.center = center;
// The height is initially set to the font height.
// If the font height is less than the icon height and the provided width
// is the less than the icon width, then scale the height relative to the
// width. Otherwise, use the icon height.
//
if (icon != null) {
if (this.height < icon.getIconHeight()) {
if (this.width < icon.getIconWidth()) {
this.height = (width * icon.getIconHeight()) / icon.getIconWidth();
}
else {
this.height = icon.getIconHeight();
}
}
}
}
/**
* Returns a scaled down image if the height or width is smaller than the
* image size.
*
* @param icon
* the image icon.
* @param newHeight
* the preferred height.
* @param newWidth
* the preferred width.
* @return the icon.
*/
public static ImageIcon scale(ImageIcon icon, int newHeight, int newWidth) {
Image img = icon.getImage();
int height = icon.getIconHeight();
int width = icon.getIconWidth();
boolean scaleHeight = height * newWidth > width * newHeight;
if (height > newHeight) {
// Too tall
if (width <= newWidth || scaleHeight) {
// Width is okay or height is limiting factor due to aspect
// ratio
height = newHeight;
width = -1;
} else {
// Width is limiting factor due to aspect ratio
height = -1;
width = newWidth;
}
} else if (width > newWidth) {
// Too wide and height is okay
height = -1;
width = newWidth;
} else if (scaleHeight) {
// Height is limiting factor due to aspect ratio
height = newHeight;
width = -1;
} else {
// Width is limiting factor due to aspect ratio
height = -1;
width = newWidth;
}
img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
return new ImageIcon(img);
}
/**
* @param icon file representing icon
* @return width and height of icon encapsulated into {@link java.awt.Dimension}
*/
public static Dimension getIconDimension(final File icon) {
try {
ImageIcon imc = new ImageIcon(Utilities.toURI(icon).toURL());
return new Dimension(imc.getIconWidth(), imc.getIconHeight());
} catch (MalformedURLException ex) {
ErrorManager.getDefault().notify(ex);
}
return new Dimension(-1, -1);
}
/**
* Create background tile when MainDesktopPane is first displayed. Recenter
* logoLabel on MainWindow and set backgroundLabel to the size of
* MainDesktopPane.
*
* @param e the component event
*/
@Override
public void componentResized(ComponentEvent e) {
// If displayed for the first time, create background image tile.
// The size of the background tile cannot be determined during construction
// since it requires the MainDesktopPane be displayed first.
if (firstDisplay) {
ImageIcon baseImageIcon = ImageLoader.getIcon(Msg.getString("img.background")); //$NON-NLS-1$
Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
Image backgroundImage = createImage((int) screen_size.getWidth(), (int) screen_size.getHeight());
Graphics backgroundGraphics = backgroundImage.getGraphics();
for (int x = 0; x < backgroundImage.getWidth(this); x += baseImageIcon.getIconWidth()) {
for (int y = 0; y < backgroundImage.getHeight(this); y += baseImageIcon.getIconHeight()) {
backgroundGraphics.drawImage(baseImageIcon.getImage(), x, y, this);
}
}
backgroundImageIcon.setImage(backgroundImage);
backgroundLabel.setSize(getSize());
firstDisplay = false;
}
// Set the backgroundLabel size to the size of the desktop
backgroundLabel.setSize(getSize());
}
/**
* resize and create the preview image
*
* @param path
*/
private void setPreview(String path) {
ImageIcon icon = new ImageIcon(path);
int w = icon.getIconWidth() > PREVIEW_SIZE.width ? PREVIEW_SIZE.width : icon.getIconWidth();
int h = icon.getIconHeight() > PREVIEW_SIZE.height ? PREVIEW_SIZE.height : icon.getIconHeight();
Image img = icon.getImage().getScaledInstance(w, h, Image.SCALE_SMOOTH);
previewObj.setIcon(new ImageIcon(img));
previewObj.setHorizontalAlignment(CENTER);
previewObj.setVerticalAlignment(CENTER);
previewObj.repaint();
}
/**
* Rescales an icon.
* @param src the original icon.
* @param newMinSize the minimum size of the new icon. The width and height of
* the returned icon will be at least the width and height
* respectively of newMinSize.
* @param an ImageObserver.
* @return the rescaled icon.
*/
public static ImageIcon rescale(ImageIcon src, Dimension newMinSize, ImageObserver observer) {
double widthRatio = newMinSize.getWidth() / (double) src.getIconWidth();
double heightRatio = newMinSize.getHeight() / (double) src.getIconHeight();
double scale = widthRatio > heightRatio ? widthRatio : heightRatio;
int w = (int) Math.round(scale * src.getIconWidth());
int h = (int) Math.round(scale * src.getIconHeight());
BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = dst.createGraphics();
g2.drawImage(src.getImage(), 0, 0, w, h, observer);
g2.dispose();
return new ImageIcon(dst);
}
/**
* Converts a given Image into a BufferedImage
*
* @param img The Image to be converted
* @return The converted BufferedImage
*/
public static BufferedImage toBufferedImage(final ImageIcon img) {
final BufferedImage bimage = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
final Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img.getImage(), 0, 0, null);
bGr.dispose();
return bimage;
}
private BufferedImage createDefaultPortrait()
{
ImageIcon defaultPortrait = Icons.DefaultPortrait.getImageIcon();
BufferedImage bufImage = new BufferedImage(defaultPortrait.getIconWidth(), defaultPortrait.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
defaultPortrait.paintIcon(this, bufImage.createGraphics(), 0, 0);
return bufImage;
}
public ResizerPanel(String texture) {
super();
ImageIcon img = edu.xtec.util.ResourceManager.getImageIcon(texture);
int w = img.getIconWidth();
int h = img.getIconHeight();
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D big = bi.createGraphics();
big.setBackground(getBackground());
big.drawImage(img.getImage(), 0, 0, getBackground(), null);
Rectangle r = new Rectangle(0, 0, w, h);
tp = new TexturePaint(bi, r);
}
/**
* Returns a scaled down image if the height or width is smaller than the
* image size.
*
* @param icon
* the image icon.
* @param newHeight
* the preferred height.
* @param newWidth
* the preferred width.
* @return the icon.
*/
public static ImageIcon scale(ImageIcon icon, int newHeight, int newWidth) {
Image img = icon.getImage();
int height = icon.getIconHeight();
int width = icon.getIconWidth();
boolean scaleHeight = height * newWidth > width * newHeight;
if (height > newHeight) {
// Too tall
if (width <= newWidth || scaleHeight) {
// Width is okay or height is limiting factor due to aspect
// ratio
height = newHeight;
width = -1;
} else {
// Width is limiting factor due to aspect ratio
height = -1;
width = newWidth;
}
} else if (width > newWidth) {
// Too wide and height is okay
height = -1;
width = newWidth;
} else if (scaleHeight) {
// Height is limiting factor due to aspect ratio
height = newHeight;
width = -1;
} else {
// Width is limiting factor due to aspect ratio
height = -1;
width = newWidth;
}
img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
return new ImageIcon(img);
}
/**
* Initialize witht he specified image icon.
* @param logoIcon an icon contain the imge of a logo.
*/
public ImageData(final ImageIcon logoIcon) {
img = logoIcon.getImage();
imgw = logoIcon.getIconWidth();
imgh = logoIcon.getIconHeight();
}
/**
* Paints a tiled icon.
*
* @param c The Component to paint onto
* @param g The Graphics to paint with
* @param icon The icon to paint
* @param sX The starting x location to paint the icon at
* @param sY The starting y location to paint the icon at
* @param width The width of the space that needs to be filled with
* the tiled icon
* @param height The height of the space that needs to be filled with
* the tiled icon
*/
private void paintTiledIcon(Component c, Graphics g, ImageIcon icon,
int sX, int sY, int width, int height){
int tileW = icon.getIconWidth();
int tileH = icon.getIconHeight();
width += sX;
height += sY;
for (int x = sX; x <= width; x += tileW) {
for (int y = sY; y <= height; y += tileH) {
icon.paintIcon(c, g, x, y);
}
}
}