TrueTypeFont font = new TrueTypeFont(new java.awt.Font ("Verdana", Font.BOLD, 20), false);...graphics.setFont(font);graphics.drawString(...);ORfont.drawString(....);
The only downside is TrueTypeFont is deprecated. Technically it is still usable, but using deprecated classes or methods rubs me the wrong way. The API says to use the UnicodeFont class instead. After some research I also learned that the UnicodeFont class is more efficient.
I assumed that UnicodeFont would work as simply as TrueTypeFont, but I was mistaken. I modified my code to use UnicodeFont, but once I did that it stopped drawing the text. After some research I learned that I can't just create the font, I also have to create an Effect, add the glyphs and load the glyphs. But I just want to make the text a little bigger!</aggravated lament> Loading the glyphs gives this class an advantage by being able to only load the characters you need. However, this seems way to difficult for someone that just wants to make their text a little bigger. My two simple lines of code for creating a TrueTypeFont and setting that font became 5 lines of code that are a lot harder to read and understand:
UnicodeFont font = new UnicodeFont(new java.awt.Font ("Verdana", Font.BOLD, 20)); font.getEffects().add(new ColorEffect(java.awt.Color.white);font.addNeheGlyphs();font.loadGlyphs();graphics.setFont(font);
Maybe I'm just picky, but I don't want to stick this ugly block of code in every place that I just want to make my text a little bigger. So, I created a class that abstracted out all of the ColorEffect/Glyphs/Confusingness. Now when I want to make my font a little bigger I can just use:
SimpleFont font = new SimpleFont("Verdana", Font.BOLD, 20);graphics.setFont(font.get());
Plus I still get some of the efficiency benefits that UnicodeFont offers by only loading the core ascii characters. Hopefully my research will save others time in drawing their text just a little bigger. Here is the SimpleFont class I created:
import org.newdawn.slick.SlickException;import org.newdawn.slick.UnicodeFont;import org.newdawn.slick.font.effects.ColorEffect;import java.awt.Color;import java.awt.Font;public class SimpleFont {private UnicodeFont font;public SimpleFont(String fontName, int style, int size, Color color) throws SlickException {this(new Font(fontName, style, size), color);}public SimpleFont(String fontName, int style, int size) throws SlickException {this(new Font(fontName, style, size));}public SimpleFont(Font font) throws SlickException {this(font, Color.white);}public SimpleFont(Font font, Color color) throws SlickException {this.font = new UnicodeFont(font);ColorEffect colorEffect = new ColorEffect(color);this.font.getEffects().add(colorEffect);this.font.addNeheGlyphs();this.font.loadGlyphs();}public void setColor(Color color) throws SlickException {font.getEffects().clear();font.getEffects().add(new ColorEffect(color));font.clearGlyphs();font.addNeheGlyphs();font.loadGlyphs();}public UnicodeFont get() {return font;}}
3 comments:
Thank you! I was struggling with drawing text a different size, and got really frustrated with UnicodeFont. Thanks for the help! :)
Thanks! I was frustrated just trying to get my text bigger, I appreciate your SimpleFont class! :)
Hey Brandon!
Thanks for the tutorial.
How can I prevent the SimpleFont class to log to the output?
It logs:
loading glyphs -1
glyphs loaded
Thanks!
Post a Comment