Align text with java graphics 2D

Can anyone tell me how to find text in Java 2D?

This is the code that draws a list of naturally aligned text

Font yFont = new Font("Arial",Font.BOLD,13);

interval = 0;

g2d.setFont(yFont);
for (String l : binLabels) {
     g2d.drawString(l,(135 + interval));
     interval = interval + 15;
}

It's driving me crazy Thank you

slothishtype

Solution

In the paintcomponent () method, you can use fontmetrics to get the width of the string to draw:

FontMetrics fm = getFontMetrics( getFont() );
int width = fm.stringWidth("your string here");

Then calculate the offset position to start drawing according to the width of the component

The question is why you did it You can use jlabel and set its alignment to the right

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>