Font Resize

J

Joseph Gruber

Hi all -- I'm new to Java programming but I'm trying to write a
program that emulates a terminal (e.g. DOS). I'm using the NetBeans
IDE and I'm looking for any suggestions on how to have the font resize
when the window is resized. Basically so that a specific number of
"lines" (20) would be in the window.

Thanks

Joseph
 
S

saalocin

Hi all -- I'm new to Java programming but I'm trying to write a
program that emulates a terminal (e.g. DOS). I'm using the NetBeans
IDE and I'm looking for any suggestions on how to have the font resize
when the window is resized. Basically so that a specific number of
"lines" (20) would be in the window.

Thanks

Joseph

Well you cant learn java by using netBeans. netBeans is for ppl that
know the J classes. I suggest thet you drop netBeans and do it the
hard way the first time. Otherwise you are wasting your time by using
it. I suggest that you use eclipse, you wil learn the classes very
fast with that.
 
J

Jeff Higgins

Joseph said:
Hi all -- I'm new to Java programming but I'm trying to write a
program that emulates a terminal (e.g. DOS). I'm using the NetBeans
IDE and I'm looking for any suggestions on how to have the font resize
when the window is resized. Basically so that a specific number of
"lines" (20) would be in the window.

You don't say how far along you are. Have you seen this?
 
J

Joseph Gruber

You don't say how far along you are. Have you seen this?

Yeah I've looked through that but didn't see anything related to font
resizing based upon the form resizing.

saalocin -- if I wasn't using NetBeans how would you suggest doing
something like this?
 
J

Jeff Higgins

Joseph said:
Yeah I've looked through that but didn't see anything related to font
resizing based upon the form resizing.
OK. That narrows it down somewhat. Have you seen this?
 
J

Joseph Gruber

Yeah I thought about listening for the resize event and then writing
some code to calculate the proper font size and then setting it but I
was hoping there might have been some easier way -- e.g. like a font-
resize property or something on a label

And while searching around a little bit more I realize I'm using the
wrong term. I want my fonts to "scale" with the window size.
 
J

Jeff Higgins

Joseph said:
And while searching around a little bit more I realize I'm using the
wrong term. I want my fonts to "scale" with the window size.
You might also look at Font.deriveFont()
 
R

Roedy Green

And while searching around a little bit more I realize I'm using the
wrong term. I want my fonts to "scale" with the window size.

you are going to have to field a resizing event and resize the
component fonts inside. There are ways to look at all the children to
find the ones suitable for a resize. You then want to revalidate your
container.

see http://mindprod.com/jgloss/event11.html
 
K

Karl Uppiano

Joseph Gruber said:
Hi all -- I'm new to Java programming but I'm trying to write a
program that emulates a terminal (e.g. DOS). I'm using the NetBeans
IDE and I'm looking for any suggestions on how to have the font resize
when the window is resized. Basically so that a specific number of
"lines" (20) would be in the window.

About 10 years ago now (whew! has it been that long?) I was tasked with
writing an IBM 3270 mainframe terminal emulator in Java. We were using AWT
at the time, but not that much has changed when you're doing this kind of
application. Our screen size was 24 rows x 80 columns. We used a fixed-pitch
font (monospace). We listened to the component resize event.

On each size event, we calculated the largest font that would fit into the
available space (vertical and horizontal), by iterating through the font
sizes starting at 1 point, and creating a new font, looking at the font
metrics (advance and height + descent, IIRC). When the font got too big, we
went back to the previous point size, and used that one. We never found a
more efficient way to do this, but it was actually fast enough. The screen
and the font would resize in real time as the user dragged the window
border.
 
A

Andrew Thompson

Karl Uppiano wrote:
...
On each size event, we calculated the largest font that would fit into the
available space (vertical and horizontal), by iterating through the font
sizes starting at 1 point, and creating a new font, looking at the font
metrics (advance and height + descent, IIRC). When the font got too big, we
went back to the previous point size, and used that one. We never found a
more efficient way to do this, ...

<speculative>
Cache the the last component size and current point size.
If the resize show it larger, iterate the point size upwards
from the last font size, else iterate down.

Another optimization that springs to mind is to check the
ratio of cached viewing width to point size, and use that
as a guide to the size of the font with the new width.
...but it was actually fast enough.

Which raises the whole question of whether doing anything
other than that, amounts to 'over optimization'.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200706/1
 
K

Karl Uppiano

Andrew Thompson said:
Karl Uppiano wrote:
..

<speculative>
Cache the the last component size and current point size.
If the resize show it larger, iterate the point size upwards
from the last font size, else iterate down.

Another optimization that springs to mind is to check the
ratio of cached viewing width to point size, and use that
as a guide to the size of the font with the new width.
</speculative>

Come to think of it, I think we might have done the first optimization after
we got it working correctly. Memory fades...
 
K

Karl Uppiano

Andrew Thompson said:
Karl Uppiano wrote:
..
Which raises the whole question of whether doing anything
other than that, amounts to 'over optimization'.

Another thing... we did this in the "update" loop on a buffered image, so
AWT could coalesce events. All we did on "paint" was blit the buffered image
to the screen. This was before the days of double-buffered Swing components.
 
R

Roedy Green

On each size event, we calculated the largest font that would fit into the
available space (vertical and horizontal), by iterating through the font
sizes starting at 1 point, and creating a new font, looking at the font
metrics (advance and height + descent, IIRC). When the font got too big, we
went back to the previous point size, and used that one. We never found a
more efficient way to do this, but it was actually fast enough. The screen
and the font would resize in real time as the user dragged the window
border.

you could cache the results.
 
J

Joshua Cranmer

Hi all -- I'm new to Java programming but I'm trying to write a program
that emulates a terminal (e.g. DOS). I'm using the NetBeans IDE and I'm
looking for any suggestions on how to have the font resize when the
window is resized. Basically so that a specific number of "lines" (20)
would be in the window.

Thanks

Joseph

You could try officially calculating the font size for a window:

There is a method somewhere that tells you resolution (e.g., my monitor
is 96 DPI) (GraphicsDevice?), and there are 72 points per inch, so a 12
point font would be (theoretically) 16 pixels high on my monitor.

int calculateFontSize(int newHeight) {
double lineHeight = newHeight/20.0;
double pixperpoint = {dpi}/72.0;
double fontSize = lineHeight/pixperpoint;
return (int)(fontSize+1e-5); // Round up slightly to avoid fp-error
}
 
K

Karl Uppiano

Roedy Green said:
you could cache the results.

Yes, if memory serves we did avoid recalculating the whole thing every time;
I just meant we could not think of any way to avoid the trial-and-error
approach of fitting a font into a given space.

After the initial full iteration, I believe we worked up or down from the
current font size based on the direction of the component size change.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top