1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create(); // on crée un nouveau contexte
String s = "TEXTE EN BIAIS";
Rectangle2D stringBounds = g2d.getFontMetrics().getStringBounds(s, g);
g2d.rotate(Math.PI/4, 100 + stringBounds.getCenterX(), 100 + stringBounds.getCenterY()); // la rotation ne s'applique qu'à g2d, pas à g
g2d.setColor(Color.BLUE);
g2d.drawString(s, 100, 100);
g2d.dispose(); // on libère le contexte graphique qu'on a créé
g.setColor(Color.RED);
g.drawString(s, 100, 100);
g.drawOval(100, 100-(int)g2d.getFontMetrics().getLineMetrics(s, g).getAscent(), (int)stringBounds.getWidth(), (int)stringBounds.getHeight());
} |
Partager