File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Font spacing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Font spacing" Watch "Font spacing" New topic

Font spacing

Joe Ridener
Ranch Hand

Joined: Jul 16, 2012
Posts: 88
I have two difficult tasks at hand that involve the font spacing to be accurate. Is there any way to control the font spacing so that it will occupy a given cell before the next letter. Many people do not realize that when you use draw String as it is, it has the tendency to paint the letters unevenly side by side, one takes up 2 pixels the next may take up more and there is no space between them. Therefore, if you are trying to do something that involves character spacing it will not work as it is. It need additional control to provide a means of even spacing between letters. I want to get character and surrounding space to occupy one 10 pixel cell. Each and every letter. So that 10 letters take up 100 pixels or 70 at 7 pixel cells. Any ideas?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

If you need each character to be equally wide you should use a monospaced font. You can create a java.awt.Font object quite easily:

How To Ask Questions How To Answer Questions
Joe Ridener
Ranch Hand

Joined: Jul 16, 2012
Posts: 88
Thank you Rob,

I did actually come up with something that worked. I created a Font object like so
new Font("monospaced.plain",Font.Plain,12); It did actually work though it was less than perfect. I am doing a text pad from the ground up and JTextArea will not cut it although it might.
I agree. Here's the link:
subject: Font spacing
It's not a secret anymore!