I\'m developing a web application which use JFreeChart to render chart. However, when server dose not have any Chinese font installed, JFreeChart dose not display Chinese ch
I know this is an old question, but I was searching for the answer myself and having seen the responses above, I still didn't really understand the purpose of registering a font. Having researched the issue, this is what I found:
You don't have to register your font with the graphics environment, but doing so comes with the advantage of being able to use the registered font in 'new Font()' constructors.
You can use the following code to get a list of all the fonts which are currently available (i.e installed and ready to use in your application):
String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
Assuming you're on Windows, and one of the installed fonts is Arial, you can use this font in your application like this:
JButton yesButton = new JButton ("Yes");
yesButton.setFont(new Font("Arial", Font.PLAIN,30));
Now Suppose you want to load in and use your own custom font from a file:
Font robotoFont = Font.createFont(Font.TRUETYPE_FONT,getClass().getResourceAsStream("/res/fonts/Roboto/Roboto-Light.ttf"));
If you wanted to set that as the font for a JButton, you might write this code:
JButton yesButton = new JButton("Yes");
yesButton.setFont(robotoFont.deriveFont(Font.PLAIN, 30f));
But if you tried to write some code like:
JButton yesButton = new JButton("Yes");
yesButton.setFont(new Font ("Roboto Light", Font.PLAIN,30));
The JButton would just be given a default font, because the graphics environment is not aware of any font named "Roboto Light". The solution to this is to register your font with the graphics environment :
GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment();
genv.registerFont(robotoFont);
You will then be able to use this font in 'new Font()' constructors like this:
JButton yesButton = new JButton("Yes");
bestButton.setFont(new Font ("Roboto Light", Font.PLAIN,30));