From 02734d0a972b23fb42c615ec2a871a7f2845278b Mon Sep 17 00:00:00 2001 From: weisj Date: Thu, 5 Nov 2020 18:53:41 +0100 Subject: [PATCH] Add OverlayIcon and TextIcon. --- .../weisj/darklaf/icons/OverlayIcon.java | 77 +++++++++++++++++ .../github/weisj/darklaf/icons/TextIcon.java | 85 +++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 property-loader/src/main/java/com/github/weisj/darklaf/icons/OverlayIcon.java create mode 100644 property-loader/src/main/java/com/github/weisj/darklaf/icons/TextIcon.java diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/OverlayIcon.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/OverlayIcon.java new file mode 100644 index 00000000..d09eac7b --- /dev/null +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/OverlayIcon.java @@ -0,0 +1,77 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jannis Weis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ +package com.github.weisj.darklaf.icons; + +import java.awt.*; +import java.util.Objects; + +import javax.swing.*; + +import com.github.weisj.darklaf.util.Alignment; + +public class OverlayIcon implements Icon { + + private Icon icon; + private Icon overlay; + private Alignment alignment; + + public OverlayIcon(final Icon icon, final Icon overlay, final Alignment alignment) { + setIcon(icon); + setOverlay(overlay); + setAlignment(alignment); + } + + public Icon getIcon() { + return icon; + } + + public void setIcon(final Icon icon) { + this.icon = icon != null ? icon : EmptyIcon.create(0); + } + + public void setOverlay(final Icon overlay) { + this.overlay = overlay != null ? overlay : EmptyIcon.create(0); + } + + public void setAlignment(final Alignment alignment) { + Objects.requireNonNull(alignment); + this.alignment = alignment; + } + + @Override + public void paintIcon(final Component c, final Graphics g, final int x, final int y) { + icon.paintIcon(c, g, x, y); + Point pos = alignment.alignInside(new Dimension(overlay.getIconWidth(), overlay.getIconHeight()), + new Rectangle(x, y, getIconWidth(), getIconHeight())); + overlay.paintIcon(c, g, pos.x, pos.y); + } + + @Override + public int getIconWidth() { + return icon.getIconWidth(); + } + + @Override + public int getIconHeight() { + return icon.getIconHeight(); + } +} diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/TextIcon.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/TextIcon.java new file mode 100644 index 00000000..81a991b6 --- /dev/null +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/TextIcon.java @@ -0,0 +1,85 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jannis Weis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ +package com.github.weisj.darklaf.icons; + +import java.awt.*; + +import javax.swing.*; + +import com.github.weisj.darklaf.util.FontUtil; + +public class TextIcon implements Icon, DerivableIcon { + + private final String text; + private final Color color; + private final Font font; + private final int width; + private final int height; + private final int textOffset; + + public TextIcon(final String text, final Color color, final Font font, final int width, final int height) { + this(text, color, font, width, height, 0); + } + + public TextIcon(final String text, final Color color, final Font font, final int width, final int height, + final int textOffset) { + this.text = text; + this.color = color; + this.font = font; + this.width = width; + this.height = height; + this.textOffset = textOffset; + } + + @Override + public void paintIcon(final Component c, final Graphics gg, final int x, final int y) { + Graphics2D g = (Graphics2D) gg.create(); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT); + g.setFont(font); + FontMetrics fm = g.getFontMetrics(font); + int w = fm.stringWidth(text); + int textX = x + (getIconWidth() - w) / 2; + int textY = y + FontUtil.getCenteredFontPosition(height, fm) + fm.getAscent(); + textY += textOffset; + g.setColor(color); + g.drawString(text, textX, textY); + g.dispose(); + } + + @Override + public int getIconWidth() { + return width; + } + + @Override + public int getIconHeight() { + return height; + } + + @Override + public TextIcon derive(final int width, final int height) { + int off = Math.round(height * ((float) this.textOffset / this.height)); + Font f = font.deriveFont(height * (font.getSize2D() / this.height)); + return new TextIcon(text, color, f, width, height, off); + } +}