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

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

问题描述

我想知道你将如何去导入的字体。

我试图使用自定义下载的字体,但因为这会去跑这不会有这种字体,因为它不是一个默认字体大多数计算机。我怎么会去使字体的工作,即使他们没有字体?

我使用它要GAMEOVER屏幕和需要显示与它的分数,并希望得分文本是相同的字体。这是图像,

在的情况下它的事项在我的电脑上的字体名称是端子

编辑:我假设它必须在java文件的目录中的字体,会有使用的一些方法,但我不知道怎么样。还是有更好的办法?

EDIT2:我已经找到了如何做到这一点,但是要我如何去使用这个...的点击我要链接

EDIT3:

  URL fontUrl =新的URL(http://www.webpagepublicity.com/+自由字体/ A / Airacobra%20Condensed.ttf);
字体的字体= Font.createFont(Font.TRUETYPE_FONT,fontUrl.openStream());
GraphicsEnvironment中GE = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(字体);
g.setFont(字体);

错误消息

 文件:F:\\计算机科学\\ draw.java [行:252]
错误:F:\\计算机科学\\ draw.java:252:字体不是公众java.awt.Component中;不能从外面包访问

下面是我尝试:

  URL fontUrl =新的URL(http://img.dafont.com/dl/?f=badaboom_bb);
字体的字体= Font.createFont(Font.TRUETYPE_FONT,fontUrl.openStream());
GraphicsEnvironment中GE = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(字体);
g.setFont(字体);

Edit4:

 文件fontfile =新的文件(TexasLED.ttf);
File.toURI(fontfile).toURL(fontfile);
网址fontUrl =新的URL(fontfile);

错误

 错误:F:\\计算机科学\\ draw.java:250:toURI()在java.io.File中不能适用于(java.io.File中)


解决方案

Airacobra简明的字体可从下载免费字体

 进口java.awt中的*。
进口的javax.swing *。
进口的java.net.URL;类LoadFont {
    公共静态无效的主要(字串[] args)抛出异常{
        //这个字体为< 35KB。
        网址fontUrl =新的URL(http://www.webpagepublicity.com/+
            自由字体/ A / Airacobra%20Condensed.ttf);
        字体的字体= Font.createFont(Font.TRUETYPE_FONT,fontUrl.openStream());
        GraphicsEnvironment中GE =
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(字体);
        JList的字体=新的JList(ge.getAvailableFontFamilyNames());
        JOptionPane.showMessageDialog(空,新JScrollPane的(字体));
    }
}


OK,这很有趣,但是这是什么字体实际上是什么样子?

 进口java.awt中的*。
进口的javax.swing *。
进口的java.net.URL;类DisplayFont {
    公共静态无效的主要(字串[] args)抛出异常{
        网址fontUrl =新的URL(http://www.webpagepublicity.com/+
            自由字体/ A / Airacobra%20Condensed.ttf);
        字体的字体= Font.createFont(Font.TRUETYPE_FONT,fontUrl.openStream());
        字体= font.deriveFont(Font.PLAIN,20);
        GraphicsEnvironment中GE =
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(字体);        JLabel的L =新的JLabel(
            敏捷的棕色狐狸跳过懒狗0123456789);
        l.setFont(字体);
        JOptionPane.showMessageDialog(NULL,L);
    }
}

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

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)

解决方案

'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天全站免登陆