Академический Документы
Профессиональный Документы
Культура Документы
Provides
A uniform rendering model for display devices and printers
A wide range of geometric primitives
Mechanisms for performing hit detection on shapes, tet, and images
A compositing model
!nhanced color support that facilitates color management
"upport for printing comple documents
Most importantly it is the #uilding #loc$s to your own custom
components%
&endering process
"hapes
Images
'et
'ransformation
stro$e
font
&endering hints
rasteri(er
)lipping shape paint )ompositing rule
fill*+
draw*+
Draw"tring*+
drawImage*+
images
Graphics
Primitives
Output
Devices
,raphics2D
2D ,raphics
'he Java 2D API ena#les you to control
-ine 'hic$ness
.ills
Move, rotate, scale, and shear tet and graphics
)omposites
Allows for comple graphics *)harts, overlays, etc/+
'he Java 2D API also ena#les you to store and to manipulate image
data
0lur
"harpen
'he Java 2D APIs are closely integrated with the A#stract 1indowing
'ool$it *A1'+%
)oordinate "paces
Java 2D o#2ects live in User Space, defined #y )artesian coordinates
3#2ects rendered to an output device are transformed into Device
Space for that device%
4sually one unit of device space corresponds to one piel on the device%
Default transformations converts 52 piels to one inch *typical screen
resolution+
Default transformation has physical screen si(e e6ual physical print
si(e
'he &asteri(er
Inside the rendering pipeline, the rasteri(er ta$es ideal shapes within
your program and produces coverage values for each piel on a
display device%
'he coverage values represent how much of each piel is covered #y
the shape%
'hese coverage values are called alpha values.
'he alpha value indicates the transparency of the color itself%
Alpha values range from 7%7 to 8%7, 7%7 #eing fully transparent, 8%7
#eing fully opa6ue%
Anti9aliased shapes often have piels with non 7%7:8%7 values at their
edges
)ompositing is used to decide how to translate alpha values into color
information
)ompositing determines how the colors of a new graphics primitive
are com#ined with the eisting colors on a drawing surface%
Drawing "hapes
!cept for Point2D and Dimension2D, each of the geometry classes
implements the "hape interface%
1ith these classes you can create virtually any geometric shape and
render it through ,raphics2D #y calling the draw method or the fill
method%
'o draw someShape
g2d.draw(someShape);
g2d.fill(someShape+;
Painting in Java
In A1', painting was done in the paint*,raphics g+ method of a
2ava%awt%)anvas
In "wing, you will do painting in the paint)omponent*,raphics g+
method of a 2ava%swing%J)omponent
J)omponent<s paint*+ method now calls paint)omponent*+,
paint0order*+ and paint)hildren*+
)hanges to the ,raphics2D o#2ect within paint stay within the
paint)omponent*+ method%
1ith the eception of &endering =ints
1ith the A1', you generally drew a shape #y calling the drawXxx or
fillXxx methods of the ,raphics o#2ect% In Java2D, you generally
create a "hape o#2ect, then call either the draw or fill method of the
,raphics2D o#2ect, supplying the "hape o#2ect as an argument% >ou<ll
still want draw"tring and drawImage, however% =ere<s an eample of
the normal approach?
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d (Graphics2D)g;
!! "ssume #$ %$ and diameter are instance variables
&llipse2D.Double circle
new &llipse2D.double(#$ %$ diameter$ diameter);
g2d.fill(circle);
...
'
Drawing "hapes? 3verview
"imple"hapes%2ava
Note rough edge to drawing
)olors in Java 2
)olor is found in 2ava%awt pac$age
Primarily unchanged through different A1':"wing versions
It is a class to encapsulate colors in the default s&,0 color space
Multiple )onstructors
)olor*float r, float g, float #,+
)olor*float r, float g, float #, float a+
)olor*int rg#+
&ed in #its 8@92A
,reen in #its B98C
0lue in #its 795
)olor*int r, int g, int #+
)olor*int r, int g, int #, int a+
"ystem)olors
J.) allows you to set colors relative to des$top
4se 4IManager%get)olor*"tring itemDame+ to pull the current loo$ and
feel colors into your application
.or !ample, if you wanted to set the #ac$ground of your window to
the default #ac$ground on the loo$ and feel you are using, you would
use
set0ac$ground*4IManager%get)olor*EwindowF++
4se these instead of the eplicit )olor o#2ects whenever possi#le
"ample values can #e found in Metal-oo$And.eel%2ava, #ut they wor$
in any of the -oo$ and .eels
)olor $eys
Gdes$topF 9 )olor of the des$top #ac$ground
Gactive)aptionG 9 )olor for captions *title #ars+ when they are active%
Gactive)aption'etG 9 'et color for tet in captions *title #ars+%
Gactive)aption0orderG 9 0order color for caption *title #ar+ window
#orders%
Ginactive)aptionG 9 )olor for captions *title #ars+ when not active%
Ginactive)aption'etG 9 'et color for tet in inactive captions *title
#ars+%
Ginactive)aption0orderG 9 0order color for inactive caption *title #ar+
window #orders%
GwindowF 9 Default color for the interior of windows
Gwindow0orderF 9 0order of window
Gwindow'etF 9 'et color of window
GmenuF 9 0ac$ground color for menus
Gmenu'etF 9 'et color for menus
More )olor $eys
GtetF 9 'et #ac$ground color
Gtet'etF 9 'et foreground color
Gtet=ighlightF 9 'et #ac$ground color when selected
Gtet=ighlight'etF 9 'et color when selected
GtetInactive'etF 9 'et color when disa#led
GcontrolF 9 Default color for controls *#uttons, sliders, etc+
Gcontrol'etF 9 Default color for tet in controls
Econtrol=ighlightF 9 "pecular highlight *opposite of the shadow+
Gcontrol-t=ighlightF 9 =ighlight color for controls
Gcontrol"hadowF 9 "hadow color for controls
GcontrolD$"hadowF 9 Dar$ shadow color for controls
Gscroll#arF 9 "croll#ar #ac$ground *usually the Gtrac$G+
GinfoF 9 'ool'ip 0ac$ground
Ginfo'etF 9 'ool'ip 'et
)lasses in the 2ava%awt%geom pac$age
Point2D
Arc2D
Area
)u#ic)urve2D
Dimension2D
!llipse2D
,eneralPath
Points
A point is not a piel
Point2D is an a#stract class
)ontains inner children
Point2d%.loat and Point2D%Dou#le
Point is still left over from A1' 8%7, holds coordinates as integers
Applying?
)all translate, rotate, scale, or shear on ,raphics2D o#2ect
)reate an Affine'ransform and apply it via set'ransform or transform
9ageable interface
specifies a way to associate a 9age0ormat and a 9rintable with
each page of a print 2o#%
,ava.awt.print.*oo+ implements this interface
Downloads
,et the presentation at http?::we#dev%apl%2hu%edu:Y
r#e:2ava:JavaM2D:all%(ip