> 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
[quoted text clipped - 5 lines]
>
> 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.
Joseph Gruber wrote:
> 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?
<http://java.sun.com/docs/books/tutorial/uiswing/components/text.html>
> Thanks
>
> Joseph
Joseph Gruber - 23 Jun 2007 19:08 GMT
> Joseph Gruber wrote:
> > Hi all -- I'm new to Java programming but I'm trying to write a
[quoted text clipped - 9 lines]
>
> > Joseph
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?
Jeff Higgins - 23 Jun 2007 19:12 GMT
Joseph Gruber wrote:
> 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?
<http://java.sun.com/docs/books/tutorial/uiswing/events/index.html>
Joseph Gruber - 23 Jun 2007 19:24 GMT
> Joseph Gruber wrote:
>
[quoted text clipped - 3 lines]
> OK. That narrows it down somewhat. Have you seen this?
> <http://java.sun.com/docs/books/tutorial/uiswing/events/index.html>
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
Joseph Gruber - 23 Jun 2007 19:39 GMT
> 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.
Jeff Higgins - 23 Jun 2007 20:23 GMT
Joseph Gruber wrote:
>> 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
[quoted text clipped - 3 lines]
> 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.
Well, I guess you got me. Your'e using Swing I guess. What Component.
This guy gives an example of scaling text in a Graphics2D environment.
But I don't think that's what your after.
<http://forum.java.sun.com/thread.jspa?threadID=5182688>
Jeff Higgins - 23 Jun 2007 20:46 GMT
Joseph Gruber wrote:
>> 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
[quoted text clipped - 3 lines]
> 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()
Roedy Green - 24 Jun 2007 03:24 GMT
On Sat, 23 Jun 2007 11:39:31 -0700, Joseph Gruber
<joseph.gruber@gmail.com> wrote, quoted or indirectly quoted someone
who 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 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
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
> 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.
Andrew Thompson - 24 Jun 2007 04:08 GMT
...
>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.
</speculative>
>...but it was actually fast enough.
Which raises the whole question of whether doing anything
other than that, amounts to 'over optimization'.

Signature
Andrew Thompson
http://www.athompson.info/andrew/
Karl Uppiano - 24 Jun 2007 04:18 GMT
> ..
>
[quoted text clipped - 7 lines]
> 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...
Karl Uppiano - 24 Jun 2007 04:28 GMT
> ..
> 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.
Roedy Green - 24 Jun 2007 07:20 GMT
On Sun, 24 Jun 2007 02:36:41 GMT, "Karl Uppiano"
<karl.uppiano@verizon.net> wrote, quoted or indirectly quoted someone
who said :
>On each size event, we calculated the largest font that would fit into the
>available space (vertical and horizontal), by iterating through the font
[quoted text clipped - 4 lines]
>and the font would resize in real time as the user dragged the window
>border.
you could cache the results.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Karl Uppiano - 24 Jun 2007 20:46 GMT
> On Sun, 24 Jun 2007 02:36:41 GMT, "Karl Uppiano"
> <karl.uppiano@verizon.net> wrote, quoted or indirectly quoted someone
[quoted text clipped - 11 lines]
>
> 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.
> 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
[quoted text clipped - 5 lines]
>
> 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
}