As shown in the following picture, an AttributedString is drawn on a JPanel (500X500).
The FontMetrics.getStringBounds()
of that AttributedString gives a width of 164.0, as indicated by the trace output.
java.awt.geom.Rectangle2D$Float[x=0.0,y=-12.064453,w=164.0,h=15.09375]
However, the picture suggests the width should be 300-400 (because the width of the panel is 500).
Could you help to comment the reason and the workaround ?
MyJFrame.java
import javax.swing.*;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
class MyJPanel extends JPanel {
MyJPanel() {
setPreferredSize(new Dimension(500,500));
}
@Override
public void paintComponent(Graphics gold) {
super.paintComponent(gold);
Graphics2D g = (Graphics2D)gold;
//
AttributedString text = new AttributedString("Bunny rabits and flying ponies");
text.addAttribute(TextAttribute.FONT, new Font("Arial", Font.BOLD, 24), 0, "Bunny rabits".length());
text.addAttribute(TextAttribute.FOREGROUND, Color.RED, 0, "Bunny rabits".length());
text.addAttribute(TextAttribute.FONT, new Font("Arial", Font.BOLD & Font.ITALIC, 32), 17, 17 + "flying ponies".length());
text.addAttribute(TextAttribute.FOREGROUND, Color.BLUE, 17, 17 + "flying ponies".length());
FontMetrics fm = g.getFontMetrics();
System.out.println(fm.getStringBounds(text.getIterator(), 0, text.getIterator().getEndIndex(), g));
g.drawString(text.getIterator(), 50, 50);
//
g.dispose();
}
}
public class MyJFrame extends JFrame {
public static void main(String[] args) {
MyJFrame frame = new MyJFrame();
frame.setContentPane(new MyJPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}