Locating the Fonts Available to Your Java Apps

One way to find which fonts are available to your Java programs is to call the getFontList method of the java.awt.Toolkit class. It returns an array of font names. Although just a few are given, these names can be used to create a font instance:

Toolkit toolkit = getToolkit();String[] fontNames = toolkit.getFontList();

The problem with this method is that the getFontList method has been deprecated. As of Java 1.2, the recomended way to find the fonts available to a JVM is to use java.awt.GraphicsEnvironment’s getAvailableFontFamilyNames method. It generally provides a longer list of names.

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();String[] fontNames = ge.getAvailableFontFamilyNames();

The following class can be used to preview the fonts. Its constructor takes a String array of font names. Once the object has been created, simply add it to a java.awt.Container or a sub-class of thereof:

import javax.swing.JPanel;import java.awt.Graphics;import java.awt.Font;import java.awt.Color;class FontPanel extends JPanel {	String [] fontNames;	public FontPanel(String [] fonts) {		fontNames = fonts; 	}	public void paint(Graphics g) {		g.setColor(Color.black);		int y = 20;		for (int f = 0; f < fontNames.length; f++) {			g.setFont(new Font(fontNames[f],Font.PLAIN,16) );			g.drawString(fontNames[f] + ": The quick brown fox jumps over the lazy dog.", 10, y);			y += 20;		}	}}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a