Hayles Berry

Ranch Hand
+ Follow
since Dec 16, 2012
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Hayles Berry

Actually, it's all okay now, I've managed to figure it all out from what you'd said so far.
Thank-You for all your help, it means a lot.
11 years ago
Actually, it's all okay now, I've managed to figure it all out from what you'd said so far.
Thank-You for all your help, it means a lot.
11 years ago

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.

It's the only way I know. Why, is there an easier way?

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.

If you mean by changing 'Width' to 'Height' in the FontMetrics method, I just tried that and it gave me a 'cannot find symbol' error.
11 years ago

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.

Not a rectangle. A specific polygon that I've drawn.

Paul Clapham wrote:And I don't see any code in that loop which displays characters, so I don't understand it.

That's because that piece of code isn't in that loop. It's not displaying the characters that's the problem, it's getting the polygons to display in the right place, at each occurrence of the character they're replacing.

Here's my pseudocode:

Paul Clapham wrote:

How do I get the dimensions?
11 years ago

Paul Clapham wrote:Um... for syntax, you'd use an "if" statement to decide between two alternatives. But probably I'm misunderstanding your question.

No, you haven't misunderstood it, don't worry. :)
That bit's working now (to an extent). The only problem is, the polygon is only being displayed at the first occurrence of the char it's representing. All other occurrences are being left as blank spaces.

Here's my code for this so far if it helps:

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?

In my case, wouldn't that depend on what the character is?
If it's a h/H or a b/B, it should be displayed as a polygon. Otherwise, it should be displayed as normal.

Btw, does 'x-coordinate' mean the x-coordinate of the polygon I'm drawing?
If so, how would I go about adding that to the character's width without getting an "incompatible types" error?
11 years ago

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.

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). ^^;;
11 years ago
Actually, I've managed to get the FontMetrics bit working based on what you said Paul, so thanks a million for that.

Now all I need to do is figure-out how to incorporate the charWidth value I'll get from FontMetrics into a calculation, that will give me the X coordinate of that specific char in my String, thus (hopefully) allowing me to make it appear as if an arbitrary drawn polygon has been put in said char's place in the String... Heh, when I say it like that it probably sounds more complex than it is, but it's the best explanation I can think of. xD
11 years ago

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.

Okay, thanks, do you have any possible hints/examples of how I could do that?
11 years ago

I am currently writing a Java applet that takes a string, input by the user, and performs various actions on it, the main one being taking two specific characters (H and B) and replacing them with arbitrary drawn polygons. In order to do this however, I have been advised on another forum that I need to find the x coordinate of where the polygon should be drawn, by knowing the width of each letter and multiplying that by how far into the string I am.

What I would like to know is how to find out the width of any one character within a string, as what I have in my paint method so far is causing the error mentioned in the title, in the line:

int width = FontMetrics.stringWidth("b");

Any help/guidance would be greatly appreciated.
11 years ago

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.

What do you mean by 'data structures'?

Also, this is the code fragment that's giving me the problem.

I don't know whether you'll be able to tell me where I'm going wrong?
What should be drawn every time the user inputs a string of text is just that, the string. But if the user types in a string that contains either one or both of my "special characters" ('h'/'H' or 'b'/'B', called 'sl1' and 'sl2' in my code), that character's relevant polygon should be displayed in its' place. The rest of the string should also be displayed as normal.

11 years ago

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.

Okay. How would I do that, exactly?
11 years ago

Ulf Dittmer wrote:Yes, that's fine. Now you just have to write the paint method so that it makes use of them.

Done, but I now have a new problem I've just mentioned in my last post. ^^
Thanks for all your help so far, by the way. It means a lot.
11 years ago

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.

Okay, that does seem to make sense.

I have it working now, but I have now been presented with a new problem.
Before I got the text to be drawn at the point of mouse click, I had two polygons that were only drawn to the screen by the program if a certain two keyboard characters were entered as part of user input (either together or separately). Since fixing this topic's problem, those two polygons no longer show-up on screen at all when the certain two characters are typed in.
11 years ago
I am currently creating a Java applet. The applet works by taking text input from the user and displaying it to the screen when they press return. This is the point up to which my program is working at the moment. I was wondering whether there was a way in which I could make it so the text input/String is drawn when the mouse is clicked, at the point of mouse click.

Many thanks in advance to anyone who can help me out with this.
11 years ago

I was wondering if it was possible to replace a character with a polygon, in any instance of that character's occurrence in a string?
For example, if I typed-in the word 'Holly' and hit 'enter', the first letter would be replaced by the polygon.
If I then went to type the word 'thistle', the polygon's new position would be in place of the second letter?

This is my code fragment, the character I want to replace is called sl1.

Any help/guidance will be greatly appreciated.
11 years ago