import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JPanel;
public class MetricaJPanel extends JPanel
{
// muestra la métrica de los tipos de letra
public void paintComponent( Graphics g )
{
super.paintComponent( g ); // llama al método paintComponent de la superclase
g.setFont( new Font( "SansSerif", Font.BOLD, 12 ) );
FontMetrics metrica = g.getFontMetrics();
g.drawString( "Tipo de letra actual: " + g.getFont(), 10, 40 );
g.drawString( "Ascendente: " + metrica.getAscent(), 10, 55 );
g.drawString( "Descendente: " + metrica.getDescent(), 10, 70 );
g.drawString( "Altura: " + metrica.getHeight(), 10, 85 );
g.drawString( "Interlineado: " + metrica.getLeading(), 10, 100 );
Font tipoLetra = new Font( "Serif", Font.ITALIC, 14 );
metrica = g.getFontMetrics( tipoLetra );
g.setFont( tipoLetra );
g.drawString( "Tipo de letra actual: " + tipoLetra, 10, 130 );
g.drawString( "Ascendente: " + metrica.getAscent(), 10, 145 );
g.drawString( "Descendente: " + metrica.getDescent(), 10, 160 );
g.drawString( "Altura: " + metrica.getHeight(), 10, 175 );
g.drawString( "Interlineado: " + metrica.getLeading(), 10, 190 );
} // fin del método paintComponent
} // fin de la clase MetricaJPanel
--------------------------------------------------------------------------------------------------
import javax.swing.JFrame;
public class Metrica
{
// ejecuta la aplicación
public static void main( String args[] )
{
// crea marco para objeto MetricaJPanel
JFrame marco = new JFrame( "Demostracion de FontMetrics" );
marco.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
MetricaJPanel metricaJPanel = new MetricaJPanel();
marco.add( metricaJPanel ); // agrega metricaJPanel al marco
marco.setSize( 530, 250 ); // establece el tamaño del marco
marco.setVisible( true ); // muestra el marco
} // fin de main
} // fin de la clase Metrica

No hay comentarios:
Publicar un comentario