This site requires JavaScript, please enable it in your browser!
Greenfoot back
crissty21
crissty21 wrote ...

2022/6/21

Import Font from .ttf file

crissty21 crissty21

2022/6/21

#
I haven't seen another solution to this problem, so I'll leave mine here. I've spent far too much time finding it, so I hope it will help some of you!
File f = new File("file.ttf");
        try {
            FileInputStream in = new FileInputStream(f);
            Font dynamicFont, dynamicFont32;

            dynamicFont = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(f));
            dynamicFont32 = dynamicFont.deriveFont(32f);

            java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(dynamicFont32);
            greenfoot.Font BurstFont = new greenfoot.Font(dynamicFont32.getName(), dynamicFont32.getStyle() % 2 == 1, dynamicFont32.getStyle() / 2 == 1, dynamicFont32.getSize());
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (FontFormatException e) {
            e.printStackTrace();
        }
BurstFont is the font you are going to use. the import lines are:
import greenfoot.*;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
RcCookie RcCookie

2022/6/21

#
What error do you get?
crissty21 crissty21

2022/6/22

#
RcCookie wrote...
What error do you get?
I don't get an error. I just wanted to share it, so others won't spend a life time trying to figure it out. (as I did)
danpost danpost

2022/6/22

#
crissty21 wrote...
RcCookie wrote...
What error do you get?
I don't get an error. I just wanted to share it, so others won't spend a life time trying to figure it out. (as I did)
Just note that this will only work locally. It will not work when uploaded to the site as you cannot use the java.awt package here (the legacy version, which require a plug-in, may still work, however).
You need to login to post a reply.