How do you import a font?

青春壹個敷衍的年華 提交于 2019-11-26 02:25:27

问题


I\'m wondering how you would go about importing a font.

I\'m trying to use a custom downloaded font but since most computers that would go to run this would not have this font as it\'s not a default font. How would I go about making the font work even if they don\'t have the font?

I\'m using it for a gameover screen and need to display a score with it and want the score text to be the same font. This is the image,

\"enter

In case it matters the font name on my computer is Terminal

Edit: I\'m assuming it would have to have the font in the directory of the java file and there would be some way of using that but I\'m not sure how. Or is there a better way?

Edit2: I have found a nice tutorial on how to do it but need some help on how I go about using this... click me for link

Edit3:

URL fontUrl = new URL(\"http://www.webpagepublicity.com/\" + \"free-fonts/a/Airacobra%20Condensed.ttf\");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
g.setFont(font);

Error Message

File: F:\\Computer Science\\draw.java  [line: 252]
Error: F:\\Computer Science\\draw.java:252: font is not public in java.awt.Component; cannot be accessed from outside package

Here is what I\'m trying:

URL fontUrl = new URL(\"http://img.dafont.com/dl/?f=badaboom_bb\");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
g.setFont(font);

Edit4:

File fontfile = new File(\"TexasLED.ttf\");
File.toURI(fontfile).toURL(fontfile);
URL fontUrl = new URL(\"fontfile\");

Error

Error: F:\\Computer Science\\draw.java:250: toURI() in java.io.File cannot be applied to (java.io.File)

回答1:


'Airacobra Condensed' font available from Download Free Fonts.

import java.awt.*;
import javax.swing.*;
import java.net.URL;

class LoadFont {
    public static void main(String[] args) throws Exception {
        // This font is < 35Kb.
        URL fontUrl = new URL("http://www.webpagepublicity.com/" +
            "free-fonts/a/Airacobra%20Condensed.ttf");
        Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
        GraphicsEnvironment ge = 
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(font);
        JList fonts = new JList( ge.getAvailableFontFamilyNames() );
        JOptionPane.showMessageDialog(null, new JScrollPane(fonts));
    }
}

OK, that was fun, but what does this font actually look like?

import java.awt.*;
import javax.swing.*;
import java.net.URL;

class DisplayFont {
    public static void main(String[] args) throws Exception {
        URL fontUrl = new URL("http://www.webpagepublicity.com/" +
            "free-fonts/a/Airacobra%20Condensed.ttf");
        Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
        font = font.deriveFont(Font.PLAIN,20);
        GraphicsEnvironment ge =
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(font);

        JLabel l = new JLabel(
            "The quick brown fox jumps over the lazy dog. 0123456789");
        l.setFont(font);
        JOptionPane.showMessageDialog(null, l);
    }
}



回答2:


You can use GraphicsEnvironment.registerFont

http://docs.oracle.com/javase/6/docs/api/java/awt/GraphicsEnvironment.html#registerFont(java.awt.Font)

With this you can load a font from a .ttf file:

private static final Font SERIF_FONT = new Font("serif", Font.PLAIN, 24);

private static Font getFont(String name) {
    Font font = null;
    if (name == null) {
        return SERIF_FONT;
    }

    try {
        // load from a cache map, if exists
        if (fonts != null && (font = fonts.get(name)) != null) {
            return font;
        }
        String fName = Params.get().getFontPath() + name;
        File fontFile = new File(fName);
        font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
        GraphicsEnvironment ge = GraphicsEnvironment
                .getLocalGraphicsEnvironment();

        ge.registerFont(font);

        fonts.put(name, font);
    } catch (Exception ex) {
        log.info(name + " not loaded.  Using serif font.");
        font = SERIF_FONT;
    }
    return font;
}



回答3:


I have solved my own problem. I have done

URL fontUrl = new URL("file:///F:/Computer_Science/TexasLED.ttf");

That points to the font and works for me!




回答4:


You can use fonts embedded in your application jar file too. I have used this function for many years to load fonts in my projects.

public Font getFont(String fileName) throws Exception {
    String path = "/xyz/isururanawaka/wb/fonts/" + fileName;
    URL url = getClass().getResource(path);
    return Font.createFont(Font.TRUETYPE_FONT, new File(url.toURI()));
}


来源:https://stackoverflow.com/questions/8364787/how-do-you-import-a-font

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!