My installation of OS X 10.8 comes pre-installed with 11 fonts in the family Helvetica Neue. I'm trying to find a way to access the fonts with styles like medium or condensed, which cannot be represented by the bit-mask values Font.BOLD
and Font.ITALIC
.
GraphicsEnvironment.getAllFonts()
returns Font
objects for all these fonts but applying them using JLabel.setFont()
seems to only use the styles representable with the mentioned bit-mask. This is shown on the left in the screenshot below, which compares it to a sample of all fonts when they are used in TextEdit.
The same happens if a Font
object is constructed using the font's full name or its PostScript name.
Is there a way to use all those fonts, either by applying it to a Swing component or when painting to a Graphics2D
(or Graphics
) instance?
Below is the code I used to produce the dialog in the above screenshot.
package fahrplan;
import java.awt.*;
import javax.swing.*;
public class FontsMain {
public static void main(String[] a) {
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
for (Font i : e.getAllFonts()) {
String name = i.getFontName();
if (name.startsWith("HelveticaNeue")) {
JLabel label = new JLabel(name);
label.setFont(i.deriveFont(18f));
contentPane.add(label);
}
}
JFrame frame = new JFrame("Fonts");
frame.setContentPane(contentPane);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}