/* * @(#)AllFonts.java 1.18 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ package demos.Fonts; import java.awt.*; import java.awt.event.*; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.util.Vector; import J2DCanvas; /** * Scrolling text of fonts returned from GraphicsEnvironment.getAllFonts(). */ public class AllFonts extends J2DCanvas { private Vector fonts = new Vector(); private Vector v = new Vector(); private int nStrs; private int strH; private int fi; public AllFonts() { setBackground(Color.white); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Font allfonts[] = ge.getAllFonts(); for (int i = 0; i < allfonts.length; i++) { if (allfonts[i].canDisplayUpTo(allfonts[i].getName()) > 0) { fonts.addElement(allfonts[i]); } } sleepAmount = 200; runnable = true; } public void drawDemo(Graphics2D g2) { if (fi < fonts.size()) { Font f = new Font(((Font)fonts.get(fi)).getName(),Font.PLAIN,14); v.addElement(new TextLayout(f.getName(),f,g2.getFontRenderContext())); } if (newBufferedImage) { TextLayout tl = (TextLayout) v.firstElement(); strH = (int) (tl.getAscent()+tl.getDescent()); nStrs = h/strH + 1; fi = 0; } g2.setColor(Color.black); if (v.size() == nStrs && v.size() != 0 || fi > fonts.size()) { v.removeElementAt(0); } int yy = (fi >= fonts.size()) ? 0 : h - v.size() * strH - strH/2; for (int i = 0; i < v.size(); i++) { int sw = (int) ((TextLayout)v.get(i)).getBounds().getWidth(); ((TextLayout) v.get(i)).draw(g2, (int) (w/2-sw/2),yy += strH); } fi = (v.size() == 0) ? 0 : ++fi; } public static void main(String argv[]) { final AllFonts demo = new AllFonts(); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowDeiconified(WindowEvent e) { demo.start(); } public void windowIconified(WindowEvent e) { demo.stop(); } }; Frame f = new Frame("Java2D Demo - AllFonts"); f.addWindowListener(l); f.add("Center", demo); f.pack(); f.setSize(new Dimension(400,200)); f.show(); demo.start(); } }