Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Chapter 11
Graphics and Java2D
—clipboards,
—support for printing and
—keyboard navigation.
Why?
The original AWT deals with GUI elements
by not dealing with them.
Display
… write once,
debug everywhere...
AWT
Frame,
Window,
Dialog
Image-Observer
Component
M
Container
1
interface
All* Containers come equipped with a layout LayoutManager
manager, which positions and shapes the
container’s components.
—Graphics object
—Location
—Size
—Native peer
—Parent container
—Fonts and font dimensions ( font metrics)
—Foreground and Background colors
—Locale
—Minimum, Maximum and Preferred sizes
Java I--Copyright © 2000 Tom
• A Component does not decide for itself how it will be
shaped, and so its shape ends up taking a lot of abuse.
ScrollPane—scrolls.
—The horizontal and vertical gaps specify not only the gaps
between components but also between the edges of the
components and the sides of the container.
horizontal gap
vertical gap
rows
columns
(Zero means the number total horizontal gap
is computed)
vertical gap
Java I--Copyright © 2000 Tom
GridBag Layout
public GridBagConstraints ()
{
gridx = RELATIVE;
gridy = RELATIVE;
gridwidth = 1;
gridheight = 1;
weightx = 0;
weighty = 0;
anchor = CENTER;
fill = NONE;
Container c = getContentPane();
c.setLayout( gbl );
Container c = getContentPane();
c.setLayout( gbl );
Container c = getContentPane();
c.setLayout( gbl );
c.add( butt );
—gridx
—gridy
These are used to specify the row and column
for the upper-left hand corner of the
component.
Default value:
GridBagConstraints.RELATIVE
Java I--Copyright © 2000 Tom
JButton butt = new JButton( “Hairy” );
Container c = getContentPane();
c.setLayout( gbl );
gbcon.gridx = 0;
gbcon.gridy = 1; Valid Values:
RELATIVE,
gbl.setConstraints( butt, gbcon ); which means to the
previous component, or
c.add( butt );
any integer.
—gridwidth
—gridheight
These descriptions refer to the number of cells,
not the number of pixels.
Default value:
1
Whether or not a component fills its allotted grid
cells depends on the fill attribute.
Container c = getContentPane();
c.setLayout( gbl );
gbcon.gridwidth = 2;
gbcon.gridheight = 2;
—anchor
There are a certain number of valid constants
you can use for these. Note: if the component entirely
fills up its allotted space, you won’t notice its anchor
point.
Default value:
GridBagConstraints.CENTER
Container c = getContentPane();
c.setLayout( gbl );
gbcon.anchor = GridBagConstraints.NORTH;
—fill
This is used when the component’s size is
smaller than the space it is expected to fill.
This determines in what way—if any—the
component will be stretched.
Default value:
GridBagConstraints.NONE
Container c = getContentPane();
c.setLayout( gbl );
gbcon.fill = GridBagConstraints.HORIZONTAL;
—ipadx
—ipady
Default value:
0
Container c = getContentPane();
c.setLayout( gbl );
gbcon.ipadx = 5;
gbcon.ipady = -2;
c.add( butt );
—insets
Default value:
(0,0,0,0)
Container c = getContentPane();
c.setLayout( gbl );
c.add( butt );
—weightx
—weighty
Default value:
0.0
Double values that represent weighting given
to a component’s grid cells relative to other components in
the same row or column.
Java I--Copyright © 2000 Tom
AWT
vs
Swing
}
In this situation, we are actually using a
derived class of Graphics—a Subclass of
Graphics actually implements the drawing
capabilities. Graphics merely supplies the
interface, while using heavyweight
components to do the heavy lifting.
Notice, we never wrote:
Graphics g = new Graphics()
Java I--Copyright © 2000 Tom
Graphics Basics
(X,Y)
• current color
• current font
• drawing
• color manipulation.
red,
green, and
blue components.
• RGB values:
—3 Integers from 0-255 each.
[ This equates to 16.7 million colors.]
Alternately,
Alternately,
Can use the Color Constants ( page 517-518)
Java I--Copyright © 2000 Tom
Color
• Color Constants:
RGB Value
public final static Color orange 255, 200, 0
Red
Green
Blue
g.setColor( col );
Or,
Color x = g.getColor();
Or,
• You specify a font by its font face name ( or font name for
short), and its point size.
leading
ascent
xylgQ$ baseline
descent
Page 531
Java I--Copyright © 2000 Tom
Drawing
Arcs
negative Obviously, it
x, y doesn’t matter
which way you
positive draw the arc. You
can start from 0
degrees and go
negative, or start
from 90 degrees
and go positive.
x, y
versus
Graphics2D
ovals and
polygons.
texture.
interface Shape
• Curves and arcs are created using the same interface.
interface Paint
interface Stroke
java.awt.geom
Ellipse2D.Double
Rectangle2D.Double
RoundRectangle2D.Double
Arc2D.Double
Line2D.Double
When you see the word
First of all, the “.Double” tells us that
.Double
Double is a static inner class.
What does that tell
Each of these classes represent a shape
you?
with its dimensions specified as double-
precision floating point values.
Java I--Copyright © 2000 Tom
Java2D Shapes
• There are Float static inner classes in each of these
classes.
Ellipse2D.Float
Rectangle2D.Float
RoundRectangle2D.Float
Arc2D.Float
Line2D.Float