
{filelink=9427}
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.FontMetrics;
public class ExempleFontMetric extends JPanel
{
public void paintComponent( Graphics g )
{
super.paintComponent( g );
g.setColor( Color.MAGENTA ); //Modifier la couleur du graphique
// Définir une police pour le graphique
g.setFont( new Font( "Serif", Font.BOLD + Font.ITALIC, 14 ) );
// Définir un objet Metric pour obtenir des infos sur la police
FontMetrics metrics = g.getFontMetrics();
g.drawString( "Police courante: " + g.getFont(), 10, 40 );
g.drawString( "ASCENT: " + metrics.getAscent(), 10, 55 );
g.drawString( "DESCENT: " + metrics.getDescent(), 10, 70 );
g.drawString( "HEIGHT: " + metrics.getHeight(), 10, 85 );
g.drawString( "LEADING: " + metrics.getLeading(), 10, 100 );
}
// Fonction principale
public static void main (String[] args)
{
JFrame frame=new JFrame("Police et la classe FontMetric") ;
JPanel policePanel=new ExempleFontMetric();
frame.add(policePanel);
frame.setSize(300,200);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}