1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
public void paintComponent(Graphics g){
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
String message = "test de taille";
Font f = new Font("Arial Black", Font.ITALIC, 12);
g2d.setFont(f);
// mesure le message
FontRenderContext context = g2d.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(message, context);
// Affichage
g2d.drawString(message, 10 , (int) bounds.getHeight());
Rectangle2D rect = new Rectangle2D.Double(10 ,0, bounds.getWidth(),bounds.getHeight());
g2d.draw(rect);
g2d.drawString("Hauteur de la chaine : " + bounds.getHeight() + ", largeur : " + bounds.getWidth(), 10 , (int) (2 * bounds.getHeight()) );
} |
Partager