Name: dbT83986 Date: 10/10/99
The following program draws a string "& W w h q e" to a graphic object with the font Wingding.ttf (this one is installed on all Win platforms). This should result in glyphs like a book, a cross and waves - but only rectangles are displayed.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FontTest extends JPanel{
public FontTest() {
GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
String envfonts[] = gEnv.getAvailableFontFamilyNames();
setFont(new Font("Wingdings",Font.PLAIN, 48));
}
public void paintComponent (Graphics g) {
super.paintComponent( g );
Graphics2D g2 = (Graphics2D) g;
int w = getWidth();
int h = getHeight();
String text = "& W w h q e";
FontMetrics metrics = g2.getFontMetrics();
int width = metrics.stringWidth( text );
int height = metrics.getHeight();
g2.drawString( text, w/2-width/2, h/2-height/2 );
}
public static void main(String s[]) {
JFrame f = new JFrame("FontTest");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
f.getContentPane().add(new FontTest(), BorderLayout.CENTER);
f.setSize(new Dimension(550,250));
f.setVisible(true);
}
}
(Review ID: 96318)
======================================================================