你如何导入字体? [英] How do you import a font?

查看:19
本文介绍了你如何导入字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道您将如何导入字体.

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,

如果重要的话,我电脑上的字体名称是 Terminal

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

我假设它必须在 java 文件的目录中有字体,并且会有一些使用它的方法,但我不确定如何使用.或者有更好的方法吗?

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

编辑 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);

错误信息

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

这是我正在尝试的:

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

编辑 4:

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

错误

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

推荐答案

'Airacobra Condensed' 字体可从 下载免费字体.

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

这篇关于你如何导入字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆