In the new version of the library, so material-ui-swing-1.1.1 (0.3) is possible load a personal font with the library directly in the clien application such as JMars 5.
The building the font with material library is very fast because the instance of the font is the same, the class MaterialFontsFactory is use the facory pattern and store the font inside a map and the class doesn't build the same font two times.
The previus version of the library were able only to load the Noto-Sans font (font library) but it didn't able to load a font from InputStream or an relative path.
With the new API the class have tre different method, like:
public FontUIResource getFont(MaterialTypeFont typeFont)public FontUIResource getFontWithPath(String path)public FontUIResource getFontWithStream(InputStream stream)
P.S: Remember that the class MaterialFontesFactory is an Singleton, so you should get the instance
of the class with this code: MaterialFontFactory.getInstance()
JMars is using the following code to load the personal font
public static Font getFont(String fileName) {
try {
Font customFont = Font
.createFont(Font.PLAIN, Main.getResourceAsStream(PATH_TO_MATERIAL_FONTS + fileName))
.deriveFont(FONTS.ROBOTO.fontSize());
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(customFont);
return customFont;
} catch (IOException e) {
return null;
} catch (FontFormatException e) {
return null;
}
}
This code is an basic code and should be include different bug with different monitor.
material-ui-library include different bug fixeng to the personal fonts, like
You can look the font pixeled in the following image
Pixeled font result.
Pixeled font fixed.
To import the change easy inside JMars you can change the getFont mothod with this code:
public static Font getFont(String fileName) {
Font customFont = MaterialFontFactory.getInstance().getFontWithStream(Main.getResourceAsStream(PATH_TO_MATERIAL_FONTS + fileName));
return customFont;
}
If the Input stream is wrong the library throws 2 different RuntimeException, like:
- IllegalArgumentException: if the stream is null
- RuntimeException: if there is error in the build the font, an example: If the input stream contains an image and not an font.
P.S: The Material library build Only one instance each type font and the dimension of font is calculate with the following method.
float size = defaultSize * Math.min(Toolkit.getDefaultToolkit().getScreenResolution(), 96) /72;
Where the defaultSize is by default = 11. (FIXED dimension, at the moment you should be able to change the value)
If you use the Roboto font, you can have other problem with JDK unitll 13, this is described the bug: https://bugs.openjdk.java.net/browse/JDK-8236996
P.S: The bug was fixed in the 2020-01-27 13:06 and it will be release in the JDK 15, unfortunatly.
Vincent Palazzo: https://github.com/vincenzopalazzo
Document License
To support my work in this version you can use
or
This document is developer for Arizona State University.



