简体   繁体   English

一个java项目中下载的字体如何在不同的PC上使用?

[英]How can I use a downloaded font in a java Project on different PCs?

I downloaded the Font "Retro Gaming" ( https://www.dafont.com/retro-gaming.font ) to use it on my Java project.我下载了“Retro Gaming”字体 ( https://www.dafont.com/retro-gaming.font ) 以在我的 Java 项目中使用它。 This project needs to run on every machine I run it.这个项目需要在我运行它的每台机器上运行。 My question is: How or where can I put the font so that all computers (even without having it installed) can use it?我的问题是:如何或在哪里放置字体以便所有计算机(即使没有安装它)都可以使用它? I thought gradle could be a nice solution but I found nothing about it on the inte.net.我认为 gradle 可能是一个不错的解决方案,但我在 inte.net 上找不到任何相关信息。

The example of code is:代码示例如下:

public PlayerOneLabel() {
        this.setText("PLAYER 1");
        this.setForeground(Color.white);
        this.setFont(new Font("Retro Gaming", Font.BOLD, CenterOnDefaultScreen.center().height*2/100));
        }

This obviously run only on the PCs with the font already installed.这显然只能在已安装字体的 PC 上运行。

Save the font file wherever you like...place it with the rest of your game files or place it in a resource folder.将字体文件保存在您喜欢的任何位置...将其与游戏文件的 rest 放在一起,或将其放在资源文件夹中。 It doesn't need to go somewhere special if it's just for your game.如果它只是为了你的游戏,它不需要 go 特殊的地方。 You can load the font when you need it by using the following method:您可以使用以下方法在需要时加载字体:

/**
 * Loads a Font file and returns it as a Font Object. This method does try to 
 * utilize the proper Font Type Resource (TrueType or Type1) but will default 
 * to TrueType if the resource type can not be established.
 * 
 * @param fontFilePath (String) Full path and file name of the font to load.<br>
 * 
 * @param fontStyle (Integer) The Font Style to use for the loaded font, for 
 * example:<pre>
 * 
 *      Font.PLAIN
 *      Font.BOLD
 *      Font.ITALIC
 *      Font.BOLDITALIC</pre>
 * 
 * @param fontSize (Integer) The desired size of font.<br>
 * 
 * @return (Font) A Font Object
 */
public Font loadFont(String fontFilePath, int fontStyle, int fontSize) {
    Font font = null;
    int fontTypeResource = Font.TRUETYPE_FONT;
    if ((fontFilePath == null || fontFilePath.isEmpty()) || fontSize < 1) {
        throw new IllegalArgumentException("loadFont() Method Error! Arguments "
                + "passed to this method must contain a file path OR a numerical "
                + "value other than 0!" + System.lineSeparator());
    }
    String fileExt = (fontFilePath.contains(".") ? fontFilePath.substring(fontFilePath.lastIndexOf(".") + 1) : "");
    if (fontFilePath.isEmpty()) {
        throw new IllegalArgumentException("loadFont() Method Error! An illegal "
                + "font file has been passed to this method (no file name "
                + "extension)!" + System.lineSeparator());
    }
    
    switch (fileExt.toLowerCase()) {
        case "fot":
        case "t2":
        case "otf":
        case "ttf":
            fontTypeResource = Font.TRUETYPE_FONT;
            break;
        // PostScript/Adobe
        case "lwfn":
        case "pfa":
        case "pfb":
        case "pdm":
            fontTypeResource = Font.TYPE1_FONT;
            break;
        default:
            fontTypeResource = Font.TRUETYPE_FONT;
    }
    
    try {
        font = Font.createFont(fontTypeResource, new FileInputStream(
               new File(fontFilePath))).deriveFont(fontStyle, fontSize);
    }
    catch (FileNotFoundException ex) {
        Logger.getLogger("loadFont()").log(Level.SEVERE, null, ex);
    }
    catch (FontFormatException | IOException ex) {
        Logger.getLogger("loadFont()").log(Level.SEVERE, null, ex);
    }
    return font;
}

And how you might use it:以及如何使用它:

JLabel jLabel_1 = new JLabel("This is my Label Caption");

/* Assumes the .ttf file in within the application's project 
   directory. Use a getResource() mechanism when loading from
   a resource directory.                           */
Font gameFont = loadFont("Retro Gaming.ttf", Font.BOLD, 18);

jLabel_1.setFont(gameFont);

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

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