1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
import javax.swing.*;
import java.awt.*;
public class image extends JPanel {
String mot;
Font font;
FontMetrics fm;
public image(String mot){
this.mot = mot;
font = new Font(null, Font.PLAIN, 12);
fm = getFontMetrics(font);
System.out.println("Largeur : " + fm.stringWidth(mot));
System.out.println("Hauteur entre 2 lignes : " + fm.getHeight());
System.out.println("Jambage ascendant : " + fm.getAscent());
System.out.println("Jambage descendant : " + fm.getDescent());
System.out.println("Interligne : " + fm.getLeading());
setPreferredSize(new Dimension(fm.stringWidth(mot) + 1,fm.getHeight()));
}
public void paintComponent(Graphics g){
Graphics2D gg = (Graphics2D) g.create();
gg.setBackground(Color.red);
gg.clearRect(0,0,getWidth(),getHeight());
gg.setFont(font);
gg.drawString(mot, 0, 12);//12 = jambage ascendant
}
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setSize(500,500);
jf.setLayout(new FlowLayout());
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
image im = new image("coucoucou");
jf.add(im);
jf.setVisible(true);
}
} |
Partager