Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Java 2 D
Java 2 D
1 2 3 4 5 % ' ( + Introduccin Contextos y objetos grficos Colores Fuentes Pintar L neas! "ectngulos y #$alos Pintar &rcos Pintar Pol gonos and Polil neas )a$a2* &PI ,je-.lo
1 Introduccin
Pintar figuras de 2D Uso y contro co ores Uso y contro de fuentes Uso !s sofisticado de pri!itivas de di"u#o en 2D
Uso de for!ar y po $gonos 2D persona i%ados &e eno de figuras con co ores' gradientes' patrones y te(turas)
Java 2D API
1 Introduccin
Identifica todos os puntos disponi" es de a panta as ,rigen de coordenadas -.'./ en a es*uina superior i%*uierda +iste!a de coordenadas co!puestas por co!ponentes 0 e Y)
(0, 0)
x +
X a xis
(x , y )
y +
Y a xis
Conte(to Grap1ics
Per!ite pintar en a panta a) 2 o"#eto Graphics contro a e conte(to de graficos
3a e !5todo paint de a ase Component e!p ea e o"#eto Graphics +e puede invocar por !edio de !5todo repaint
6 Co ores
C ase Color
Define os !5todos y as constantes para !anipu ar os co ores) 3os co ores se crean en "ase a es*ue!a de ro#o7verde7a%u -&G8/)
Color constant public final static Color ORANG public final static Color !"N# public final static Color C$AN public final static Color %AG N&A public final static Color $ ''O( public final static Color )'AC# public final static Color (*"& public final static Color GRA$ public final static Color '"G*&+GRA$ public final static Color ,AR#+GRA$ public final static Color R , public final static Color GR public final static Color )'N
Co or
&G8 va ue
orange pink cyan magenta yellow black white gray light gray dark gray red green bl#e
&**+ &,,+ , &**+ -.*+ -.* ,+ &**+ &** &**+ ,+ &** &**+ &**+ , ,+ ,+ , &**+ &**+ &** -&/+ -&/+ -&/ -0&+ -0&+ -0& 12+ 12+ 12 &**+ ,+ , ,+ &**+ , ,+ ,+ &**
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 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 2 3 4 5 ! ( ) + 1. 11 12 13 14 15 1! 1( 1) 1+ 2. 21 22 23 24 25 2! 2(
// Fig. 12.5: ShowColors.java // Demonstrating Colors. import java.awt.*; import java .swing.*; p"#li$ $lass ShowColors e ten%s &Frame ' // $onstr"$tor sets win%ow*s title #ar string an% %imensions p"#li$ ShowColors,' Pinta la ventana c#ando s"per, /0sing $olors/ -; setSi1e, 4..2 13. -; set3isi#le, tr"e -; 4
// %raw re$tangles an% Strings in %i55erent $olors p"#li$ voi% paint, 6raphi$s g ' // $all s"per$lass*s paint metho% )l m8todo fillRect crea #n s"per.paint, g -; rect9ng#lo relleno en el color de // set new %rawing $olor "sing integers g.setColor, new Color, 2552 .2 . - -; g.5ill7e$t, 252 252 1..2 2. -; g.%rawString, /C"rrent 768: / 9 g.getColor,-2 13.2 4. -;
pint#ra act#al.
2) 2+ 3. 31 32 33 34 35 3! 3( 3) 3+ 4. 41 42 43 44 45 4! 4( 4) 4+ 5. 51 52 53 54
// set new %rawing $olor "sing 5loats g.setColor, new Color, ...52 1..52 ...5 - -; g.5ill7e$t, 252 5.2 1..2 2. -; g.%rawString, /C"rrent 768: / 9 g.getColor,-2 13.2 !5 -; // set new %rawing $olor "sing stati$ Color o#je$ts g.setColor, Color.8:0; -; g.5ill7e$t, 252 (52 1..2 2. -; g.%rawString, /C"rrent 768: / 9 g.getColor,-2 13.2 +. -; // %ispla< in%ivi%"al 768 val"es Color $olor = Color.>?6;@A?; g.setColor, $olor -; g.5ill7e$t, 252 1..2 1..2 2. -; g.%rawString, /768 val"es: / 9 $olor.get7e%,- 9 /2 / 9 $olor.get6reen,- 9 /2 / 9 $olor.get8l"e,-2 13.2 115 -; 4 // en% metho% paint // e e$"te appli$ation p"#li$ stati$ voi% main, String argsBC ' ShowColors appli$ation = new ShowColors,-; appli$ation.setDe5a"ltCloseDperation, &Frame.;EFAGD@GC:DS; -; 4 4 // en% $lass ShowColors
1 2 3 4 5 ! ( ) + 1. 11 12 13 14 15 1! 1( 1) 1+ 2. 21 22 23
// Fig. 12.!: ShowColors2.java // Choosing $olors with &ColorChooser. import java.awt.*; import java.awt.event.*; import java .swing.*; p"#li$ $lass ShowColors2 e ten%s &Frame ' private &8"tton $hangeColor8"tton; private Color $olor = Color.:F6HAG67?I; private Container $ontainer; // set "p 60F p"#li$ ShowColors2,' s"per, /0sing &ColorChooser/ -; $ontainer = getContentJane,-; $ontainer.set:a<o"t, new Flow:a<o"t,- -; // set "p $hangeColor8"tton an% register its event han%ler $hangeColor8"tton = new &8"tton, /Change Color/ -; $hangeColor8"tton.a%%?$tion:istener,
24 25 2! 2( 2) 2+ 3. 31 32 33 34 35 3! 3( 3) 3+ 4. 41 42 43 44 45 4! 4( 4) 4+ 5.
// %ispla< &ColorChooser when "ser $li$Ks #"tton 1ColorChooser presenta p"#li$ voi% a$tionJer5orme%, ?$tion;vent event un di ogo para ' se eccionar co ores $olor = &ColorChooser.showDialog, ShowColors2.this2 /Choose a $olor/2 $olor -; // set %e5a"lt $olor2 i5 no $olor is ret"rne% i5 , $olor == n"ll $olor = Color.:F6HAG67?I; // $hange $ontent pane*s #a$Kgro"n% $olor $ontainer.set8a$Kgro"n%, $olor -; 4 4 // en% anon<mo"s inner $lass -; // en% $all to a%%?$tion:istener $ontainer.a%%, $hangeColor8"tton -; setSi1e, 4..2 13. -; set3isi#le, tr"e -; 4 // en% ShowColor2 $onstr"$tor
static sho/,ialog
51 52 53 54 55 5! 5( 5)
// e e$"te appli$ation p"#li$ stati$ voi% main, String argsBC ' ShowColors2 appli$ation = new ShowColors2,-; appli$ation.setDe5a"ltCloseDperation, &Frame.;EFAGD@GC:DS; -; 4 4 // en% $lass ShowColors2
0ho/Colors234ava
= :uente
C ase 5ont
Contiene !5todos y constantes para e contro de as fuentes) 2 constructor de a c ase :ont tiene tres argu!entos
:ont na!e
%onospaced' 0ans0erif' 0erif' etc)
:ont sty e
5ont3!'A"N' 5ont3"&A'"C y 5ont3)O',
:ont si%e
9edido en puntos
( constant representing a plain font style. ( constant representing a bold font style. ( constant representing an italic font style. Creates a 5ont object with the specified font+ style and si6e. $et#rns an integer val#e indicating the c#rrent font style.
get0i6e()
true if the font is plain. true if the font is bold. true if the font is italic.
Sets the c#rrent font to the font+ style and si6e specified by the object reference f.
5ont
1 2 3 4 5 ! ( ) + 1. 11 12 13 14 15 1! 1( 1) 1+ 2. 21 22 23 24 25
// Fig. 12.+: Fonts.java // 0sing 5onts. import java.awt.*; import java .swing.*; p"#li$ $lass Fonts e ten%s &Frame ' // set win%ow*s title #ar an% %imensions p"#li$ Fonts,' s"per, /0sing 5onts/ -; setSi1e, 42.2 125 -; set3isi#le, tr"e -; 4 // %ispla< Strings in %i55erent 5onts an% $olors p"#li$ voi% paint, 6raphi$s g )l m8todo set5ont ' // $all s"per$lass*s paint metho% s"per.paint, g -;
// set 5ont to Seri5 ,Aimes-2 #ol%2 12pt an% %raw a string g.setFont, new Font, /Seri5/2 Font.8D:D2 12 - -; g.%rawString, /Seri5 12 point #ol%./2 2.2 5. -; )scribe
// set 5ont to >onospa$e% ,Co"rier-2 itali$2 24pt an% %raw a g.setFont, new Font, />onospa$e%/2 Font.FA?:FC2 24 - -; g.%rawString, />onospa$e% 24 point itali$./2 2.2 (. -; // set 5ont to SansSeri5 ,Helveti$a-2 plain2 14pt an% %raw a g.setFont, new Font, /SansSeri5/2 Font.J:?F@2 14 - -; g.%rawString, /SansSeri5 14 point plain./2 2.2 +. -; // set 5ont to Seri5 ,Aimes-2 #ol%/itali$2 1)pt an% %raw a g.setColor, Color.7;D -; g.setFont, new Font, /Seri5/2 Font.8D:D 9 Font.FA?:FC2 1) - -; g.%rawString, g.getFont,-.get@ame,- 9 / / 9 g.getFont,-.getSi1e,- 9 / point #ol% itali$./2 2.2 11. -; 4 // en% metho% paint // e e$"te appli$ation p"#li$ stati$ voi% main, String argsBC ' Fonts appli$ation = new Fonts,-; application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) # # '' en( class Fonts
Contro de fuentes
<eig1t > A tura 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
he ight
Xy1
:ig) 12)11 5ont%etrics and Graphics !et1ods for o"taining font !etrics
9et1od Description FontMetrics methods public int getAscent()
'' Fi*. )+.)+, -etrics..a/a + '' Font-etrics an( 0rap1ics %etrics34ava met1o(s useful for o2tainin* font %ine && %ine &; metrics. 3 import .a/a.a4t.5 ! import .a/a6.s4in*.5 % Set font to SansSerif -&5point bold 7 pu2lic class -etrics e6ten(s Obtain 5ont%etrics JFrame 8 object for c#rrent font 9 " '' set 4in(o4:s title 2ar Strin* an( (imensions $ pu2lic -etrics()
+%
*.(ra4Strin*( ;Cscent, ; B metrics.*etCscent()= )&= %% ) $epeat same process for %ines &*5&/ +7 *.(ra4Strin*( ;Descent,Serif ; -25point italic font B metrics.*etDescent()= )&= 9& ) %ines ;,5;. +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 )
%etrics34ava
C ase Graphics
'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 3 x+ 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.
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 2 3 4 5 ! ( ) + 1. 11 12 13 14 15 1! 1( 1) 1+ 2. 21 22 23 24 25 2! 2(
// Fig. 12.14: :ines7e$tsDvals.java // Drawing lines2 re$tangles an% ovals. import java.awt.*; import java .swing.*; p"#li$ $lass :ines7e$tsDvals e ten%s &Frame ' // set win%ow*s title #ar String an% %imensions p"#li$ :ines7e$tsDvals,' s"per, /Drawing lines2 re$tangles an% ovals/ -; setSi1e, 4..2 1!5 -; set3isi#le, tr"e -; 4 // %ispla< vario"s lines2 re$tangles an% ovals p"#li$ voi% paint, 6raphi$s g ' s"per.paint, g -; // $all s"per$lass*s paint metho% g.setColor, Color.7;D -; g.%raw:ine, 52 3.2 35.2 3. -; g.setColor, Color.8:0; -; g.%raw7e$t, 52 4.2 +.2 55 -; g.5ill7e$t, 1..2 4.2 +.2 55 -;
2) 2+ 3. 31 32 33 34 35 3! 3( 3) 3+ 4. 41 42 43 44 45 4! 4( 4) 4+ 5.
g.setColor, Color.CI?@ -; g.5ill7o"n%7e$t, 1+52 4.2 +.2 552 5.2 5. -; g.%raw7o"n%7e$t, 2+.2 4.2 +.2 552 2.2 2. -; g.setColor, Color.I;::DL -; g.%raw3D7e$t, 52 1..2 +.2 552 tr"e -; g.5ill3D7e$t, 1..2 1..2 +.2 552 5alse -; g.setColor, Color.>?6;@A? -; g.%rawDval, 1+52 1..2 +.2 55 -; g.5illDval, 2+.2 1..2 +.2 55 -; 4 // en% metho% paint
'raw 3non5filled4 ro#nded rectangle 'raw ;' rectangle 'raw filled ;' rectangle 'raw oval 'raw filled oval
// e e$"te appli$ation p"#li$ stati$ voi% main, String argsBC ' :ines7e$tsDvals appli$ation = new :ines7e$tsDvals,-; appli$ation.setDe5a"ltCloseDperation, &Frame.;EFAGD@GC:DS; -; 4 4 // en% $lass :ines7e$tsDvals
width
(x , y)
height
width
@ Pintar Arcos
Arco
Porcin de un va o +e !iden en grados 8arre -Sweeps) e nA!ero de grados *ue indi*ue e ngu o de arco +Beep e!pie%a en e inicio de !edida de os ngu os
8arre en sentido contrario a as agu#as de re o# si e ngu o es positivo 8arre en sentido de as agu#as de re o# para ngu os negativos)
Positi e angles 90
!egati e angles 90
180
180
270
270
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.
'' Fi*. )+.)$, Dra4Crcs..a/a '' Dra4in* arcs. import .a/a.a4t.5 import .a/a6.s4in*.5 pu2lic class Dra4Crcs e6ten(s JFrame 8 '' set 4in(o4:s title 2ar Strin* an( (imensions pu2lic Dra4Crcs() 8 super( ;Dra4in* Crcs; ) setSi<e( 3&&= )9& ) set>isi2le( true ) # '' (ra4 rectan*les an( arcs pu2lic /oi( paint( 0rap1ics * ) 8 super.paint( * ) '' call superclass:s paint met1o( '' start at & an( s4eep 37& (e*rees *.setColor( Color.GELLOH ) *.(ra4Iect( )%= 3%= "&= "& ) *.setColor( Color.@LCCJ ) *.(ra4Crc( )%= 3%= "&= "&= &= 37& )
'raw first arc that sweeps ;1, degrees and is contained in rectangle
'' start at & an( s4eep ))& (e*rees *.setColor( Color.GELLOH ) *.(ra4Iect( )&&= 3%= "&= "& ) *.setColor( Color.@LCCJ ) *.(ra4Crc( )&&= 3%= "&= "&= &= ))& ) '' start at & an( s4eep K+9& (e*rees *.setColor( Color.GELLOH ) *.(ra4Iect( )"%= 3%= "&= "& ) *.setColor( Color.@LCCJ ) *.(ra4Crc( )"%= 3%= "&= "&= &= K+9& ) '' start at & an( s4eep 37& (e*rees *.fillCrc( )%= )+&= "&= !&= &= 37& ) '' start at +9& an( s4eep K$& (e*rees *.fillCrc( )&&= )+&= "&= !&= +9&= K$& ) '' start at & an( s4eep K+9& (e*rees *.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
%) %+ %3 %! %% %7 %9 %"
'' e6ecute application pu2lic static /oi( main( Strin* ar*sEF ) 8 Dra4Crcs application A ne4 Dra4Crcs() application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) # # '' en( class Dra4Crcs
C ases !olygon
Po $gonos
Po i $neas
'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 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 )
Constr#cts a new polygon object. !he polygon does not contain any points.
!olygon( int x<alues:;. int y<alues:;. int numberOf!oints ) numberOf!oints sides+ x<alues and a !5coordinate
Constr#cts a new polygon object. !he polygon has with each point consisting of an %5coordinate from from y<alues .
public void add!oint( int x. int y )
!olygon .
'' Fi*. )+.+), Dra4Lol?*ons..a/a '' Dra4in* pol?*ons. import .a/a.a4t.5 import .a/a6.s4in*.5 pu2lic class Dra4Lol?*ons e6ten(s JFrame 8 '' set 4in(o4:s title 2ar Strin* an( (imensions pu2lic Dra4Lol?*ons() 8 super( ;Dra4in* Lol?*ons; ) setSi<e( +9%= +3& ) set>isi2le( true ) # '' (ra4 pol?*ons an( pol?lines pu2lic /oi( paint( 0rap1ics * ) 8 super.paint( * ) '' call superclass:s paint met1o( int 6>aluesEF A 8 +&= !&= %&= 3&= +&= )% # int ?>aluesEF A 8 %&= %&= 7&= "&= "&= 7& # Lol?*on pol?*on) A ne4 Lol?*on( 6>alues= ?>alues= 7 ) *.(ra4Lol?*on( pol?*on) )
int 6>alues+EF A 8 9&= $&= )&&= "&= 9&= 7%= 7& # int ?>alues+EF A 8 )&&= )&&= ))&= ))&= )3&= ))&= $& # *.(ra4Lol?line( 6>alues+= ?>alues+= 9 ) int 6>alues3EF A 8 )+&= )!&= )%&= )$& # int ?>alues3EF A 8 !&= 9&= "&= 7& # *.fillLol?*on( 6>alues3= ?>alues3= ! ) Lol?*on pol?*on+ A ne4 Lol?*on() pol?*on+.a((Loint( )7%= )3% ) pol?*on+.a((Loint( )9%= )%& ) pol?*on+.a((Loint( +9&= +&& ) pol?*on+.a((Loint( +&&= ++& ) pol?*on+.a((Loint( )3&= )"& ) *.fillLol?*on( pol?*on+ ) # '' en( met1o( paint '' e6ecute application pu2lic static /oi( main( Strin* ar*sEF ) 8 Dra4Lol?*ons application A ne4 Dra4Lol?*ons() application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) # # '' en( class Dra4Lol?*ons
int arrays specifying !olyline points 'raw !olyline to screen Specify points and draw 3filled4 !olygon to screen
D Java2D API
Java 2D API
Java 2D for!as
Pa*uetes 4ava3a/t3geom
= llipse2,3,ouble = Rectangle2,3,ouble = RoundRectangle2,3,ouble = Arc9,3,ouble = 'ines2,3,ouble
'' Fi*. )+.++, S1apes..a/a '' Demonstratin* some Ja/a+D s1apes. import .a/a.a4t.5 import .a/a.a4t.*eom.5 import .a/a.a4t.ima*e.5 import .a/a6.s4in*.5 pu2lic class S1apes e6ten(s JFrame 8 '' set 4in(o4:s title 2ar Strin* an( (imensions pu2lic S1apes() 8 super( ;Dra4in* +D s1apes; ) setSi<e( !+%= )7& ) set>isi2le( true ) # '' (ra4 s1apes 4it1 Ja/a+D CLI pu2lic /oi( paint( 0rap1ics * ) 8 super.paint( * ) '' call superclass:s paint met1o( 0rap1ics+D *+( A ( 0rap1ics+D ) * '' cast * to 0rap1ics+D
0hapes34ava
'' (ra4 +D ellipse fille( 4it1 a 2lueK?ello4 *ra(ient *+(.setLaint( ne4 0ra(ientLaint( %= 3&= Color.@LME= 3%= )&&= Color.GELLOH= true ) ) *+(.fill( ne4 Ellipse+D.Dou2le( %= 3&= 7%= )&& ) ) '' (ra4 +D rectan*le in re( *+(.setLaint( Color.IED ) *+(.setStroNe( ne4 @asicStroNe( )&.&f ) ) *+(.(ra4( ne4 Iectan*le+D.Dou2le( "&= 3&= 7%= )&& ) ) '' (ra4 +D roun(e( rectan*le 4it1 a 2uffere( 2acN*roun( @uffere(Ima*e 2uffIma*e A ne4 @uffere(Ima*e( )&= )&= @uffere(Ima*e.TGLE_INT_I0@ ) 0rap1ics+D ** A 2uffIma*e.create0rap1ics() **.setColor( Color.GELLOH ) '' (ra4 in ?ello4 **.fillIect( &= &= )&= )& ) '' (ra4 a fille( rectan*le **.setColor( Color.@LCCJ ) '' (ra4 in 2lacN **.(ra4Iect( )= )= 7= 7 ) '' (ra4 a rectan*le **.setColor( Color.@LME ) '' (ra4 in 2lue **.fillIect( )= )= 3= 3 ) '' (ra4 a fille( rectan*le **.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
'' paint 2uffIma*e onto t1e JFrame *+(.setLaint( ne4 Te6tureLaint( 2uffIma*e= ne4 Iectan*le( )&= )& ) ) ) *+(.fill( ne4 Ioun(Iectan*le+D.Dou2le( )%%= 3&= 9%= )&&= %&= %& ) ) '' (ra4 +D pieKs1ape( arc in 41ite *+(.setLaint( Color.HDITE ) *+(.setStroNe( ne4 @asicStroNe( 7.&f ) ) *+(.(ra4( ne4 Crc+D.Dou2le( +!&= 3&= 9%= )&&= &= +9&= Crc+D.LIE ) ) '' (ra4 +D lines in *reen an( ?ello4 *+(.setLaint( Color.0IEEN ) *+(.(ra4( ne4 Line+D.Dou2le( 3$%= 3&= 3+&= )%& ) ) float (as1esEF A 8 )& # *+(.setLaint( Color.GELLOH ) *+(.setStroNe( ne4 @asicStroNe( != @asicStroNe.CCL_IOMND= @asicStroNe.JOIN_IOMND= )&= (as1es= & ) ) *+(.(ra4( ne4 Line+D.Dou2le( 3+&= 3&= 3$%= )%& ) ) # '' en( met1o( paint
<se )uffered"mage as te"t#re for painting ro#nded rectangle <se Arc2,3!" to draw white5border &' pie5shaped arc
93 9! 9% 97 99 9" 9$ "&
'' e6ecute application pu2lic static /oi( main( Strin* ar*sEF ) 8 S1apes application A ne4 S1apes() application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) # # '' en( class S1apes
0hapes34ava
'' Fi*. )+.+3, S1apes+..a/a '' Demonstratin* a *eneral pat1. import .a/a.a4t.5 import .a/a.a4t.*eom.5 import .a/a6.s4in*.5 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; ) *etContentLane().set@acN*roun(( Color.HDITE ) setSi<e( !&&= !&& ) set>isi2le( true ) # '' (ra4 *eneral pat1s pu2lic /oi( paint( 0rap1ics * ) 8 super.paint( * ) '' call superclass:s paint met1o( int 6LointsEF A 8 %%= 79= )&$= 93= "3= %%= +9= 39= )= !3 # int ?LointsEF A 8 &= 37= 37= %!= $7= 9+= $7= %!= 37= 37 #
0rap1ics+D *+( A ( 0rap1ics+D ) * 0eneralLat1 star A ne4 0eneralLat1() '' create 0eneralLat1 o2.ect '' set t1e initial coor(inate of t1e 0eneral Lat1 star.mo/eTo( 6LointsE & F= ?LointsE & F ) '' create t1e starKKt1is (oes not (ra4 t1e star for ( int count A ) count O 6Loints.len*t1 countBB ) star.lineTo( 6LointsE count F= ?LointsE count F ) star.closeLat1() '' close t1e s1ape *+(.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 *+(.rotate( -at1.LI ' )&.& ) '' rotate coor(inate s?stem '' set ran(om (ra4in* color *+(.setColor( ne4 Color( ( int ) ( -at1.ran(om() 5 +%7 )= ( 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 0hapes234ava %ine &/ Create star %ines ;-5;. %ines 2&5*,
0hapes234ava