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.
Asked
Active
Viewed 1.7k times
3 Answers
1
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!

bucmipid-153
- 328
- 1
- 10
0
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);

Community
- 1
- 1

MadProgrammer
- 343,457
- 22
- 230
- 366
-
I see. When you set the font, you put the fonts name. And in the font below, I spelled times new roman wrong, yet it doesn't show up as an error. Why? static Font f = new Font("Times New Roan",Font.BOLD,20); – Ski Apr 02 '15 at 21:40
-
Why should it? If I recall correctly, the API has a fall back process, so if the font doesn't exist on the system, it falls back to common font instead – MadProgrammer Apr 02 '15 at 21:41
-
How do we know if the font exist on the API or not, then? Is there a full list of the available fonts somewhere? And how many contructors exist for creating fonts? – Ski Apr 02 '15 at 21:43
-
Something like `String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();`. Have a look at [`GraphicsEnvironment.html#getAllFonts`](http://docs.oracle.com/javase/7/docs/api/java/awt/GraphicsEnvironment.html#getAllFonts()) and [`GraphicsEnvironment.html#getAvailableFontFamilyNames`](http://docs.oracle.com/javase/7/docs/api/java/awt/GraphicsEnvironment.html#getAvailableFontFamilyNames()) – MadProgrammer Apr 02 '15 at 21:48
-
-
`String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();` ... – MadProgrammer Apr 02 '15 at 22:09
-
You do realise, getting a list of the available fonts was really part of the original question you asked ;) – MadProgrammer Apr 02 '15 at 22:10
-
See update (the original comment had additional unicode characters in it, which were screwing things up) – MadProgrammer Apr 02 '15 at 22:13
0
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);
}

Ski
- 111
- 2
- 3
- 17