// -------------------------------------------------- // FONTINFO : INFORMATION SUR LES POLICES DE CARACT. // -------------------------------------------------- import java.applet.*; import java.awt.*; public class FontInfo extends Applet { String s; Font fnt1, fnt2, fnt3; FontMetrics fm1, fm2, fm3; public void init() { // Définir la couleur de fond de l'applet setBackground(Color.black); // Initialiser la chaîne s = "Java"; // Créer 2 polices de caractères fnt1 = new Font("Courier", Font.BOLD, 24); fnt2 = new Font("Dialog", Font.PLAIN, 12); fnt3 = new Font("Dialog", Font.BOLD, 12); // Créer 2 objets FontMetrics dérivés des 2 polices ci-dessus fm1 = getFontMetrics(fnt1); fm2 = getFontMetrics(fnt2); fm3 = getFontMetrics(fnt3); } // Affichage public void paint(Graphics g) { // Ecrire en blanc g.setColor(Color.white); // Ecrire le mot Java, dans la police numéro 1 g.setFont(fnt1); g.drawString(s, 10, 30); // La même chose, en police numéro 2 g.setFont(fnt2); g.drawString(s, 10, 60); // Idem, en police numéro 3 g.setFont(fnt3); g.drawString(s, 10, 90); // Afficher les largeurs des 3 chaînes g.setColor(Color.orange); g.setFont(fnt2); g.drawString("Taille en police #1 : "+fm1.stringWidth(s), 100, 30); g.drawString("Taille en police #2 : "+fm2.stringWidth(s), 100, 60); g.drawString("Taille en police #3 : "+fm3.stringWidth(s), 100, 90); } }