Juxhin
Juxhin

Reputation: 5640

Issue regarding Graphics 2D drawString

I have written my own Pie chart class and have decided to add a displayValues() which basically displays values, one of the parameters is Direction which is my ENUM consisting of LEFT, RIGHT, TOP, BOTTOM.

Basically the drawString will draw according to the Direction. The other parameter is also the padding between the String and the Pie Chart.

Everything is going well however the issue comes up when I want to create a padding when direction == LEFT.

Issue being that I cannot set the drawString(String str, getX() - 10, int y) as the string will still go inside the chart.

I would like to be able to keep the padding from right to left as opposed to the usual left to right. Is this possible?

Here's what I'm trying to get at: enter image description here

Upvotes: 0

Views: 219

Answers (1)

Carmeister
Carmeister

Reputation: 216

Where g is your Graphics object, use

int textWidth = g.getFontMetrics().stringWidth(str)

to get the width of the text you want to draw. Then you can replace the getX() - 10 with getX() - textWidth - 10, which will position the text so that the end of it is 10 pixels to the left of getX(), as desired.

Upvotes: 1

Related Questions