• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can i identify the total number of fonts be fit to my screen resolution in one line only?  RSS feed

 
SunilK Chauhan
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Guys,

I have a new challenge to set a big line including hundred numbers of characters into one line only. I know that in one line 80 characters can be stored. but here i have a facility to change the type of fonts and size of fonts also.
Than how can i identify that how many characters can be stored in one line with the selected font style and size?

is it possible to do that in Swing application?
 
Paul Clapham
Sheriff
Posts: 22514
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yes it is. But there's a lot of obscure classes involved. I wrote code like that about five years ago and I haven't looked at it since, so I'll just give a brief outline.

First you have to put your hundred-number string into an AttributedString along with the details of the Font you plan to use. That includes the size of the Font, so you will want to repeat this with different Font sizes until you find one which works.

Next get the width of the component you want to display the string into. You should be able to get a Dimension object from the component and then get its width.

Finally you need some code like this:



At least I think that's right, I've extracted that snippet from a more complicated bit of code which is supposed to wrap the text into a block of fixed width. So you will probably have to play around with it. You might also want to search out a tutorial dealing with those classes, because that's most likely where I found out about them.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend going with FontMetrics
A working example can be found here https://docs.oracle.com/javase/tutorial/2d/text/measuringtext.html
Also check out the convenient static method SwingUtilities#computeStringWidth(FontMetrics fontMetrics, String stringToMeasure);
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!