Java Reference
In-Depth Information
int
screenWidth
= screenDim.width;
int
pixPerInch
= tk.getScreenResolution();
■
13.3
Scaling a drawing
Resizing the frame from Section 5.3 does not resize the drawing. Thus it might
become partly invisible if the frame is made small or just fill a very small portion of
the frame if the frame is large. To access a large drawing one can use scroll panes
as described in Chapter 11. Sometimes this is not appropriate because one wants
the whole drawing displayed regardless of the size of the window. Here we describe
how to resize a drawing if the window is resized. This approach is suited for simple
and more or less static drawings. In Chapter 21 a much more elaborate method
is presented, which allows complex, highly dynamical and resizeable drawings to
be displayed.
We derive a frame
ResizeFrame
from
SimpleFrame
.Inits content pane we
glue a
ResizePanel
which we derive from
JPanel
.A
ResizePanel
has a yellow
background and a black rectangle in the middle. The black rectangle is one-third
as wide and high as the panel. This ratio is preserved if the frame (and with it the
panel) is resized.
In order to do this we have to know the
current
size of the panel. We use the
methods
int
getWidth();
int
getHeight();
They return the current width and height of the panel in pixels. The left edge of the
black rectangle is then one-third the width of the panel. We then place the black
rectangle at the desired position by calling
drawRect
with the appropriate val-
ues. The following listings show the code including the start class
ResizeDriver
.
Figure 13.1 shows the result.
Figure 13.1 The resizeable frame
Search WWH ::
Custom Search