package com.macmillan.nmeyers; import javax.swing.*; import java.awt.*; import java.awt.event.*; /* * ShowFonts12: Display available fonts, using JDK1.2 graphics * capabilities. * * Usage: ShowFonts12 [ [...]] * * Author: Nathan Meyers, nmeyers@javalinux.net * $Id: ShowFonts12.java,v 1.6 1999/11/07 23:51:09 nathanm Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * with this program. If not, the license is available from the * GNU project, at http://www.gnu.org. */ public class ShowFonts12 extends JApplet { public ShowFonts12(String[] argv) { if (argv != null && argv.length == 0) argv = null; initialize(argv); } public ShowFonts12() { initialize(null); } private static class FontLabel extends JLabel { FontLabel(String fontname) { super(fontname); setFont(Font.decode(fontname)); setForeground(Color.black); } public void paintComponent(Graphics g) { ((Graphics2D)g).addRenderingHints(new RenderingHints( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)); ((Graphics2D)g).addRenderingHints(new RenderingHints( RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON)); super.paintComponent(g); } } public void initialize(String[] families) { // Build a scrolled pane containing a collection of labels // showing all of the available fonts Box box = Box.createVerticalBox(); // Build a panel with a 2-wide gridlayout to hold our styled labels JPanel panel = new JPanel(new GridLayout(0, 2)); panel.setBackground(Color.white); box.add(panel); getContentPane().add(new JScrollPane(box)); // Get our list of font families. Even if user has specified families // to display, this step seems necessary to make them available. String[] availableFamilies = GraphicsEnvironment.getLocalGraphicsEnvironment(). getAvailableFontFamilyNames(); if (families == null) families = availableFamilies; // Start creating and adding labels with 16-point fonts for (int i = 0; i < families.length; i++) { panel.add(new FontLabel(families[i] + "-plain-16")); panel.add(new FontLabel(families[i] + "-bold-16")); panel.add(new FontLabel(families[i] + "-italic-16")); panel.add(new FontLabel(families[i] + "-bolditalic-16")); } } public static void main(String[] argv) { JFrame frame = new JFrame(); ShowFonts12 api = new ShowFonts12(argv); frame.getContentPane().add(api); frame.pack(); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { System.exit(0); } }); } }