package com.macmillan.nmeyers; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; /* FontPerf - Demonstrate the performance of font rendering under JDK1.1 and JDK1.2, with and without double-buffering. Copyright (c) 1999 Nathan Meyers $Id: FontPerf.java,v 1.5 1999/11/09 20:18:47 nathanm Exp $ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ class FontPerf extends JFrame { int nReps; int countdown = 0; Date starttime; FontPerf(String fontname, int nCols, int nRows, int nr, boolean doublebuffer) { super("Font Rendering Performance Test"); nReps = nr; if (!doublebuffer) { // Turn off double-buffering so we can watch rendering getRootPane().setDoubleBuffered(false); ((JComponent)getContentPane()).setDoubleBuffered(false); } // Get all available font families for JDK1.2 try { GraphicsEnvironment.getLocalGraphicsEnvironment(). getAvailableFontFamilyNames(); } catch (NoClassDefFoundError e) {} JTextArea textarea = new JTextArea(nRows, nCols); textarea.setFont(Font.decode(fontname)); getContentPane().add(textarea, BorderLayout.CENTER); JButton start = new JButton("Run Test"); getContentPane().add(start, BorderLayout.SOUTH); String testString = ""; while (testString.length() < nCols) testString += "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; testString = testString.substring(0, nCols); for (int row = 0; row < nRows; row++) textarea.append(testString + "\n"); start.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { countdown = nReps; starttime = new Date(); repaint(); } }); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { System.exit(0); } }); } public void paint(Graphics g) { super.paint(g); if (countdown > 0) { if (--countdown > 0) repaint(); else { System.out.println("Time for " + nReps + " repaints: " + (new Date().getTime() - starttime.getTime()) + " ms"); System.out.flush(); } } } private static void usage() { System.err.println("Usage: FontPerf [-nodoublebuffer] fontname" + " columns rows repetitions"); System.exit(1); } public static void main(String[] argv) { boolean doublebuffer = true; int startArray = 0; if (argv.length > 0 && argv[0].equals("-nodoublebuffer")) { doublebuffer = false; startArray++; } if (argv.length - startArray != 4) { usage(); } int nCols = Integer.parseInt(argv[startArray + 1]); int nRows = Integer.parseInt(argv[startArray + 2]); int nReps = Integer.parseInt(argv[startArray + 3]); FontPerf fontperf = new FontPerf(argv[startArray], nCols, nRows, nReps, doublebuffer); fontperf.pack(); fontperf.setVisible(true); } }