Search code examples
javaswingiconsfont-awesome

How do I use FontAwesome 6 icons that are based on a true type font into a Icon for use in Java


This answer from madprogrammer shows how I can display a FontAwesome icon on a JLabel Font Awesome with Swing but my action code is of the form

public class EmptyDatabaseAction extends AbstractAction
{

    public EmptyDatabaseAction()
    {
        super(TextLabel.MENU_EMPTY_DB.getMsg());
        this.putValue(Action.SMALL_ICON, Icons.EMPTY_DATABASE.getIconSmall());
        this.putValue(Action.LARGE_ICON_KEY, Icons.EMPTY_DATABASE.getIconLarge());
        this.putValue(Action.SHORT_DESCRIPTION,TextLabel.MENU_EMPTY_DB.getMsg());
    }
    .....
}

I.e I define a SMALL_ICON and LARGE_ICON_KEY, it expects subclass of Icon (actually ImageIcon), but the FontAwesome is based on Fonts, so how do I convert a Font based image to an Icon ?


Solution

  • Got it working, merged code from two answers to Font Awesome with Swing, madprogrammers and jIconFont code.

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.InputStream;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class FontAwesomeImageCreator
    {
    
        public static void main(String[] args) throws Exception
        {
            InputStream is = FontAwesomeImageCreator.class.getResourceAsStream("/fa-thin-100.ttf");
            Font font = Font.createFont(Font.TRUETYPE_FONT, is);
            font = font.deriveFont(Font.PLAIN, 128f);
            saveImage("\ue024", font, Color.BLACK, "radar.png");
            saveImage("\ue024", font, Color.WHITE, "radar_dark.png");
        }
    
    
        private static void saveImage(String text, Font font, Color color, String filename) throws Exception
        {
            BufferedImage bi = buildImage (text, font, color);
            File outputfile = new File("C:\\Code\\icons", filename);
            ImageIO.write(bi, "png", outputfile);
        }
    
        private static BufferedImage buildImage(String text, Font font, Color color) {
            JLabel label = new JLabel(text);
            label.setForeground(color);
            label.setFont(font);
            Dimension dim = label.getPreferredSize();
            int width = dim.width + 1;
            int height = dim.height + 1;
            label.setSize(width, height);
            BufferedImage bufImage =
                    new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = bufImage.createGraphics();
            g2d.setRenderingHint(
                    RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g2d.setRenderingHint(
                    RenderingHints.KEY_FRACTIONALMETRICS,
                    RenderingHints.VALUE_FRACTIONALMETRICS_ON);
            label.print(g2d);
            g2d.dispose();
            return bufImage;
        }
    
    }
    

    So in this example hardcoded to use the Font Awesome 6 thin style, ands create a transparent black and white bitmap image of size 128 x 128 for the radar icon. Could be easily enough parameterized for more general use.