Professional Documents
Culture Documents
Thng3/2010
C08CNTT
2
haAWTtrongjava
http://www.mediafire.com/?hz7rf8jbvvnlnht
Nidung:
LpGraphics
Vhnhhc
Vch
Vnh
Chv
LpColor
Vngct
Cclptinchkhc
LpGraphics2D<Java1.4>
Vhnhhc
Ntv
Chv
Tloang
Vnhtrongsut
BinitatrongJava2D
LpGraphics
Khiqut:
Nuchmunvli1phntasdngphngthc:
repaint(intx,inty,intwidth,intheight)
LpGraphics
1.Vhnhhc:
Gm:
Vhnhchnht
VElip,vngtrn
Vagic
Vngthng
Vhnhchnhttrngc
Vcungtrn
LpGraphics
1.Vhnhhc:
Giithiuhta:
Vhnhchnht:
drawRect(intx,inty,intwidth,intheight)
fillRect(intx,inty,intwidth,intheight)
clearRect(intx,inty,intwidth,intheight)
draw
(x,y)
(Width
)
fill
(Height)
VD:
publicvoidpaint(Graphicsg)
{
g.drawRect(20,30,50,50);
}
(0,0)
(800,0)
Mnhnh
Y
(0,600)
(800,600)
LpGraphics
1.Vhnhhc:
Vngthng:
drawLine(intx1,inty1,intx2,inty2)
Vvngtrn,elip:
drawOval(intx,inty,intwidth,intheight)
fillOval(intx,inty,intwidth,intheight)
+ngtrn:width=height
(x,y)
(Height)
draw
(Width)
fill
(x1,y1)
(x2,y2)
LpGraphics
1.Vhnhhc:
Vhnhchnhttrngc:
drawRoundRect(intx,inty,intwidth,intheight,intarcWidth,intarcHeight)
fillRoundRect(intx,inty,intwidth,intheight,intarcWidth,intarcHeight)
(arcHeight)
(arcWidth)
LpGraphics
1.Vhnhhc:
Vagic:
drawPolygon(int[]x,int[]y,intnumberPoint)
drawPolygon(Polygonp)
Lppolygon:
CungcpkhnngtoPolygonynchuynhn.agic
cxemnh1itng.
CththmnhnhphngthcaddPoint.
Polygonpg=newPolygon();
pg.addPoint(2,50);
g.drawPolygon(pg);
Phngthccontains(intx,inty)kimtra1imcnmtrongagickhng.
PhngthcgetBounds()chobitphmvitiamaccnhchimgi (trvkiu
Rectangle)
LpGraphics
1.Vhnhhc:
Vcungtrn:
drawArc(intx,inty,intwidth,intheight,intstartAngle,intarcAngle);
fillArc(intx,inty,intwidth,intheight,intstartAngle,intarcAngle);
(x,y)
(width)
FillArc
DrawArc
arcAngle
startAngle
(height)
LpGraphics
2.Vch:
Vmngkt<Char>:
drawChars(char[]Data,intOffset,intLength,intx,inty)
Data:Mngktcnv.
Offset:Ktbtu.
Length:Sktcnv.
char[]A={H,e,l,'l,o,!};
g.drawChars(A,0,A.length,50,50);
Cchkhc:
drawBytes(byte[]Data,intOffset,intLength,intx,inty)
Data:Mngbyte(biudinmASCII).
VD:
byte[]B={65,66,67,68,69};//A,B,C,D,E
g.drawChars(B,0,A.length,50,50);
LpGraphics
2.Vch:
Vchuikt<String>:
drawString(Strings,intx,inty)
VD: g.drawString(Hello!,50,50);
ThayiFontch:
setFont(Fontf);
LpFont:
KhitoFont:Font(Stringfontname,intstyle,intfontsize)
VD:Fontf=newFont(.VnArial,Font.BOLD+Font.ITALIC,20)
MtsphngthccalpFont:getName,getStyle,getSize,
isBold,isItalic,isPlain
Lyttcfonttronghthng:DngphngthcgetFontListcaitng
Toolkit.
VD:
String[]FL=getToolkit().getFontList();
LpGraphics
3.Chv:
itngGraphicsc2chv:paint,XOR
Chpaint:Lchmcnhtavnthy.
Tadngphngthc:setPaintMode();caitngGraphicsgich
ny.
ChXOR:setXORMode(Colorc);
VD:
Fontf=newFont(".VnArial",Font.BOLD+Font.ITALIC,40);
g.setFont(f);
g.setColor(Color.blue);
(0,0,255)
g.drawString("Hello!",50,60);
(blue)
g.setXORMode(Color.green);
g.setColor(Color.black);
(blue)XOR(255,0,255)=(0,255,255)
g.fillRect(50,40,90,90);
(black)XOR(green)= (255,0,255)
(0,0,0)
(0,255,0)
LpGraphics
4.Vnh:
LpGraphicscungcpphngthcdrawImage()vnhngin:
drawImage(Imageimg,intx,inty,ImageObserverOb)
drawImage(Imageimg,intx,inty,Colorbgcolor,ImageObserverOb)
drawImage(Imageimg,intx,inty,intwidth,intheight,ImageObserverOb)
drawImage(Imageimg,intx,inty,intwidth,intheight,Colorbgcolor,ImageObserverOb)
+bgColor:Munnivinhtrongsut:
+VD:
Imageim=getToolkit().getImage("Untitled1.png");
g.drawImage(im,50,50,im.getHeight(this),im.getWidth(this),
Color.darkGray,this);
+ ImageObserver Ob: i tng
chu trch nhim kim tra xem
nhsnsngvhaycha.
Thng thng l this.(this l thn
ApplethayFrame)
LpGraphics
5.LpColor:
1imnhtrnmnhnhuckthpbi3mucbn:R,G,B(Red,Green,
Blue).R,G,Bucgitrt0255(1byte)Tngcngl3byte.
Khito1mu:
+TheogitrR,G,B:
Colorcl=newColor(intRed,intGreen,intBlue)
VD:Muxanhlcy:Colorgr=newColor(0,255,0);
+Theogitr%(cgitrt01):
Colorcl=newColor(floatRed,floatGreen,floatBlue)
VD:Green:Colorcl=newColor(0.0,1.0,0.0)
+Theogitrhexa(mmu):
VD:Colora=newColor(0xffaf00);
ff=15*161+15*160=255
Alpha
0xff
0xaf
0x00
LpGraphics
5.LpColor(tt):
+KhitomutheomucbncalpColor:
Colora=newColor(Color.black);
Hoc:Colora=Color.black;
Mt s mu c bn : black, blue, cyan, darkGray, gray, green, lightGray,
magetan,orange,pink,red,white,yellow.
MtsphngthccalpColor:
intgetRed:TrvgitrR(int).
intgetGreen:TrvgitrG(int).
intgetBlue:TrvgitrB(int).
intgetAlpha:TrvgitrAlpha(int).
LpGraphics
6.Vngct(Clipping):
VD:
itngGraphicschophptacit1vngctbngphngthcclipRect
sau:
clipRect(intx,inty,intwidth,intheight)
publicvoidpaint(Graphicsg)
{
g.drawRect(40,50,80,90);
g.clipRect(40,50,80,90);
g.setColor(Color.blue);
g.fillOval(30,40,90,90);
}
LpGraphics2D
Khiqut:
publicvoidpaint(Graphicsg)
{
Graphics2Dg2=(Graphics2D)g;
}
LpGraphics2D
CctnhnngmicaGraphics2D:
adngtrongmutvmut:tloang(Gradient),tbng
munh(pattern),tmunhtrongsut(Transparent).
Sdngfontcththeotnghiuhnh.
Thayicdycantbtv.Ccntvcthdng
mu,chophpxlccntvgpkhctheonhiucch.
Ccphpbinita.
Hnhv(Shape)cthl1itngtv.
LpGraphics2D
1.CcdnghnhhctrongJava2D
CclpitnghnhhccaJava2D(tronggijava.awt.geom):
Line2D.Double
//ngthng
Arc2D.Double
//Cungtrn
Line2D.Float
Arc2D.Float
Polygon
//agic
Area
//Vng
QuadCurve2D.Double//Cunggct
CubicCurve2D.Double//Khi
QuadCurve2D.Float
CubicCurve2D.Float
Rectangle2D.Double//Hnhchnht
Ellipse2D.Double //Hnhtrn,elip
Rectangle2D.Float
Ellipse2D.Float
RoundRectangle2D.Double //HnhCN
GenrralPath
//Vcung
RoundRectangle2D.Float Gctrn
LpGraphics2D
1.CcdnghnhhctrongJava2D(tt)
Line2D.Double:Khito:setLine(intx1,inty1,intx2,inty2)
Arc2D.Double:Khito:setArc(x,y,w,h,angStart,angNum,closure)
Ellipse2D.Double:Khito:setFrame(x,y,w,h);
VD:
publicvoidpaint(Graphicsg)
{
Graphics2Dg2=(Graphics2D)g;//EpkieuchodoituongGraphics
Line2D.Doublea=newLine2D.Double();
a.setLine(100,150,150,150);//KhoitaoLine
Arc2D.Doubleb=newArc2D.Double();
b.setArc(50,50,90,90,20,90,2);//Khoitaocungtron
Ellipse2D.Doublec=newEllipse2D.Double();
c.setFrame(20,20,80,80); //KhoitaoElip
g2.setColor(Color.orange); //Doimau
g2.fill(b); //Vecungtron
g2.draw(a);//VeLine
g2.draw(c);//Xemthuvungcat
g2.clip(c);//Catbanghinhc
g2.setColor(Color.magenta);//Doimau
g2.fillRect(10,50,50,50);//VehinhCN
LpGraphics2D
1.CcdnghnhhctrongJava2D(tt)
Dnghnhhccbit:
GenrralPath:Vcung
GeneralPathp=newGeneralPath(GeneralPath.WIND_EVEN_ODD);
(x3,y3)
Curve:
p.moveTo(x1,y1);
(x2,y2)
p.curveTo(x2,y2,x3,y3,x4,y4);
Quad:
p.moveTo(x1,y1);
p.quadTo(x2,y2,x3,y3);
(Curve)
(x1,y1)
QuadCurve2D:Vcung(TngtQuad
(x2,y2)
caGenrralPath)
(x4,y4)
Khito:
publicvoidsetCurve(x1,y1,x2,y2,x3,y4)
Hoc:publicvoidsetCurve(Pointp1,Pointp2,Pointp3)
CubicCurve2D:(TngtCurvecaGenrralPath)
(x1,y1)
(Quad)
(x3,y3)
LpGraphics2D
2.Thayintv:
Ccphngthcdraw..caitngGraphicsucntvl1pixel.
Graphics2Dckhnngthayidycantv,ngthicngcththay
ickiucantv.
Thayidycantv:
TadngphngthcsetStroke(Strokes) : Nhvytaphito1itngc
ci t giao din Stroke truyn, lp BasicStroke l lp duy nht ci giao
din:
BasicStroke(foatpenWidth);
VD:
Trc:
publicvoidpaint(Graphicsg)
{
Graphics2Dg2=(Graphics2D)g;
Sau:
g2.setStroke(newBasicStroke(3));
g2.drawLine(30,40,60,90);
}
LpGraphics2D
2.Thayintv:
Thayikiucantv:
Ccphngthckhitontvvikiuntv:
NgoiphngthcBasicStroke(foatpenWidth)tacn1sphngthckhito:
BasicStroke(floatwidth,intcap,intjoin,floatmiterlimit,floatdash[],floatdash_phase)
capStyle:Kiuumtntv(CAP_SQUARE,CAP_BUTT,CAP_ROUND).
jonStyle:Kiuni(JOIN_MITER,JOIN_BEVEL,JOIN_ROUND).
miterlimit:Giihnmni.
dash[]:mngchathngtintgy:
dash_phase:imbtucant.
VD:20pxlin,30pxt,10pxlin,20pxt.
floatA={20,30,10,20,40}
capStyle=0
jonStyle=0
capStyle=1
jonStyle=1
capStyle=2
jonStyle=2
LpGraphics2D
3.Tmu:
Graphics2DclpphngthcsetPaint(Paintx)thaychosetColor()caGraphics
trcytmu:
CclpconcalpPaint:
+Color:Mun.
+GradientPaint:Tloang.
+TexturePaint:Ttheomunh.
GraientPaint:Tloang.
GradientPaint(x1,y1,Color1,x2,y2,Color2)
publicvoidpaint(Graphicsg)
{
Graphics2Dg2=(Graphics2D)g;
GradientPaintgr=newGradientPaint(100,100,Color.red,150,150,Color.BLUE);
g2.setPaint(gr);
g2.fillOval(50,50,200,200);
}
LpGraphics2D
3.Tmu:
TexturePaint:Ttheomunh.
TexturePaint(BufferedImagea,Rectangle2Db)
publicvoidpaint(Graphicsg)
{
Graphics2Dg2=(Graphics2D)g;
Imagea=getToolkit().getImage("Warning.png");
BufferedImagee=newBufferedImage(a.getWidth(this),
a.getHeight(this),BufferedImage.TYPE_INT_RGB);
Graphics2Dg2d=e.createGraphics();
g2d.drawImage(a,0,0,this);
Rectanglert=newRectangle();
rt.setRect(0,0,a.getWidth(this),a.getHeight(this));
TexturePaintgr=newTexturePaint(e,rt);
g2.setPaint(gr);
g2.fillOval(50,50,200,200);
LpGraphics2D
4.Vnhtrongsut:
vnhtrongsuttadngphngthcsetComposite()calpGraphics2D:
DngitngAlphaCompositetruynvophngthcsetComposite().
publicvoidpaint(Graphicsg)
{
Graphics2Dg2=(Graphics2D)g;
g2.setColor(Color.red);
g2.fillRect(50,50,60,60);
g2.setColor(Color.blue);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)0.3));
g2.fillOval(60,60,80,80);
}
LpGraphics2D
5.Binita:
Java2Dchophpdch,quay,cogin,bpmo1hnhddng:
HmbinitacaGraphics2D
translate(intx,inty):Dichuyngctan(x,y).
rotate(doublea,intx,inty):Quayhtaa,vdichuyntmquaynta(x,y)
scale(sx,sy):Phngto,thunhvitlsx,sy.
shear(floatshx,floatshy):Bpmo1hnhtheochiungang,dc.
publicvoidpaint(Graphicsg)
{
Graphics2Dg2=(Graphics2D)g;
g2.shear((float)1.0,(float)0.0);
g2.setColor(Color.red);
g2.fillRect(50,50,60,60);
}
Likt:
Phlc:
***Tiliuthamkho***
JavaTp1Nhxutbnlaong.
OReillyJavaCookbook2ndEdition.
ThinkingInJava.
***Nhmthchin***
Nhm8:
NguynnhHng
DuyBnh
PhmVnHoi
PhmVnLc
VnHon
TrnVnKin
ThnhNam
TrnThHin
Cmnsquantmtheo
dicathyvccbn!
THEEND
http://www.mediafire.com/?hz7rf8jbvvnlnht
Thng3/2010