/* * TestFontMetrics.java * * Created on June 6, 2003, 11:44 AM */ import java.awt.*; /** Test FontMetrics * * @author scott */ public class TestFontMetrics extends Component { private int prevWidth=0, prevHeight=0; String text; static public final String VALUE_FONT_NAME = "luxi sans"; // a scalable font for rendering field values Font font; public TestFontMetrics (String text,int pointSize) { this.text = text; font = new Font (VALUE_FONT_NAME,Font.PLAIN,pointSize); } public void paint ( Graphics g ) { if( getWidth () != prevWidth || getHeight () != prevHeight ) { prevWidth = getWidth(); prevHeight = getHeight(); } g.setColor (Color.white); g.fillRect (0,0,getWidth (),getHeight ()); g.setColor (Color.black); g.setFont (font); FontMetrics fm = g.getFontMetrics (); int x = 20; int y = getHeight()/2; g.setColor (Color.orange); g.drawLine(0,y,getWidth(),y); int asc = y - fm.getAscent (); int des = y + fm.getDescent (); g.setColor (Color.blue); g.drawLine(0,asc,getWidth(),asc); g.drawLine(0,des,getWidth(),des); int aMax = y - fm.getMaxAscent (); int dMax = y + fm.getMaxDescent (); g.setColor (Color.yellow); g.drawLine(0,aMax,getWidth(),aMax); g.drawLine(0,dMax,getWidth(),dMax); g.setColor (Color.black); g.drawString(text,x,y); System.out.println("Font metrics:" + "\n getAscent:" + fm.getAscent () + "\n getDescent:" + fm.getDescent () + "\n getMaxAdvance:" + fm.getMaxAdvance () + "\n getMaxAscent:" + fm.getMaxAscent () + "\n getMaxDescent:" + fm.getMaxDescent () + "\n getHeight:" + fm.getHeight () + "\n getLeading:" + fm.getLeading () + "\n getFont:" + fm.getFont ().toString () ); } public static void main ( String[] args ) { java.awt.Frame f = new java.awt.Frame (); f.setTitle ("TestFontMetrics: " + System.getProperties ().getProperty ("java.vm.vendor") + " " + System.getProperties ().getProperty ("java.vm.version")); f.addWindowListener (new java.awt.event.WindowAdapter () { public void windowClosing (java.awt.event.WindowEvent evt) { System.exit (0); } }); int pointsize = 50; if (args.length > 0) { try { pointsize = Integer.parseInt (args[0]); } catch (Throwable t) { } } f.add ( new TestFontMetrics ("A b i y | O M 123",pointsize) ); f.pack (); f.setVisible (true); f.repaint(); } public Dimension getPreferredSize () { final Dimension dim = new Dimension (200,200); return dim; } public Dimension getMinimumSize () { final Dimension dim = new Dimension (20,20); return dim; } }