Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing text with AWT Going out of bounds

 
Chris Poe
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a hard time figuring out why Java cannot draw a string in the place I want it to go.

I am trying to draw a string, and have it start at X=0.  However, it seems to start at X=-5 , or even less.. depending on the letter.
What is it about certain letters that makes them go out of bounds like this? And how can I compensate for it?
My final goal is to be able to draw a string that fit perfectly within a box, without ridiculous padding just to ensure it won't get cut off.

Here is the code, if you run it you will see the letters are cut off quite a bit.. especially with this font, Komika Axis. There are many other fonts however that would cut off like this.

Thanks in advance for any help!.

 
Paul Clapham
Sheriff
Posts: 21576
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did try your code (and thank you for providing a short and runnable example). I don't have the Komika Axis font installed on my computer but no matter what other font I tried I couldn't replicate your claim that letters are "cut off" -- presumably at the left-hand side? Every example I tried had the text correctly left-justified.

But perhaps I didn't try one of the other fonts which you say have the same problem? Could you provide a list of them and maybe one of them will be a font that I have?
 
Rob Camick
Ranch Hand
Posts: 2673
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some other comments:

1) Custom painting is done by overriding paintComponent(...) not paint(...) and super.paintComponent(...) should be the first statement in the method.

2) You need to override `getPreferredSize()` to return the size of your component so your component can be used properly with layout managers.

Read the section from the Swing tutorial on Custom Painting for more information.
 
Chris Poe
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I did try your code (and thank you for providing a short and runnable example). I don't have the Komika Axis font installed on my computer but no matter what other font I tried I couldn't replicate your claim that letters are "cut off" -- presumably at the left-hand side? Every example I tried had the text correctly left-justified.

But perhaps I didn't try one of the other fonts which you say have the same problem? Could you provide a list of them and maybe one of them will be a font that I have?
axis.jpg
[Thumbnail for axis.jpg]
 
Chris Poe
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I did try your code (and thank you for providing a short and runnable example). I don't have the Komika Axis font installed on my computer but no matter what other font I tried I couldn't replicate your claim that letters are "cut off" -- presumably at the left-hand side? Every example I tried had the text correctly left-justified.

But perhaps I didn't try one of the other fonts which you say have the same problem? Could you provide a list of them and maybe one of them will be a font that I have?


Hi Paul,
See screenshot in my above post.

I'm starting to realize that it's script type fonts, and some non-script fonts with italics turned on that have this problem happening.

Try any script font you have.  Something like Palace Script MT.
 
Chris Poe
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Some other comments:

1) Custom painting is done by overriding paintComponent(...) not paint(...) and super.paintComponent(...) should be the first statement in the method.

2) You need to override `getPreferredSize()` to return the size of your component so your component can be used properly with layout managers.

Read the section from the Swing tutorial on Custom Painting for more information.


I made this a Swing component so the problem can be demonstrated easily.  I'm really writing on a Buffered image and then saving it to a file, and having the same problem.

 
Paul Clapham
Sheriff
Posts: 21576
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With Palace Script MT there's maybe one pixel cut off at the left margin, and the same with Freestyle Script. But I haven't found any fonts in which the minus sign is cut off at the left margin, contrary to your screen post.

I'm running Java 8 on a Windows machine, if that makes any difference.
 
Chris Poe
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:With Palace Script MT there's maybe one pixel cut off at the left margin, and the same with Freestyle Script. But I haven't found any fonts in which the minus sign is cut off at the left margin, contrary to your screen post.

I'm running Java 8 on a Windows machine, if that makes any difference.


All environments will render same.

Here, this code will cut off using Palace Script.

 
Chris Poe
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:With Palace Script MT there's maybe one pixel cut off at the left margin, and the same with Freestyle Script. But I haven't found any fonts in which the minus sign is cut off at the left margin, contrary to your screen post.

I'm running Java 8 on a Windows machine, if that makes any difference.


The minus sign cuts off on Komika Axis. Other fonts, not so much. It really depends on the font.

What I'm trying to figure out is, how can I compensate when fonts do have this issue for certain characters, like the lower case L on Palace Script that goes out by a lot. I would have expected it to be quite simple, the letter is in the font, it has a width, so Java measures the width and places it at exactly 0,0. However there seems to be much more to it than that.
 
Chris Poe
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:With Palace Script MT there's maybe one pixel cut off at the left margin, and the same with Freestyle Script. But I haven't found any fonts in which the minus sign is cut off at the left margin, contrary to your screen post.

I'm running Java 8 on a Windows machine, if that makes any difference.



Here is a better example. What I did is set a blackground for the text attribute.
It so happens that the background represents what Java thinks is the size of the letter.. which is returned by FontMetrics stringWidth. If you measure the red background in pixels, it matches the stringWidth.
Now, all I need is to figure out how to get the pixel count on the left side. Any ideas?

 
Chris Poe
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have figured out that the "Width" is the baseline width.. the part of the letter that touches the baseline from the leftmost to rightmost. In my example, the letter being italic, the top right/bottom left parts stick out, so they don't touch the baseline.
I can't seem to find any Java function to return the actual letter width in FontMetrics.
 
Chris Poe
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have solved it..
Basically you have to render the letter as a glyph, and that tells you it's real width, as well as the actual starting X position, then you can adjust by that X position and move the letter forward.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic