Font.createFont(..) set color and size (java.awt.Font)

18

I'd like to create a new Font object using a TTF file. It is really simple to create a Font object but I don't know how to set color and size because I can't find a method for it?

InputStream is = new FileInputStream("helvetica.ttf");
Font helvetica = Font.createFont(Font.TRUETYPE_FONT, is);

This question is tagged with java fonts awt

~ Asked on 2013-05-26 17:05:53

The Best Answer is


24

Font's don't have a color; only when using the font you can set the color of the component. For example, when using a JTextArea:

JTextArea txt = new JTextArea();
Font font = new Font("Verdana", Font.BOLD, 12);
txt.setFont(font);
txt.setForeground(Color.BLUE);

According to this link, the createFont() method creates a new Font object with a point size of 1 and style PLAIN. So, if you want to increase the size of the Font, you need to do this:

 Font font = Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf"));
 return font.deriveFont(12f);

~ Answered on 2013-05-26 17:06:53


5

Well, once you have your font, you can invoke deriveFont. For example,

helvetica = helvetica.deriveFont(Font.BOLD, 12f);

Changes the font's style to bold and its size to 12 points.

~ Answered on 2013-05-26 17:11:29


Most Viewed Questions: