Paul Clapham wrote:That's just telling you that the stringWidth() method isn't a static method of the FontMetrics class, so you can't call it that way. You have to get a suitable instance of FontMetrics and call the method on that instance.
You're getting into pretty deep water for somebody who is still a beginner, so I will tell you that you need a FontMetrics object that reflects how the characters will be drawn. That means you should ask your Graphics object for one.
Paul Clapham wrote:Well, I can't tell what most of your posted code is doing, but here's what I would do:
Start with your x-coordinate at zero. Go through the string to be displayed one character at a time. For each character you read, figure out its width. Either display the character at the current x-coordinate, or display the rectangle if the rules say to do that instead. Add the character's width to the x-coordinate and continue with the next character.
Hayles Berry wrote:That does seem like the right thing to do, but I'm having a bit of trouble with the syntax of telling the program to decide whether to display the character at the current x-coordinate, or display the rectangle.
Could you possibly give me a general example?
Also, I don't quite understand why I'm adding the character's width to the x-coordinate (or how to do so). ^^;;
Paul Clapham wrote:Um... for syntax, you'd use an "if" statement to decide between two alternatives. But probably I'm misunderstanding your question.
Paul Clapham wrote:You need to know where each of the characters in the string is supposed to be displayed. So ask yourself these questions: Where should the first character be displayed? Where should the second character be displayed? Where should the third character be displayed?
Paul Clapham wrote:I thought that the rectangle was supposed to replace certain characters? At least, that's what I thought your original post said.
Paul Clapham wrote:And I don't see any code in that loop which displays characters, so I don't understand it.
Paul Clapham wrote:
Paul Clapham wrote:If I understand it right, you're displaying all of the characters at the start and then trying to go back and replace some of them (i.e. over-write them?) with something else. That seems to me to be the hard way of doing it.
Paul Clapham wrote:Anyway, how do you get the dimensions of a character in that font? Well, that's what the FontMetrics object is supposed to tell you. You've already figured out how to get the width, and there should be a method to get the height.