简体   繁体   English

如何导入字体?

[英]How do you import a font?

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, 这是图片,

在此处输入图片说明

In case it matters the font name on my computer is Terminal 万一我的电脑上的字体名称是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. 编辑:我假设它将必须在java文件的目录中具有该字体,并且会有某种使用该字体的方法,但是我不确定如何使用。 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 EDIT2:我已经找到了如何做到这一点,但是要我如何去使用这一些帮助一个很好的教程... 点击我要链接

Edit3: 编辑3:

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: 编辑4:

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)

'Airacobra Condensed' font available from Download Free Fonts . “ Airacobra Condensed”字体可从“ 下载免费字体”中获得

注册字体

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);
    }
}

You can use GraphicsEnvironment.registerFont 您可以使用GraphicsEnvironment.registerFont

http://docs.oracle.com/javase/6/docs/api/java/awt/GraphicsEnvironment.html#registerFont(java.awt.Font ) 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: 这样,您可以从.ttf文件加载字体:

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;
}

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! 指向字体并为我工作!

You can use fonts embedded in your application jar file too. 您也可以使用应用程序jar文件中嵌入的字体。 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()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM