├── README.md ├── .gitignore ├── LICENSE ├── pom.xml └── src └── main └── java └── jiconfont └── swing └── IconFontSwing.java /README.md: -------------------------------------------------------------------------------- 1 | Visit [http://jiconfont.github.io/swing](http://jiconfont.github.io/swing) 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | pom.xml.tag 3 | pom.xml.releaseBackup 4 | pom.xml.versionsBackup 5 | pom.xml.next 6 | release.properties 7 | dependency-reduced-pom.xml 8 | buildNumber.properties 9 | .mvn/timing.properties 10 | *.iml 11 | .idea/ 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 jIconFont 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | UTF-8 9 | 10 | 11 | com.github.jiconfont 12 | jiconfont-swing 13 | 1.0.2-SNAPSHOT 14 | jar 15 | 16 | jIconFont - Swing 17 | jIconFont-Swing is a API to provide icons generated from any IconFont. These icons can be used in 18 | Swing. 19 | 20 | https://github.com/jIconFont/jiconfont-swing 21 | 22 | 23 | 24 | The MIT License (MIT) 25 | http://opensource.org/licenses/mit-license.html 26 | repo 27 | 28 | 29 | 30 | 31 | scm:git:git@github.com:jIconFont/jiconfont-swing.git 32 | scm:git:git@github.com:jIconFont/jiconfont-swing.git 33 | scm:git:git@github.com:jIconFont/jiconfont-swing.git 34 | HEAD 35 | 36 | 37 | 38 | 39 | caduandrade 40 | Cadu Andrade 41 | cadu@caduandrade.net 42 | 43 | 44 | 45 | 46 | 47 | sonatype-nexus-staging 48 | Sonatype Repository 49 | https://oss.sonatype.org/service/local/staging/deploy/maven2 50 | 51 | 52 | sonatype-nexus-snapshots 53 | Sonatype Repository 54 | https://oss.sonatype.org/content/repositories/snapshots 55 | 56 | 57 | 58 | 59 | 60 | com.github.jiconfont 61 | jiconfont 62 | 1.0.0 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | org.apache.maven.plugins 71 | maven-compiler-plugin 72 | 3.1 73 | 74 | 1.7 75 | 1.7 76 | 77 | 78 | 79 | 80 | org.apache.maven.plugins 81 | maven-source-plugin 82 | 2.4 83 | 84 | 85 | attach-sources 86 | 87 | jar 88 | 89 | 90 | 91 | 92 | 93 | 94 | org.apache.maven.plugins 95 | maven-javadoc-plugin 96 | 2.10.1 97 | 98 | 99 | attach-javadocs 100 | 101 | jar 102 | 103 | 104 | 105 | 106 | 107 | 108 | org.apache.maven.plugins 109 | maven-gpg-plugin 110 | 1.5 111 | 112 | 113 | sign-artifacts 114 | package 115 | 116 | sign 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /src/main/java/jiconfont/swing/IconFontSwing.java: -------------------------------------------------------------------------------- 1 | package jiconfont.swing; 2 | 3 | import jiconfont.IconCode; 4 | import jiconfont.IconFont; 5 | 6 | import javax.swing.Icon; 7 | import javax.swing.ImageIcon; 8 | import javax.swing.JLabel; 9 | import java.awt.*; 10 | import java.awt.image.BufferedImage; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | import java.util.logging.Level; 14 | import java.util.logging.Logger; 15 | 16 | /** 17 | * Copyright (c) 2016 jIconFont
18 | *
19 | * Permission is hereby granted, free of charge, to any person obtaining a copy 20 | * of this software and associated documentation files (the "Software"), to deal 21 | * in the Software without restriction, including without limitation the rights 22 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 23 | * copies of the Software, and to permit persons to whom the Software is 24 | * furnished to do so, subject to the following conditions:
25 | *
26 | * The above copyright notice and this permission notice shall be included in 27 | * all copies or substantial portions of the Software.
28 | *
29 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 30 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 31 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 32 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 33 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 34 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 35 | * SOFTWARE. 36 | */ 37 | public final class IconFontSwing { 38 | 39 | 40 | private static List fonts = new ArrayList<>(); 41 | 42 | /** 43 | * Register an icon font. 44 | * 45 | * @param iconFont the icon font. 46 | */ 47 | public static synchronized void register(IconFont iconFont) { 48 | if (IconFontSwing.fonts.contains(iconFont) == false) { 49 | IconFontSwing.fonts.add(iconFont); 50 | } 51 | } 52 | 53 | /** 54 | * Builds a font. 55 | * 56 | * @param fontFamily the font family. 57 | * @return the font. 58 | */ 59 | public static synchronized final Font buildFont(String fontFamily) { 60 | try { 61 | for (IconFont iconFont : IconFontSwing.fonts) { 62 | if (iconFont.getFontFamily().equals(fontFamily)) { 63 | return Font.createFont(Font.TRUETYPE_FONT, iconFont.getFontInputStream()); 64 | } 65 | } 66 | } catch (Exception ex) { 67 | Logger.getLogger(IconFontSwing.class.getName()).log(Level.SEVERE, 68 | "Font load failure", ex); 69 | } 70 | 71 | Logger.getLogger(IconFontSwing.class.getName()).log(Level.SEVERE, 72 | "Font not found: " + fontFamily); 73 | throw new IllegalArgumentException("Font not found: " + fontFamily); 74 | } 75 | 76 | 77 | private IconFontSwing() { 78 | } 79 | 80 | /** 81 | * Builds an image. 82 | * 83 | * @param iconCode the icon code. 84 | * @param size the size. 85 | * @return the image. 86 | */ 87 | public static Image buildImage(IconCode iconCode, float size) { 88 | return buildImage(iconCode, size, Color.BLACK); 89 | } 90 | 91 | /** 92 | * Builds an image. 93 | * 94 | * @param iconCode the icon code. 95 | * @param size the size. 96 | * @param color the size. 97 | * @return the image. 98 | */ 99 | public static Image buildImage(IconCode iconCode, float size, Color color) { 100 | Font font = buildFont(iconCode, size); 101 | String text = Character.toString(iconCode.getUnicode()); 102 | return buildImage(text, font, color); 103 | } 104 | 105 | /** 106 | * Builds an icon. 107 | * 108 | * @param iconCode the icon code. 109 | * @param size the size. 110 | * @return the icon. 111 | */ 112 | public static Icon buildIcon(IconCode iconCode, float size) { 113 | return buildIcon(iconCode, size, Color.BLACK); 114 | } 115 | 116 | /** 117 | * Builds an icon. 118 | * 119 | * @param iconCode the icon code. 120 | * @param size the size. 121 | * @param color the size. 122 | * @return the icon. 123 | */ 124 | public static Icon buildIcon(IconCode iconCode, float size, Color color) { 125 | return new ImageIcon(buildImage(iconCode, size, color)); 126 | } 127 | 128 | private static BufferedImage buildImage(String text, Font font, Color color) { 129 | JLabel label = new JLabel(text); 130 | label.setForeground(color); 131 | label.setFont(font); 132 | Dimension dim = label.getPreferredSize(); 133 | int width = dim.width + 1; 134 | int height = dim.height + 1; 135 | label.setSize(width, height); 136 | BufferedImage bufImage = 137 | new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 138 | Graphics2D g2d = bufImage.createGraphics(); 139 | g2d.setRenderingHint( 140 | RenderingHints.KEY_TEXT_ANTIALIASING, 141 | RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 142 | g2d.setRenderingHint( 143 | RenderingHints.KEY_FRACTIONALMETRICS, 144 | RenderingHints.VALUE_FRACTIONALMETRICS_ON); 145 | label.print(g2d); 146 | g2d.dispose(); 147 | return bufImage; 148 | } 149 | 150 | private static Font buildFont(IconCode iconCode, float size) { 151 | Font font = IconFontSwing.buildFont(iconCode.getFontFamily()); 152 | return font.deriveFont(size); 153 | } 154 | 155 | 156 | } --------------------------------------------------------------------------------