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.
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: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: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.
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.
Ulf Dittmer wrote:That depends on what might be displayed at any given time. The thing to keep in mind is that the paint method might be called any number of times, whether you expect it to or not. Whatever it doesn't draw whenever it's called, might be gone from the display. So... your data structures and the paint method need to work together to produce whatever it is that should be displayed.
Ulf Dittmer wrote:You need to keep track of all state of what should and should not be drawn in some appropriate data structures, so that any time the paint method is called (and you'll never know when it will be called), it draws exactly that which should be shown at that point in time. It sounds as if that is not the case here.
Ulf Dittmer wrote:Yes, that's fine. Now you just have to write the paint method so that it makes use of them.
Ulf Dittmer wrote:Certainly. You'd need a MouseListener that records the X/Y coordinates of where the click happened, and then call repaint() to have the component be drawn anew.
This assumes that you have altered the paintComponent method (assuming you extend JApplet) or paint method (assuming you extend Applet) so that it draws the string at those coordinates. That means you need to store the coordinates somewhere where the paint/paintComponent method can get at it.