jiconfont.swing.IconFontSwing Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jiconfont-swing Show documentation
Show all versions of jiconfont-swing Show documentation
jIconFont-Swing is a API to provide icons generated from any IconFont. These icons can be used in
Swing.
package jiconfont.swing;
import jiconfont.IconCode;
import jiconfont.IconFont;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Copyright (c) 2016 jIconFont
*
* 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.
*/
public class IconFontSwing {
private static List fonts = new ArrayList<>();
public static synchronized void register(IconFont iconFont) {
if (IconFontSwing.fonts.contains(iconFont) == false) {
IconFontSwing.fonts.add(iconFont);
}
}
public static synchronized final Font buildFont(String fontFamily) {
try {
for (IconFont iconFont : IconFontSwing.fonts) {
if (iconFont.getFontFamily().equals(fontFamily)) {
return Font.createFont(Font.TRUETYPE_FONT, iconFont.getFontInputStream());
}
}
} catch (Exception ex) {
Logger.getLogger(IconFontSwing.class.getName()).log(Level.SEVERE,
"Font load failure", ex);
}
Logger.getLogger(IconFontSwing.class.getName()).log(Level.SEVERE,
"Font not found: " + fontFamily);
throw new IllegalArgumentException("Font not found: " + fontFamily);
}
private IconFontSwing() {
}
public static Image buildImage(IconCode iconCode, float size) {
return buildImage(iconCode, size, Color.BLACK);
}
public static Image buildImage(IconCode iconCode, float size, Color color) {
Font font = buildFont(iconCode, size);
String text = Character.toString(iconCode.getUnicode());
return buildImage(text, font);
}
public static Icon buildIcon(IconCode iconCode, float size) {
return buildIcon(iconCode, size, Color.BLACK);
}
public static Icon buildIcon(IconCode iconCode, float size, Color color) {
return new ImageIcon(buildImage(iconCode, size, color));
}
private static BufferedImage buildImage(String text, Font font) {
JLabel label = new JLabel(text);
label.setFont(font);
Dimension dim = label.getPreferredSize();
label.setSize(dim);
int width = dim.width;
int height = dim.height;
BufferedImage bufImage =
new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bufImage.createGraphics();
label.print(g2d);
g2d.dispose();
return bufImage;
}
private static Font buildFont(IconCode iconCode, float size) {
Font font = IconFontSwing.buildFont(iconCode.getFontFamily());
return font.deriveFont(size);
}
}