Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Java 2 D
Java 2 D
Pintar figuras de 2D
Java 2D API
Conte(to Grap1ics
Per!ite pintar en a pantaa)
4o se puede instanciar
Case Color
Case 5ont
:ont na!e
%onospaced' 0ans0erif' 0erif' etc)
:ont stye
5ont3!'A"N' 5ont3"&A'"C y 5ont3)O',
:ont si%e
9edido en puntos
95todos y constantes reacionados con a case 5ont
9et1od or constant Description
Font constants constructors and methods for drawing pol!gons
public final static int !'A"N
( constant representing a plain font style.
public final static int )O',
( constant representing a bold font style.
public final static int "&A'"C
( constant representing an italic font style.
public 5ont( 0tring name. int style. int si6e )
Creates a 5ont object with the specified font+ style and si6e.
public int get0tyle()
$et#rns an integer val#e indicating the c#rrent font style.
public int get0i6e()
$et#rns an integer val#e indicating the c#rrent font si6e.
public 0tring getName()
$et#rns the c#rrent font name as a string.
public 0tring get5amily()
$et#rns the font:s family name as a string.
public boolean is!lain()
!ests a font for a plain font style. $et#rns true if the font is plain.
public boolean is)old()
!ests a font for a bold font style. $et#rns true if the font is bold.
public boolean is"talic()
!ests a font for an italic font style. $et#rns true if the font is italic.
9et1od or constant
Description
Graphics methods for manipulating Fonts
public 5ont get5ont()
$et#rns a 5ont object reference representing the c#rrent font.
public void set5ont( 5ont f )
Sets the c#rrent font to the font+ style and si6e specified by the 5ont
object reference f.
1 // Fig. 12.+: Fonts.java
2 // 0sing 5onts.
3 import java.awt.*;
4 import java.swing.*;
5
! p"#li$ $lass Fonts eten%s &Frame '
(
) // set win%ow*s title #ar an% %imensions
+ p"#li$ Fonts,-
1. '
11 s"per, /0sing 5onts/ -;
12
13 setSi1e, 42.2 125 -;
14 set3isi#le, tr"e -;
15 4
1!
1( // %ispla< Strings in %i55erent 5onts an% $olors
1) p"#li$ voi% paint, 6raphi$s g -
1+ '
2. // $all s"per$lass*s paint metho%
21 s"per.paint, g -;
22
23 // set 5ont to Seri5 ,Aimes-2 #ol%2 12pt an% %raw a string
24 g.setFont, new Font, /Seri5/2 Font.8D:D2 12 - -;
25 g.%rawString, /Seri5 12 point #ol%./2 2.2 5. -;
)l m8todo set5ont establece la f#ente a #sar
)scribe el te"to con la
config#raci7n act#al de
f#ente
2!
2( // set 5ont to >onospa$e% ,Co"rier-2 itali$2 24pt an% %raw a
string
2) g.setFont, new Font, />onospa$e%/2 Font.FA?:FC2 24 - -;
2+ g.%rawString, />onospa$e% 24 point itali$./2 2.2 (. -;
3.
31 // set 5ont to SansSeri5 ,Helveti$a-2 plain2 14pt an% %raw a
string
32 g.setFont, new Font, /SansSeri5/2 Font.J:?F@2 14 - -;
33 g.%rawString, /SansSeri5 14 point plain./2 2.2 +. -;
34
35 // set 5ont to Seri5 ,Aimes-2 #ol%/itali$2 1)pt an% %raw a
string
3! g.setColor, Color.7;D -;
3( g.setFont, new Font, /Seri5/2 Font.8D:D 9 Font.FA?:FC2 1) - -;
3) g.%rawString, g.getFont,-.get@ame,- 9 / / 9
g.getFont,-.getSi1e,- 9
3+ / point #ol% itali$./2 2.2 11. -;
4.
41 4 // en% metho% paint
42
43 // ee$"te appli$ation
44 p"#li$ stati$ voi% main, String argsBC -
45 '
4! Fonts appli$ation = new Fonts,-;
4( application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE )
!" #
!$
%& # '' en( class Fonts
Contro de fuentes
!7 #
!9
!" # '' en( class -etrics
<se 5ont%etrics to
obtain ascent+ descent+
height and leading
$epeat same process for
Serif -25point italic font
%etrics34ava
5 Pintar Lneas! "ectngulos y #$alos
Case Graphics
Arco
Porcin de un vao
+e !iden en grados
Cases !olygon
Po$gonos
Poi$neas
Java 2D API
Proporciona capacidades grficas avan%as 2D
4ava3a/t
4ava3a/t3image
4ava3a/t3color
4ava3a/t3font
4ava3a/t3geom
4ava3a/t3print
4ava3a/t3image3renderable
Usa a case 4ava3a/t3Graphics2,
Java 2D for!as
Pa*uetes 4ava3a/t3geom
=llipse2,3,ouble
=Rectangle2,3,ouble
=RoundRectangle2,3,ouble
=Arc9,3,ouble
='ines2,3,ouble
0hapes34ava
) '' Fi*. )+.++, S1apes..a/a
+ '' Demonstratin* some Ja/a+D s1apes.
3 import .a/a.a4t.5
! import .a/a.a4t.*eom.5
% import .a/a.a4t.ima*e.5
7 import .a/a6.s4in*.5
9
" pu2lic class S1apes e6ten(s JFrame 8
$
)& '' set 4in(o4:s title 2ar Strin* an( (imensions
)) pu2lic S1apes()
)+ 8
)3 super( ;Dra4in* +D s1apes; )
)!
)% setSi<e( !+%= )7& )
)7 set>isi2le( true )
)9 #
)"
)$ '' (ra4 s1apes 4it1 Ja/a+D CLI
+& pu2lic /oi( paint( 0rap1ics * )
+) 8
++ super.paint( * ) '' call superclass:s paint met1o(
+3
+! 0rap1ics+D *+( A ( 0rap1ics+D ) * '' cast * to 0rap1ics+D
+%
+7 '' (ra4 +D ellipse fille( 4it1 a 2lueK?ello4 *ra(ient
+9 *+(.setLaint( ne4 0ra(ientLaint( %= 3&= Color.@LME= 3%= )&&=
+" Color.GELLOH= true ) )
+$ *+(.fill( ne4 Ellipse+D.Dou2le( %= 3&= 7%= )&& ) )
3&
3) '' (ra4 +D rectan*le in re(
3+ *+(.setLaint( Color.IED )
33 *+(.setStroNe( ne4 @asicStroNe( )&.&f ) )
3! *+(.(ra4( ne4 Iectan*le+D.Dou2le( "&= 3&= 7%= )&& ) )
3%
37 '' (ra4 +D roun(e( rectan*le 4it1 a 2uffere( 2acN*roun(
39 @uffere(Ima*e 2uffIma*e A ne4 @uffere(Ima*e( )&= )&=
3" @uffere(Ima*e.TGLE_INT_I0@ )
3$
!& 0rap1ics+D ** A 2uffIma*e.create0rap1ics()
!) **.setColor( Color.GELLOH ) '' (ra4 in ?ello4
!+ **.fillIect( &= &= )&= )& ) '' (ra4 a fille( rectan*le
!3 **.setColor( Color.@LCCJ ) '' (ra4 in 2lacN
!! **.(ra4Iect( )= )= 7= 7 ) '' (ra4 a rectan*le
!% **.setColor( Color.@LME ) '' (ra4 in 2lue
!7 **.fillIect( )= )= 3= 3 ) '' (ra4 a fille( rectan*le
!9 **.setColor( Color.IED ) '' (ra4 in re(
!" **.fillIect( != != 3= 3 ) '' (ra4 a fille( rectan*le
!$
<se Gradient!aint to
fill shape with gradient
Fill ellipse with gradient
<se )asic0tro>e to draw
&' red5border rectangle
)uffered"mage prod#ces
image to be manip#lated
'raw te"t#re into
)uffered"mage
%& '' paint 2uffIma*e onto t1e JFrame
%) *+(.setLaint( ne4 Te6tureLaint( 2uffIma*e=
%+ ne4 Iectan*le( )&= )& ) ) )
%3 *+(.fill( ne4 Ioun(Iectan*le+D.Dou2le( )%%= 3&= 9%= )&&= %&= %& ) )
%!
%% '' (ra4 +D pieKs1ape( arc in 41ite
%7 *+(.setLaint( Color.HDITE )
%9 *+(.setStroNe( ne4 @asicStroNe( 7.&f ) )
%" *+(.(ra4( ne4 Crc+D.Dou2le( +!&= 3&= 9%= )&&= &= +9&= Crc+D.LIE ) )
%$
7& '' (ra4 +D lines in *reen an( ?ello4
7) *+(.setLaint( Color.0IEEN )
7+ *+(.(ra4( ne4 Line+D.Dou2le( 3$%= 3&= 3+&= )%& ) )
73
7! float (as1esEF A 8 )& #
7%
77 *+(.setLaint( Color.GELLOH )
79 *+(.setStroNe( ne4 @asicStroNe( != @asicStroNe.CCL_IOMND=
7" @asicStroNe.JOIN_IOMND= )&= (as1es= & ) )
7$ *+(.(ra4( ne4 Line+D.Dou2le( 3+&= 3&= 3$%= )%& ) )
9&
9) # '' en( met1o( paint
9+
<se )uffered"mage as te"t#re
for painting ro#nded rectangle
<se Arc2,3!" to
draw white5border
&' pie5shaped arc
'raw solid green line
'raw dashed yellow line
that crosses solid green line
0hapes34ava
93 '' e6ecute application
9! pu2lic static /oi( main( Strin* ar*sEF )
9% 8
97 S1apes application A ne4 S1apes()
99 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE )
9" #
9$
"& # '' en( class S1apes
) '' Fi*. )+.+3, S1apes+..a/a
+ '' Demonstratin* a *eneral pat1.
3 import .a/a.a4t.5
! import .a/a.a4t.*eom.5
% import .a/a6.s4in*.5
7
9 pu2lic class S1apes+ e6ten(s JFrame 8
"
$ '' set 4in(o4:s title 2ar Strin*= 2acN*roun( color an( (imensions
)& pu2lic S1apes+()
)) 8
)+ super( ;Dra4in* +D S1apes; )
)3
)! *etContentLane().set@acN*roun(( Color.HDITE )
)% setSi<e( !&&= !&& )
)7 set>isi2le( true )
)9 #
)"
)$ '' (ra4 *eneral pat1s
+& pu2lic /oi( paint( 0rap1ics * )
+) 8
++ super.paint( * ) '' call superclass:s paint met1o(
+3
+! int 6LointsEF A 8 %%= 79= )&$= 93= "3= %%= +9= 39= )= !3 #
+% int ?LointsEF A 8 &= 37= 37= %!= $7= 9+= $7= %!= 37= 37 #
+7
"5y coordinates that comprise star
0hapes234ava
%ine &/
%ines ;-5;.
%ines 2&5*,
+9 0rap1ics+D *+( A ( 0rap1ics+D ) *
+" 0eneralLat1 star A ne4 0eneralLat1() '' create 0eneralLat1 o2.ect
+$
3& '' set t1e initial coor(inate of t1e 0eneral Lat1
3) star.mo/eTo( 6LointsE & F= ?LointsE & F )
3+
33 '' create t1e starKKt1is (oes not (ra4 t1e star
3! for ( int count A ) count O 6Loints.len*t1 countBB )
3% star.lineTo( 6LointsE count F= ?LointsE count F )
37
39 star.closeLat1() '' close t1e s1ape
3"
3$ *+(.translate( +&&= +&& ) '' translate t1e ori*in to (+&&= +&&)
!&
!) '' rotate aroun( ori*in an( (ra4 stars in ran(om colors
!+ for ( int count A ) count OA +& countBB ) 8
!3 *+(.rotate( -at1.LI ' )&.& ) '' rotate coor(inate s?stem
!!
!% '' set ran(om (ra4in* color
!7 *+(.setColor( ne4 Color( ( int ) ( -at1.ran(om() 5 +%7 )=
!9 ( int ) ( -at1.ran(om() 5 +%7 )=
!" ( int ) ( -at1.ran(om() 5 +%7 ) ) )
!$
%& *+(.fill( star ) '' (ra4 fille( star
%) #
General!ath is a shape
constr#cted from straight
lines and comple" c#rves
Create star
'raw filled+ randomly colored
star &, times aro#nd origin
0hapes234ava