Java fonts not displaying correctly

when I import a font from a file it displays very strangely. he should have said "TEST" but he just said "_".

here is my code:

public void paint(Graphics g2){
Graphics2D g = (Graphics2D)g2;
//<editor-fold defaultstate="collapsed" desc="FontGetter">

Font f = null;
try {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.registerFont((f = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("Triforce.ttf"))));
} catch (IOException e) {
    e.printStackTrace();
}
catch(FontFormatException e)
{
    e.printStackTrace();
}
//</editor-fold>
g.setFont(f.deriveFont(2));
g.drawString("sdfsdfetf", 100, 100);
}

      

0


source to share


1 answer


As pointed out by Font#createFont

Java Docs

... The new font is created with point size 1 and style PLAIN ...

(accent applied by me)

This means you need to get a new Font

instance from this Font

with the properties you want , like

try {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font f = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/Triforce.ttf"));
    if (!ge.registerFont(f)) {
        System.out.println("Unable to register font");
    }
    f = f.deriveFont(Font.PLAIN, 24);
    setFont(f);
} catch (IOException e) {
    e.printStackTrace();
} catch (FontFormatException e) {
    e.printStackTrace();
}

      



Font

You must first load the font (and apply its properties) outside of the context of the paint method. The painting should be done as quickly as possible and will be called several times, sometimes quickly. Loading resources within any drawing method wastes time and resource ...

As an example...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test1 {

    public static void main(String[] args) {
        new Test1();
    }

    public Test1() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            try {
                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                Font f = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/Triforce.ttf"));
                if (!ge.registerFont(f)) {
                    System.out.println("Unable to register font");
                }
                f = f.deriveFont(Font.PLAIN, 24);
                setFont(f);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (FontFormatException e) {
                e.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            FontMetrics fm = g2d.getFontMetrics();
            String text = "Hello world";
            int x = (getWidth() - fm.stringWidth(text)) / 2;
            int y = ((getHeight() - fm.getHeight()) + fm.getAscent()) / 2;
            g2d.drawString(text, x, y);
            g2d.dispose();
        }

    }

}

      

+1


source







All Articles