jack kennedy
jack kennedy

Reputation: 17

Java how to increase font size?

Hello I have this code which creates a font from a ttf file in my res folder.

         try {
            font1 = Font.createFont(Font.TRUETYPE_FONT, new File("res/1942.ttf"));
            font1.deriveFont(12f);
        } catch (FontFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

I thought .deriveFont(); increased the custom font size but for me it dosnt. What is wrong? Here is where is use the font.

        g.setColor(Color.blue);
        font1.deriveFont(52);
        g.setFont(font1);

        g.drawString("hello",480, 250);

Upvotes: 2

Views: 1851

Answers (2)

SkyWalker
SkyWalker

Reputation: 29168

Use this

g.setFont(new Font("Serif", Font.PLAIN, 14));

you can use another approach also

JButton btn = new JButton();
btn.setFont(btn.getFont().deriveFont(14.0f));

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285460

This font1.deriveFont(12f); doesn't change font1. Rather it returns a new Font of differing size. You need to something with this returned object, perhaps something like:

setFont(font1.deriveFont(12f));

or

font1 = font1.deriveFont(12f);

Upvotes: 1

Related Questions