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);
Logical Fonts Java defines five logical font families that are Serif, SansSerif, Monospaced, Dialog, and DialogInput. It must be supported by the JRE. Note that JRE maps the logical font names to physical font because these are not the actual font libraries.
You can't actually change the size of an existing Font object. The best way to achieve a similar effect is to use the deriveFont(size) method to create a new almost identical Font that is a different size.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With