|
|
@ -3,6 +3,7 @@ package com.fine.theme.icon.svg; |
|
|
|
import com.fine.theme.icon.DisabledIcon; |
|
|
|
import com.fine.theme.icon.DisabledIcon; |
|
|
|
import com.fine.theme.icon.GraphicsFilter; |
|
|
|
import com.fine.theme.icon.GraphicsFilter; |
|
|
|
import com.fine.theme.icon.IconResource; |
|
|
|
import com.fine.theme.icon.IconResource; |
|
|
|
|
|
|
|
import com.fine.theme.icon.IconType; |
|
|
|
import com.fine.theme.icon.WhiteIcon; |
|
|
|
import com.fine.theme.icon.WhiteIcon; |
|
|
|
import com.formdev.flatlaf.FlatLaf; |
|
|
|
import com.formdev.flatlaf.FlatLaf; |
|
|
|
import com.formdev.flatlaf.ui.FlatUIUtils; |
|
|
|
import com.formdev.flatlaf.ui.FlatUIUtils; |
|
|
@ -42,41 +43,28 @@ import static com.fine.theme.utils.FineUIScale.scale; |
|
|
|
@Immutable |
|
|
|
@Immutable |
|
|
|
public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { |
|
|
|
public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { |
|
|
|
|
|
|
|
|
|
|
|
public enum Type { |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 灰化图 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
disable, |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 白化图,用于反白场景 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
white, |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 原始效果图 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
origin |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final Dimension size; |
|
|
|
private final Dimension size; |
|
|
|
|
|
|
|
private final Dimension scaleSize; |
|
|
|
private final IconResource resource; |
|
|
|
private final IconResource resource; |
|
|
|
private final Type type; |
|
|
|
private final IconType type; |
|
|
|
|
|
|
|
|
|
|
|
private final NullableLazyValue<SVGDocument> svgDocument = NullableLazyValue.createValue(() -> load(Type.origin)); |
|
|
|
private final NullableLazyValue<SVGDocument> svgDocument = NullableLazyValue.createValue(() -> load(IconType.normal)); |
|
|
|
private final NullableLazyValue<SVGDocument> whiteSvgDocument = NullableLazyValue.createValue(() -> load(Type.white)); |
|
|
|
private final NullableLazyValue<SVGDocument> whiteSvgDocument = NullableLazyValue.createValue(() -> load(IconType.white)); |
|
|
|
|
|
|
|
|
|
|
|
public SvgIcon(IconResource resource, Dimension size) { |
|
|
|
public SvgIcon(IconResource resource, Dimension size) { |
|
|
|
this(resource, size, Type.origin); |
|
|
|
this(resource, size, IconType.normal); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public SvgIcon(IconResource resource, Dimension size, Type type) { |
|
|
|
public SvgIcon(IconResource resource, Dimension size, IconType type) { |
|
|
|
this.resource = resource; |
|
|
|
this.resource = resource; |
|
|
|
|
|
|
|
this.size = size; |
|
|
|
// 根据dpi进行缩放
|
|
|
|
// 根据dpi进行缩放
|
|
|
|
this.size = scale(size); |
|
|
|
this.scaleSize = scale(size); |
|
|
|
this.type = type; |
|
|
|
this.type = type; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public SvgIcon(IconResource resource, int side) { |
|
|
|
public SvgIcon(IconResource resource, int side) { |
|
|
|
this(resource, new Dimension(side, side), Type.origin); |
|
|
|
this(resource, new Dimension(side, side), IconType.normal); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -85,7 +73,7 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void paintIcon(Component c, Graphics g, int x, int y) { |
|
|
|
public void paintIcon(Component c, Graphics g, int x, int y) { |
|
|
|
if (type == Type.disable) { |
|
|
|
if (type == IconType.disable) { |
|
|
|
g = grayGraphics(g); |
|
|
|
g = grayGraphics(g); |
|
|
|
} |
|
|
|
} |
|
|
|
Object[] oldRenderingHints = FlatUIUtils.setRenderingHints(g); |
|
|
|
Object[] oldRenderingHints = FlatUIUtils.setRenderingHints(g); |
|
|
@ -104,22 +92,22 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public int getIconWidth() { |
|
|
|
public int getIconWidth() { |
|
|
|
return size.width; |
|
|
|
return scaleSize.width; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public int getIconHeight() { |
|
|
|
public int getIconHeight() { |
|
|
|
return size.height; |
|
|
|
return scaleSize.height; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void render(Component c, Graphics g, int x, int y) { |
|
|
|
private void render(Component c, Graphics g, int x, int y) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (type == Type.white) { |
|
|
|
if (type == IconType.white) { |
|
|
|
Objects.requireNonNull(whiteSvgDocument.getValue()) |
|
|
|
Objects.requireNonNull(whiteSvgDocument.getValue()) |
|
|
|
.render((JComponent) c, (Graphics2D) g, new ViewBox(x, y, size.width, size.height)); |
|
|
|
.render((JComponent) c, (Graphics2D) g, new ViewBox(x, y, scaleSize.width, scaleSize.height)); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Objects.requireNonNull(svgDocument.getValue()) |
|
|
|
Objects.requireNonNull(svgDocument.getValue()) |
|
|
|
.render((JComponent) c, (Graphics2D) g, new ViewBox(x, y, size.width, size.height)); |
|
|
|
.render((JComponent) c, (Graphics2D) g, new ViewBox(x, y, scaleSize.width, scaleSize.height)); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
FineLoggerFactory.getLogger().error("SvgIcon from url: " + resource + "can not paint.", e); |
|
|
|
FineLoggerFactory.getLogger().error("SvgIcon from url: " + resource + "can not paint.", e); |
|
|
@ -127,9 +115,9 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private SVGDocument load(Type type) { |
|
|
|
private SVGDocument load(IconType type) { |
|
|
|
SVGLoader loader = new SVGLoader(); |
|
|
|
SVGLoader loader = new SVGLoader(); |
|
|
|
return type == Type.white |
|
|
|
return type == IconType.white |
|
|
|
? loader.load(resource.getInputStream(), new WhiteParser()) |
|
|
|
? loader.load(resource.getInputStream(), new WhiteParser()) |
|
|
|
: loader.load(resource.getInputStream()); |
|
|
|
: loader.load(resource.getInputStream()); |
|
|
|
} |
|
|
|
} |
|
|
@ -141,6 +129,7 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { |
|
|
|
.add("resource=" + resource) |
|
|
|
.add("resource=" + resource) |
|
|
|
.add("type=" + type) |
|
|
|
.add("type=" + type) |
|
|
|
.add("size=" + size) |
|
|
|
.add("size=" + size) |
|
|
|
|
|
|
|
.add("scaleSize=" + scaleSize) |
|
|
|
.toString(); |
|
|
|
.toString(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -149,7 +138,7 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public @NotNull SvgIcon white() { |
|
|
|
public @NotNull SvgIcon white() { |
|
|
|
return new SvgIcon(resource, size, Type.white); |
|
|
|
return new SvgIcon(resource, size, IconType.white); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -158,6 +147,6 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public @NotNull SvgIcon disabled() { |
|
|
|
public @NotNull SvgIcon disabled() { |
|
|
|
return new SvgIcon(resource, size, Type.disable); |
|
|
|
return new SvgIcon(resource, size, IconType.disable); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|