You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.3 KiB
118 lines
3.3 KiB
package com.fr.design.icon; |
|
|
|
import com.fr.base.svg.SVGLoader; |
|
import com.fr.base.svg.SystemScaleUtils; |
|
import com.fr.design.utils.SvgDrawUtils; |
|
import com.fr.log.FineLoggerFactory; |
|
|
|
import javax.swing.GrayFilter; |
|
import javax.swing.ImageIcon; |
|
import java.awt.Component; |
|
import java.awt.Graphics; |
|
import java.awt.Graphics2D; |
|
import java.awt.Image; |
|
import java.awt.MediaTracker; |
|
import java.awt.image.ImageObserver; |
|
|
|
/** |
|
* 警告图片 |
|
* |
|
* @author zhou |
|
* @since 2012-3-28下午10:20:29 |
|
*/ |
|
public class WarningIcon extends ImageIcon { |
|
protected final static Component COMPONENT = new Component() { |
|
}; |
|
protected final static MediaTracker TRACKER = new MediaTracker(COMPONENT); |
|
private final static Image WARNING_IMAGE = SVGLoader.load("/com/fr/design/standard/warning.svg"); |
|
private static final boolean HI_DPI_SUPPORT = SystemScaleUtils.isJreHiDPIEnabled(); |
|
public static final float SYSTEM_SCALE = SystemScaleUtils.sysScale(); |
|
private Image mainImage = null; |
|
private ImageObserver imageObserver; |
|
private int width = -1; |
|
private int height = -1; |
|
|
|
public WarningIcon(Image mainImage) { |
|
this.mainImage = mainImage; |
|
|
|
if (this.mainImage != null) { |
|
loadImage(this.mainImage); |
|
} |
|
} |
|
|
|
@Override |
|
public synchronized void paintIcon(Component c, Graphics g, int x, int y) { |
|
//裁剪绘制svg的位置,以免影响到图标右侧的文字 |
|
Graphics2D graphics = (Graphics2D) g.create(x, y, WARNING_IMAGE.getWidth(null), WARNING_IMAGE.getHeight(null)); |
|
if (mainImage != null) { |
|
SvgDrawUtils.doDrawSVG(graphics, () -> SvgDrawUtils.drawImage(graphics, mainImage, x, y, null)); |
|
} |
|
if (WARNING_IMAGE != null) { |
|
SvgDrawUtils.doDrawSVG(graphics, () -> SvgDrawUtils.drawImage(graphics, WARNING_IMAGE, x, y, null)); |
|
} |
|
graphics.dispose(); |
|
} |
|
|
|
/** |
|
* Loads the image, returning only when the image is loaded. |
|
* |
|
* @param image |
|
* the image |
|
*/ |
|
protected void loadImage(Image image) { |
|
synchronized (TRACKER) { |
|
TRACKER.addImage(image, 0); |
|
try { |
|
TRACKER.waitForID(0, 0); |
|
} catch (InterruptedException e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
Thread.currentThread().interrupt(); |
|
} |
|
|
|
TRACKER.statusID(0, false); |
|
TRACKER.removeImage(image, 0); |
|
|
|
width = image.getWidth(imageObserver); |
|
height = image.getHeight(imageObserver); |
|
} |
|
} |
|
|
|
/** |
|
* Returns this icon's <code>Image</code>. |
|
* |
|
* @return the <code>Image</code> object for this <code>ImageIcon</code> |
|
*/ |
|
public Image getImage() { |
|
return mainImage; |
|
} |
|
|
|
public LockIcon getDisabledLockIcon() { |
|
Image mainDisabledImage = GrayFilter.createDisabledImage(mainImage); |
|
|
|
return new LockIcon(mainDisabledImage); |
|
} |
|
|
|
/** |
|
* Gets the width of the icon. |
|
* |
|
* @return the width in pixels of this icon |
|
*/ |
|
public int getIconWidth() { |
|
//如果环境支持高清化,drawImage可能会缩放绘制的图像比例,需要保证svg正常显示的同时调整绘制范围 |
|
return HI_DPI_SUPPORT ? (int) (width / SYSTEM_SCALE) : width; |
|
} |
|
|
|
/** |
|
* Gets the height of the icon. |
|
* |
|
* @return the height in pixels of this icon |
|
*/ |
|
public int getIconHeight() { |
|
//如果环境支持高清化,drawImage可能会缩放绘制的图像比例,需要保证svg正常显示的同时调整绘制范围 |
|
return HI_DPI_SUPPORT ? (int) (height / SYSTEM_SCALE) : height; |
|
} |
|
|
|
{ |
|
loadImage(WARNING_IMAGE); |
|
} |
|
} |