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.
60 lines
1.6 KiB
60 lines
1.6 KiB
4 years ago
|
package com.fr.base.svg;
|
||
|
|
||
|
import javax.swing.Icon;
|
||
|
import java.awt.Component;
|
||
|
import java.awt.Graphics;
|
||
|
import java.awt.Graphics2D;
|
||
|
import java.awt.image.BufferedImage;
|
||
|
|
||
|
/**
|
||
|
* SVG转化而来的Icon
|
||
|
* @author Yvan
|
||
|
* @version 10.0
|
||
|
* Created by Yvan on 2020/12/17
|
||
|
*/
|
||
|
public class SVGIcon implements Icon {
|
||
|
|
||
|
private BufferedImage image;
|
||
|
|
||
|
private static final boolean HI_DPI_SURPORT = SystemScaleUtils.isJreHiDPIEnabled();
|
||
|
|
||
|
public static final float SYSTEM_SCALE = SystemScaleUtils.sysScale();
|
||
|
|
||
|
public SVGIcon(BufferedImage image) {
|
||
|
this.image = image;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void paintIcon(Component c, Graphics g, int x, int y) {
|
||
|
if (HI_DPI_SURPORT) {
|
||
|
Graphics2D graphics = (Graphics2D) g.create(x, y, image.getWidth(null), image.getHeight(null));
|
||
|
float scale = SYSTEM_SCALE;
|
||
|
graphics.scale(1 / scale, 1 / scale);
|
||
|
graphics.drawImage(image, 0, 0, null);
|
||
|
graphics.scale(1.0D, 1.0D);
|
||
|
graphics.dispose();
|
||
|
} else {
|
||
|
g.drawImage(image, x, y, null);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int getIconWidth() {
|
||
|
return HI_DPI_SURPORT ? (int) (image.getWidth() / SYSTEM_SCALE) : image.getWidth();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public int getIconHeight() {
|
||
|
return HI_DPI_SURPORT ? (int) (image.getHeight() / SYSTEM_SCALE) : image.getHeight();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 外界读取高清图标
|
||
|
* @param url
|
||
|
* @return
|
||
|
*/
|
||
|
public static Icon readSVGIcon(String url) {
|
||
|
return new SVGIcon((BufferedImage) SVGLoader.load(url));
|
||
|
}
|
||
|
}
|