Using AWT graphics, I am attempting to draw text with a .ttf font. But when drawn on the screen it only displays text in what looks to be font size 1.
Font initialization code:
font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("font.ttf"));
font.deriveFont(24F);
fontSmall = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("font.ttf"));
fontSmall.deriveFont(16F);
And yes, those statements are inside of a try statement.
And the code that is supposed to correctly draw text with the font:
g.setFont(font);
g.setColor(RED);
g.drawString("Test123",10,10);
g.setFont(fontSmall);
g.drawString("Test123SMALL",10,10);
Thanks in advance :)
Try with:
font = font.deriveFont(24F);
And:
fontSmall = fontSmall.deriveFont(16F);
font = font.deriveFont(24F);
This should work, but when you try:
font = font.deriveFont(24);
The font looks to be size 1.
Related
I made a new TrueType Font, and I want to use it to draw a String on my JPanel. This does not seem to work:
Font f = Font.createFont(Font.TRUETYPE_FONT, MyClass.class.getClassLoader().getResourceAsStream("/res/myFont.ttf"));
f = f.deriveFont(Font.PLAIN, 20);
g2d.setFont(f);
g2d.drawString(x, y, "my string");
What should I do?
Is there a way to set fonts on a string, and then draw the string using graphics? I know you can do it on Jlabels, and Jtextfields, and J-other components, but is there a way to do it just on the string instead? Thank you.
You can do it with attributed strings an example of is
Font font = new Font("LucidaSans", Font.PLAIN, 14);
AttributedString atString= new AttributedString("Example text string");
atString.addAttribute(TextAttribute.FONT, font);
graphic.drawString(atString.getIterator(),x,y);
Cheers!
We can actually set the font in graphics...and then draw the string.
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.setFont(Fonts.f);
g.drawString(Fonts.text, 50, 50);
}
Yes, it's possible, start by checking out the Graphics, Text tutorials
Basically, you can set the font using Graphics#setFont and draw a String using Graphics#drawString.
Also see Performing Custom Painting for how you can perform custom painting in Swing
For example
List available font names....
String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for (String font : fonts) {
System.out.println(font);
}
or
Font fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (Font font : fonts) {
System.out.println(font);
}
You can manipulate a font's properties using one of the Font#derive methods...
Font font = ...;
Font bigger = font.derive(32f);
Font bolder = font.derive(Font.BOLD);
Font biggerAndBolder = font.derive(Font.BOLD, 32f);
I know that you can import a Font in Java with something like this:
File file = new File(fontPath);
Font font = Font.createFont(Font.TRUETYPE_FONT, file);
// alternative:
// Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(file));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
Then you would use for example font.deriveFont(Font.PLAIN, 20); to get the desired style and size.
Example
But now let's look as an example at the font Consolas, there you have four TTF files:
consola.ttf (Plain)
consolab.ttf (Bold)
consolai.ttf (Italic)
consolaz.ttf (Bold & Italic)
Of course you could just import consola.ttf with the method stated above, but then using font.deriveFont(Font.BOLD, 20); isn't the same as using consolab.ttf because the plain font was simply transformed to look like a bold font.
Example Pictures
Here I used the installed font with new Font("Consolas", Font.PLAIN, 20); and new Font("Consolas", Font.BOLD, 20); (as a side note, if the font is installed on the system you also get the right bold font if you use deriveFont(Font.BOLD);):
And this is consola.ttf, imported with createFont and derived bold font (both with size 20 like the example above):
Well when installed it isn't a problem, but I don't expect others to have a custom Font, so I want to put the TTFs into the jar file, so that I can import them during the initialization via getResourceAsStream(path).
Is there a way to import all relevant TTFs and then just call new Font("Custom Font Name", fontStyle, fontSize); so that it's used like an installed font (Picture 1), and that it doesn't looks like a derived 'fake' bold font (Picture 2)?
I'm not sure what exactly is the problem. You got all your TTF files and you have to import and register them. Following tests use DejaVu Sans fonts which are not installed on my system.
Test 1
Font f = Font.createFont(Font.TRUETYPE_FONT, new File("dvs.ttf"));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(f);
Font dvs = new Font("DejaVu Sans", Font.PLAIN, 20);
Here's an image with plain (dvs) and derived bold (dvs.deriveFont(Font.BOLD)) font.
Test 2
Font f = Font.createFont(Font.TRUETYPE_FONT, new File("dvs.ttf"));
Font fb = Font.createFont(Font.TRUETYPE_FONT, new File("dvsb.ttf"));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(f);
ge.registerFont(fb);
Font dvs = new Font("DejaVu Sans", Font.PLAIN, 20);
Font dvsb = new Font("DejaVu Sans", Font.BOLD, 20);
And here's an image with plain (dvs) and truly bold (dvsb) font.
You can confirm that correct file is used by looking at font2DHandle.
I also tested italic and bold italic and both worked as well as Font#createFont(int, InputStream) method.
Above approach works because fonts are mapped by their full name (e.g. Arial, Arial Bold etc.), so as long as your fonts are correctly named you can register multiple members of one family.
Usually, when I initialize the fonts I want to use in my SWING applications, I do it this way:
public static final Font TITLEFONT = new Font("Calibri", Font.BOLD, 40);
Now, I have to do it a bit differently since I'm using some custom fonts from a .ttf file. I initialize the font this way:
try
{
InputStream is = OptionsValues.class.getResourceAsStream("fonts//KOMIKAX_.ttf");
TITLEFONT = Font.createFont(Font.TRUETYPE_FONT, is);
}
catch (Exception ex)
{
ex.printStackTrace();
System.err.println("Font not loaded. Using Calibri font.");
TITLEFONT = new Font("Calibri", Font.BOLD, 40);
}
I'm pretty sure it initializes it correctly (I can't tell for sure since it is too small for me to see), but I'd like to know how I can manually set the font's size (and if it's bold / other attributes) when loading a font this way.
Thanks a lot in advance!
createFont returns a Font and you can call deriveFont(...) on this, passing in a float for the point size, or an int and float for Font style and point size. I cannot say whether it will work for your particular situation, but it's worth a try.
e.g.,
InputStream is = OptionsValues.class.getResourceAsStream("fonts//KOMIKAX_.ttf");
TITLEFONT = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(Font.BOLD, 40f);
I'd simply use:
Font.ITALIC
Font.BOLD
Font.PLAIN
Is there any way to get the system default font name in Java? The default font can differ from os. So it can create trouble if we use font Arial and the jar is running in Linux without having Arial font installed.
Try this:
private final Font FONT = new JLabel().getFont();
JavaFX makes this a lot easier:
import javafx.scene.text.Font;
then use:
Font defaultFont = Font.getDefault();
or
// Where 14 is the font size
Font defaultFont = new Font(14);
Use the defined Font constants such as SERIF/SANS_SERIF etc.
I am currently using this to get the default font, although I would rather not need to use a graphics object to get it:
private final Font getFont()
{
Graphics g = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB).getGraphics();
Font font = new Font(g.getFont().toString(), 0, 12);
g.dispose();
return font;
}
I don't think there is a way of retrieving a system default font(in Swing/AWT the font is normally associated with the current LAF and component, for instance), but if your concern is font compatibility - you could check the font you are using against all the system fonts:
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] allFonts = e.getAllFonts();
and make a "fail-over" choice if it doesn't exist.
Take a look at public static Font decode(String str) here. When the decode method receives a null pointer as a parameter it returns the "Dialog" font which is usually the system font.
getFont() returns the current font, which is (usually?) the default.
I did this to increase font size.
public MyTextArea(){
Font currentFont = super.getFont();
String fontName = currentFont.getFontName();
int fontStyle = currentFont.getStyle();
int fontSize = currentFont.getSize() + 4;
super.setFont(new Font(fontName, fontStyle, fontSize));
}
In Windows, Segoe UI
Visit http://www.apaddedcell.com/sites/www.apaddedcell.com/files/fonts-article/final/index.html to see a list of preinstalled fonts.
I chose Verdana