• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Non-static method cannot be referenced from a static context?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.

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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Hayles Berry
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Hayles Berry
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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). ^^;;
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



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

Also, I don't quite understand why I'm adding the character's width to the x-coordinate (or how to do so). ^^;;



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?
 
Hayles Berry
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that the rectangle was supposed to replace certain characters? At least, that's what I thought your original post said. In which case, no, it doesn't matter whether you're going to display a character or a rectangle, whichever you display goes in the same place.

And I don't see any code in that loop which displays characters, so I don't understand it. Here's my pseudocode:


 
Hayles Berry
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

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.
 
Hayles Berry
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Hayles Berry
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic