1 Introducción

2 Contextos y objetos gráficos
3 Colores
4 Fuentes
5 Pintar Lneas! "ectángulos y #$alos
% Pintar &rcos
' Pintar Polgonos and Polilneas
( )a$a2* &PI
+ ,je-.lo
Graficos Y Java 2D
1 Introducción

Capacidades gráficas de JAVA

Pintar figuras de 2D

Uso y contro coores

Uso y contro de fuentes

Java 2D API

Uso !ás sofisticado de pri!itivas de
di"u#o en 2D

Uso de for!ar y po$gonos 2D personai%ados

&eeno de figuras con coores' gradientes'
patrones y te(turas)
Jerar*u$a de agunas cases e interfaces de Java2D API)
Clases e interfaces del Java2D API que aparecen en el
paquete java.awt
Object
Color
Component
Font
FontMetrics
Graphics
Polygon
Graphics2D
interface
java.awt.Paint
interface
java.awt.Shape
interface
java.awt.Stroke
Clases e interfaces del Java2D API que aparecen en
el paquete java.awt.geom
GradientPaint
asicStroke
!e"t#rePaint
$ectang#larShape
GeneralPath
%ine&'
$o#nd$ectangle&'
(rc&'
)llipse&'
$ectangle&'
1 Introducción

+iste!a de coordenadas de JAVA

Identifica todos os puntos disponi"es de
a pantaas

,rigen de coordenadas -.'./ en a
es*uina superior i%*uierda

+iste!a de coordenadas co!puestas por
co!ponentes 0 e Y)
+iste!a de coordenadas de Java) Unidad de !edida en pi(es)
X axis
Y axis
(0, 0)
(
x
,
y
)
+
x
+
y
2 Contextos y objetos gráficos

Conte(to Grap1ics
− Per!ite pintar en a pantaa)

2 o"#eto Graphics controa e conte(to de
graficos

Controa co!o se pinta en a pantaa
− 3a Case Graphics es a"stracta

4o se puede instanciar

Contri"uye a a porta"iidad de Java
− 3a e !5todo paint de a ase Component e!pea
e o"#eto Graphics
public void paint( Graphics g )
− +e puede invocar por !edio de !5todo repaint
6 Coores

Case Color

Define os !5todos y as constantes para
!anipuar os coores)

3os coores se crean en "ase a es*ue!a
de ro#o7verde7a%u -&G8/)
Constantes de coores definidas en a case Color
Color constant Coor &G8 vaue
public final static Color ORANG
orange &**+ &,,+ ,
public final static Color !"N#
pink &**+ -.*+ -.*
public final static Color C$AN
cyan ,+ &**+ &**
public final static Color %AGN&A
magenta &**+ ,+ &**
public final static Color $''O(
yellow &**+ &**+ ,
public final static Color )'AC#
black ,+ ,+ ,
public final static Color (*"&
white &**+ &**+ &**
public final static Color GRA$
gray -&/+ -&/+ -&/
public final static Color '"G*&+GRA$
light gray -0&+ -0&+ -0&
public final static Color ,AR#+GRA$
dark gray 12+ 12+ 12
public final static Color R,
red &**+ ,+ ,
public final static Color GRN
green ,+ &**+ ,
public final static Color )'-
bl#e ,+ ,+ &**
95todos de a case Color y !5todos de reacionados de a
case Graphics
9et1od Description
Color constructors and methods
public Color( int r. int g. int b )
Creates a color based on red+ green and bl#e components e"pressed as integers
from , to &**.
p#blic Color3 float r+ float g+ float b 4
Creates a color based on red+ green and bl#e components e"pressed as floating5
point val#es from ,., to -.,.
p#blic int get$ed34
$et#rns a val#e between , and &** representing the red content.
p#blic int getGreen34
$et#rns a val#e between , and &** representing the green content.
p#blic int getl#e34
$et#rns a val#e between , and &** representing the bl#e content.
Graphics methods for manipulating Colors
p#blic Color getColor34
$et#rns a Color object representing the c#rrent color for the graphics conte"t.
p#blic void setColor3 Color c 4
Sets the c#rrent color for drawing with the graphics conte"t.
1 // Fig. 12.5: ShowColors.java
2 // Demonstrating Colors.
3 import java.awt.*;
4 import java.swing.*;
5
! p"#li$ $lass ShowColors eten%s &Frame '
(
) // $onstr"$tor sets win%ow*s title #ar string an% %imensions
+ p"#li$ ShowColors,-
1. '
11 s"per, /0sing $olors/ -;
12
13 setSi1e, 4..2 13. -;
14 set3isi#le, tr"e -;
15 4
1!
1( // %raw re$tangles an% Strings in %i55erent $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 new %rawing $olor "sing integers
24 g.setColor, new Color, 2552 .2 . - -;
25 g.5ill7e$t, 252 252 1..2 2. -;
2! g.%rawString, /C"rrent 768: / 9 g.getColor,-2 13.2 4. -;
2(
Pinta la ventana c#ando
comien6a la ejec#ci7n de
la aplicaci7n
)l m8todo setColor establece el color
de pint#ra en base a #n color $G
)l m8todo fillRect crea #n
rect9ng#lo relleno en el color de
pint#ra act#al.
)l m8todo dra/0tring escribe #n
String en el color act#al en las
coordenadas especificadas
2) // set new %rawing $olor "sing 5loats
2+ g.setColor, new Color, ...52 1..52 ...5 - -;
3. g.5ill7e$t, 252 5.2 1..2 2. -;
31 g.%rawString, /C"rrent 768: / 9 g.getColor,-2 13.2 !5 -;
32
33 // set new %rawing $olor "sing stati$ Color o#je$ts
34 g.setColor, Color.8:0; -;
35 g.5ill7e$t, 252 (52 1..2 2. -;
3! g.%rawString, /C"rrent 768: / 9 g.getColor,-2 13.2 +. -;
3(
3) // %ispla< in%ivi%"al 768 val"es
3+ Color $olor = Color.>?6;@A?;
4. g.setColor, $olor -;
41 g.5ill7e$t, 252 1..2 1..2 2. -;
42 g.%rawString, /768 val"es: / 9 $olor.get7e%,- 9 /2 / 9
43 $olor.get6reen,- 9 /2 / 9 $olor.get8l"e,-2 13.2 115 -;
44
45 4 // en% metho% paint
4!
4( // ee$"te appli$ation
4) p"#li$ stati$ voi% main, String argsBC -
4+ '
5. ShowColors appli$ation = new ShowColors,-;
51 appli$ation.setDe5a"ltCloseDperation, &Frame.;EFAGD@GC:DS; -;
52 4
53
54 4 // en% $lass ShowColors
)mpleamos las constantes de
la clase Color
1 // Fig. 12.!: ShowColors2.java
2 // Choosing $olors with &ColorChooser.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.swing.*;
!
( p"#li$ $lass ShowColors2 eten%s &Frame '
) private &8"tton $hangeColor8"tton;
+ private Color $olor = Color.:F6HAG67?I;
1. private Container $ontainer;
11
12 // set "p 60F
13 p"#li$ ShowColors2,-
14 '
15 s"per, /0sing &ColorChooser/ -;
1!
1( $ontainer = getContentJane,-;
1) $ontainer.set:a<o"t, new Flow:a<o"t,- -;
1+
2. // set "p $hangeColor8"tton an% register its event han%ler
21 $hangeColor8"tton = new &8"tton, /Change Color/ -;
22 $hangeColor8"tton.a%%?$tion:istener,
23
24 new ?$tion:istener,- ' // anon<mo"s inner $lass
25
2! // %ispla< &ColorChooser when "ser $li$Ks #"tton
2( p"#li$ voi% a$tionJer5orme%, ?$tion;vent event -
2) '
2+ $olor = &ColorChooser.showDialog,
3. ShowColors2.this2 /Choose a $olor/2 $olor -;
31
32 // set %e5a"lt $olor2 i5 no $olor is ret"rne%
33 i5 , $olor == n"ll -
34 $olor = Color.:F6HAG67?I;
35
3! // $hange $ontent pane*s #a$Kgro"n% $olor
3( $ontainer.set8a$Kgro"n%, $olor -;
3) 4
3+
4. 4 // en% anon<mo"s inner $lass
41
42 -; // en% $all to a%%?$tion:istener
43
44 $ontainer.a%%, $hangeColor8"tton -;
45
4! setSi1e, 4..2 13. -;
4( set3isi#le, tr"e -;
4)
4+ 4 // en% ShowColor2 $onstr"$tor
5.
static sho/,ialog
m#estra el c#adro de
di9logo
1ColorChooser presenta
un diáogo para
seeccionar coores
51 // ee$"te appli$ation
52 p"#li$ stati$ voi% main, String argsBC -
53 '
54 ShowColors2 appli$ation = new ShowColors2,-;
55 appli$ation.setDe5a"ltCloseDperation, &Frame.;EFAGD@GC:DS; -;
5! 4
5(
5) 4 // en% $lass ShowColors2
0ho/Colors234ava
:ig) 12); <+8 and &G8 ta"s
of t1e 1ColorChooser diaog
= :uente

Case 5ont

Contiene !5todos y constantes para e
contro de as fuentes)

2 constructor de a case :ont tiene tres
argu!entos

: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

Pará!etros de !edida y posición de
as :uentes

<eig1t > Atura

Descent -puntos por de"a#o de a inea
"ase/

Ascent -puntos por enci!a de a inea
"ase/

3eading -diferencia entre Ascent y
Descent/
Contro y !edidas
height
Xy1Õ
ascent
leading
baseline
descent
:ig) 12)11 5ont%etrics and
Graphics !et1ods for
o"taining font !etrics
9et1od Description
FontMetrics methods
public int getAscent()
$et#rns a val#e representing the ascent of a font in points.
public int get,escent()
$et#rns a val#e representing the descent of a font in points.
public int get'eading()
$et#rns a val#e representing the leading of a font in points.
public int get*eight()
$et#rns a val#e representing the height of a font in points.
Graphics methods for getting a Font"s FontMetrics
public 5ont%etrics get5ont%etrics()
$et#rns the 5ont%etrics object for the c#rrent drawing 5ont.
public 5ont%etrics get5ont%etrics( 5ont f )
$et#rns the 5ont%etrics object for the specified 5ont arg#ment.
%etrics34ava
%ine &&
%ine &;
) '' Fi*. )+.)+, -etrics..a/a
+ '' Font-etrics an( 0rap1ics
met1o(s useful for o2tainin* font
metrics.
3 import .a/a.a4t.5
! import .a/a6.s4in*.5
%
7 pu2lic class -etrics e6ten(s
JFrame 8
9
" '' set 4in(o4:s title 2ar
Strin* an( (imensions
$ pu2lic -etrics()
)& 8
)) super( ;Demonstratin*
Font-etrics; )
)+
)3 setSi<e( %)&= +)& )
)! set>isi2le( true )
)% #
)7
)9 '' (ispla? font metrics
)" pu2lic /oi( paint( 0rap1ics
* )
)$ 8
+& super.paint( * ) '' call
superclass:s paint met1o(
+)
++ *.setFont( ne4
Font( ;SansSerif;= Font.@OLD= )+
) )
+3 Font-etrics metrics A
*.*etFont-etrics()
+! *.(ra4Strin*( ;Current
font, ; B *.*etFont()= )&= !& )
Set font to SansSerif -&5point bold
Obtain 5ont%etrics
object for c#rrent font
%etrics34ava
%ines &*5&/
%ines ;,5;.
+% *.(ra4Strin*( ;Cscent, ; B
metrics.*etCscent()= )&= %% )
+7 *.(ra4Strin*( ;Descent, ;
B metrics.*etDescent()= )&= 9& )
+9 *.(ra4Strin*( ;Dei*1t, ; B
metrics.*etDei*1t()= )&= "% )
+" *.(ra4Strin*( ;Lea(in*, ;
B metrics.*etLea(in*()= )&= )&& )
+$
3& Font font A ne4
Font( ;Serif;= Font.ITCLIC= )! )
3) metrics A
*.*etFont-etrics( font )
3+ *.setFont( font )
33 *.(ra4Strin*( ;Current
font, ; B font= )&= )3& )
3! *.(ra4Strin*( ;Cscent, ; B
metrics.*etCscent()= )&= )!% )
3% *.(ra4Strin*( ;Descent, ;
B metrics.*etDescent()= )&= )7& )
37 *.(ra4Strin*( ;Dei*1t, ; B
metrics.*etDei*1t()= )&= )9% )
39 *.(ra4Strin*( ;Lea(in*, ;
B metrics.*etLea(in*()= )&= )$& )
3"
3$ # '' en( met1o( paint
!&
!) '' e6ecute application
!+ pu2lic static /oi(
main( Strin* ar*sEF )
!3 8
!! -etrics application A ne4
-etrics()
!%
application.setDefaultCloseOper
ation( JFrame.EXIT_ON_CLOSE )

!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! "ectángulos y #$alos

Case Graphics

Provee !5todos para pintar $neas'
rectánguos y óvaos

?odos o !5todos de pintar estas figuras
re*uieren e anc1o y ato *ue ocuparan

2(isten os !5todos para pintar figuras con o
sin reene -dra/7 y fill7/
95todos de a case Graphics para pintar $neas'
rectánguos y óvaos
9et1od Description
public void dra/'ine( int x8. int y8. int x2. int y2 )
'raws a line between the point 3x8+ y84 and the point 3x2+ y24.
public void dra/Rect( int x. int y. int /idth. int height )
'raws a rectangle of the specified /idth and height. !he top5left corner of the
rectangle has the coordinates 3x+ y4.
public void fillRect( int x. int y. int /idth. int height )
'raws a solid rectangle with the specified /idth and height. !he top5left
corner of the rectangle has the coordinate 3x+ y4.
public void clearRect( int x. int y. int /idth. int height )
'raws a solid rectangle with the specified /idth and height in the c#rrent
backgro#nd color. !he top5left corner of the rectangle has the coordinate 3x+ y4.
public void dra/RoundRect( int x. int y. int /idth. int height.
int arc(idth. int arc*eight )
'raws a rectangle with ro#nded corners in the c#rrent color with the specified
/idth and height. !he arc(idth and arc*eight determine the ro#nding of
the corners 3see Fig. -&.-*4.
public void fillRoundRect( int x. int y. int /idth. int height.
int arc(idth. int arc*eight )
'raws a solid rectangle with ro#nded corners in the c#rrent color with the
specified /idth and height. !he arc(idth and arc*eight determine the
ro#nding of the corners 3see Fig. -&.-*4.
95todos de a case Graphics para pintar $neas'
rectánguos y óvaos
9et1od
Description
public void dra/9,Rect( int x. int y. int /idth. int height. boolean b )
'raws a three5dimensional rectangle in the c#rrent color with the specified
/idth and height. !he top5left corner of the rectangle has the coordinates 3 x+
y4. !he rectangle appears raised when b is tr#e and lowered when b is false.
public void fill9,Rect( int x. int y. int /idth. int height. boolean b )
'raws a filled three5dimensional rectangle in the c#rrent color with the specified
/idth and height. !he top5left corner of the rectangle has the coordinates 3 x+
y4. !he rectangle appears raised when b is tr#e and lowered when b is false.
public void dra/Oval( int x. int y. int /idth. int height )
'raws an oval in the c#rrent color with the specified /idth and height. !he
bo#nding rectangle:s top5left corner is at the coordinates 3 x+ y4. !he oval to#ches
all fo#r sides of the bo#nding rectangle at the center of each side 3see
Fig. -&.-14.
public void fillOval( int x. int y. int /idth. int height )
'raws a filled oval in the c#rrent color with the specified /idth and height.
!he bo#nding rectangle:s top5left corner is at the coordinates 3 x+ y4. !he oval
to#ches all fo#r sides of the bo#nding rectangle at the center of each side 3see
Fig. -&.-14.
1 // Fig. 12.14: :ines7e$tsDvals.java
2 // Drawing lines2 re$tangles an% ovals.
3 import java.awt.*;
4 import java.swing.*;
5
! p"#li$ $lass :ines7e$tsDvals eten%s &Frame '
(
) // set win%ow*s title #ar String an% %imensions
+ p"#li$ :ines7e$tsDvals,-
1. '
11 s"per, /Drawing lines2 re$tangles an% ovals/ -;
12
13 setSi1e, 4..2 1!5 -;
14 set3isi#le, tr"e -;
15 4
1!
1( // %ispla< vario"s lines2 re$tangles an% ovals
1) p"#li$ voi% paint, 6raphi$s g -
1+ '
2. s"per.paint, g -; // $all s"per$lass*s paint metho%
21
22 g.setColor, Color.7;D -;
23 g.%raw:ine, 52 3.2 35.2 3. -;
24
25 g.setColor, Color.8:0; -;
2! g.%raw7e$t, 52 4.2 +.2 55 -;
2( g.5ill7e$t, 1..2 4.2 +.2 55 -;
2)
2+ g.setColor, Color.CI?@ -;
3. g.5ill7o"n%7e$t, 1+52 4.2 +.2 552 5.2 5. -;
31 g.%raw7o"n%7e$t, 2+.2 4.2 +.2 552 2.2 2. -;
32
33 g.setColor, Color.I;::DL -;
34 g.%raw3D7e$t, 52 1..2 +.2 552 tr"e -;
35 g.5ill3D7e$t, 1..2 1..2 +.2 552 5alse -;
3!
3( g.setColor, Color.>?6;@A? -;
3) g.%rawDval, 1+52 1..2 +.2 55 -;
3+ g.5illDval, 2+.2 1..2 +.2 55 -;
4.
41 4 // en% metho% paint
42
43 // ee$"te appli$ation
44 p"#li$ stati$ voi% main, String argsBC -
45 '
4! :ines7e$tsDvals appli$ation = new :ines7e$tsDvals,-;
4( appli$ation.setDe5a"ltCloseDperation, &Frame.;EFAGD@GC:DS; -;
4) 4
4+
5. 4 // en% $lass :ines7e$tsDvals
'raw filled ro#nded rectangle
'raw 3non5filled4 ro#nded rectangle
'raw ;' rectangle
'raw filled ;' rectangle
'raw oval
'raw filled oval
Atura y anc1ura de arco necesario para construir
&ounded&ectange
(x, y)
arc height
arc width
width
height
9edidas para construir un óvao en "ase a rectánguo *ue o
contiene
(
x
,
y
)
height
width
@ Pintar Arcos

Arco

Porción de un óvao

+e !iden en grados

8arre -Sweeps) e nA!ero de grados *ue
indi*ue e ánguo de arco

+Beep e!pie%a en e inicio de !edida de
os ánguos

8arre en sentido contrario a as agu#as de
reo# si e ánguo es positivo

8arre en sentido de as agu#as de reo# para
ánguos negativos)
Cnguos positivos y negativos
90°
0° 180°
270°
90°
0° 180°
270°
Positie angles !egatie angles
95todos de a case Graphics para e pintado de arcos
9et1od Description
public void dra/Arc( int x. int y. int /idth. int height. int startAngle.
int arcAngle )
'raws an arc relative to the bo#nding rectangle:s top5left coordinates #x y$ with
the specified /idth and height. !he arc segment is drawn starting at
startAngle and sweeps arcAngle degrees.
public void fillArc( int x. int y. int /idth. int height. int startAngle.
int arcAngle )
'raws a solid arc 3i.e.+ a sector4 relative to the bo#nding rectangle:s top5left
coordinates #x y$ with the specified /idth and height. !he arc segment is
drawn starting at startAngle and sweeps arcAngle degrees.
,ra/Arcs34ava
%ines &25&1
) '' Fi*. )+.)$, Dra4Crcs..a/a
+ '' Dra4in* arcs.
3 import .a/a.a4t.5
! import .a/a6.s4in*.5
%
7 pu2lic class Dra4Crcs e6ten(s JFrame 8
9
" '' set 4in(o4:s title 2ar Strin* an( (imensions
$ pu2lic Dra4Crcs()
)& 8
)) super( ;Dra4in* Crcs; )
)+
)3 setSi<e( 3&&= )9& )
)! set>isi2le( true )
)% #
)7
)9 '' (ra4 rectan*les an( arcs
)" pu2lic /oi( paint( 0rap1ics * )
)$ 8
+& super.paint( * ) '' call superclass:s paint met1o(
+)
++ '' start at & an( s4eep 37& (e*rees
+3 *.setColor( Color.GELLOH )
+! *.(ra4Iect( )%= 3%= "&= "& )
+% *.setColor( Color.@LCCJ )
+7 *.(ra4Crc( )%= 3%= "&= "&= &= 37& )
'raw first arc that
sweeps ;1, degrees and
is contained in rectangle
+9
+" '' start at & an( s4eep ))& (e*rees
+$ *.setColor( Color.GELLOH )
3& *.(ra4Iect( )&&= 3%= "&= "& )
3) *.setColor( Color.@LCCJ )
3+ *.(ra4Crc( )&&= 3%= "&= "&= &= ))& )
33
3! '' start at & an( s4eep K+9& (e*rees
3% *.setColor( Color.GELLOH )
37 *.(ra4Iect( )"%= 3%= "&= "& )
39 *.setColor( Color.@LCCJ )
3" *.(ra4Crc( )"%= 3%= "&= "&= &= K+9& )
3$
!& '' start at & an( s4eep 37& (e*rees
!) *.fillCrc( )%= )+&= "&= !&= &= 37& )
!+
!3 '' start at +9& an( s4eep K$& (e*rees
!! *.fillCrc( )&&= )+&= "&= !&= +9&= K$& )
!%
!7 '' start at & an( s4eep K+9& (e*rees
!9 *.fillCrc( )"%= )+&= "&= !&= &= K+9& )
!"
!$ # '' en( met1o( paint
%&
'raw second arc that
sweeps --, degrees and
is contained in rectangle
'raw third arc that
sweeps 5&., degrees and
is contained in rectangle
'raw fo#rth arc that is filled+ has starting
angle , and sweeps ;1, degrees
'raw fifth arc that is filled+ has starting
angle &., and sweeps 50, degrees
'raw si"th arc that is filled+ has starting
angle , and sweeps 5&., degrees
%) '' e6ecute application
%+ pu2lic static /oi( main( Strin* ar*sEF )
%3 8
%! Dra4Crcs application A ne4 Dra4Crcs()
%% application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE )
%7 #
%9
%" # '' en( class Dra4Crcs
; Pintar Po$gonos y Poi$neas

Cases !olygon

Po$gonos

:iguras de varios ados

Poi$neas

+eries de puntos conectados
95todos Graphics para pintar poigonos y !5todos de a
case !olygon
9et1od Description
Graphics methods for drawing pol!gons
public void dra/!olygon( int x!oints:;. int y!oints:;. int points )
'raws a polygon. !he %5coordinate of each point is specified in the x!oints
array and the !5coordinate of each point is specified in the y!oints array. !he
last arg#ment specifies the n#mber of points. !his method draws a closed
polygon. =f the last point is different from the first point+ the polygon is closed
by a line that connects the last point to the first point.
public void dra/!olyline( int x!oints:;. int y!oints:;. int points )
'raws a se>#ence of connected lines. !he %5coordinate of each point is specified
in the x!oints array and the !5coordinate of each point is specified in the
y!oints array. !he last arg#ment specifies the n#mber of points. =f the last
point is different from the first point+ the polyline is not closed.
public void dra/!olygon( !olygon p )
'raws the specified polygon.
public void fill!olygon( int x!oints:;. int y!oints:;. int points )
'raws a solid polygon. !he %5coordinate of each point is specified in the
x!oints array and the !5coordinate of each point is specified in the y!oints
array. !he last arg#ment specifies the n#mber of points. !his method draws a
closed polygon. =f the last point is different from the first point+ the polygon is
closed by a line that connects the last point to the first point.
public void fill!olygon( !olygon p )
'raws the specified solid polygon. !he polygon is closed.
95todos Graphics para pintar poigonos y !5todos de a
case !olygon
9et1od
Description
!olygon constructors and methods
public !olygon()
Constr#cts a new polygon object. !he polygon does not contain any points.
public !olygon( int x<alues:;. int y<alues:;. int numberOf!oints )
Constr#cts a new polygon object. !he polygon has numberOf!oints sides+
with each point consisting of an %5coordinate from x<alues and a !5coordinate
from y<alues .
public void add!oint( int x. int y )
(dds pairs of %& and !5coordinates to the !olygon .
) '' Fi*. )+.+), Dra4Lol?*ons..a/a
+ '' Dra4in* pol?*ons.
3 import .a/a.a4t.5
! import .a/a6.s4in*.5
%
7 pu2lic class Dra4Lol?*ons e6ten(s JFrame 8
9
" '' set 4in(o4:s title 2ar Strin* an( (imensions
$ pu2lic Dra4Lol?*ons()
)& 8
)) super( ;Dra4in* Lol?*ons; )
)+
)3 setSi<e( +9%= +3& )
)! set>isi2le( true )
)% #
)7
)9 '' (ra4 pol?*ons an( pol?lines
)" pu2lic /oi( paint( 0rap1ics * )
)$ 8
+& super.paint( * ) '' call superclass:s paint met1o(
+)
++ int 6>aluesEF A 8 +&= !&= %&= 3&= +&= )% #
+3 int ?>aluesEF A 8 %&= %&= 7&= "&= "&= 7& #
+! Lol?*on pol?*on) A ne4 Lol?*on( 6>alues= ?>alues= 7 )
+%
+7 *.(ra4Lol?*on( pol?*on) )
+9
int arrays specifying
!olygon polygon8 points
'raw polygon8 to screen
+" int 6>alues+EF A 8 9&= $&= )&&= "&= 9&= 7%= 7& #
+$ int ?>alues+EF A 8 )&&= )&&= ))&= ))&= )3&= ))&= $& #
3&
3) *.(ra4Lol?line( 6>alues+= ?>alues+= 9 )
3+
33 int 6>alues3EF A 8 )+&= )!&= )%&= )$& #
3! int ?>alues3EF A 8 !&= 9&= "&= 7& #
3%
37 *.fillLol?*on( 6>alues3= ?>alues3= ! )
39
3" Lol?*on pol?*on+ A ne4 Lol?*on()
3$ pol?*on+.a((Loint( )7%= )3% )
!& pol?*on+.a((Loint( )9%= )%& )
!) pol?*on+.a((Loint( +9&= +&& )
!+ pol?*on+.a((Loint( +&&= ++& )
!3 pol?*on+.a((Loint( )3&= )"& )
!!
!% *.fillLol?*on( pol?*on+ )
!7
!9 # '' en( met1o( paint
!"
!$ '' e6ecute application
%& pu2lic static /oi( main( Strin* ar*sEF )
%) 8
%+ Dra4Lol?*ons application A ne4 Dra4Lol?*ons()
%3 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE )
%! #
%%
%7 # '' en( class Dra4Lol?*ons
int arrays specifying
!olyline points
'raw !olyline to screen
Specify points and draw 3filled4
!olygon to screen
Method add!oint adds pairs of
"5y coordinates to a !olygon
D Java2D API

Java 2D API
− Proporciona capacidades gráficas avan%as 2D

4ava3a/t

4ava3a/t3image

4ava3a/t3color

4ava3a/t3font

4ava3a/t3geom

4ava3a/t3print

4ava3a/t3image3renderable
− Usa a case 4ava3a/t3Graphics2,

2(tiende a case 4ava3a/t3Graphics
12)D Java2D API

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

Sign up to vote on this title
UsefulNot useful