PackageCom.mapbar.graphics;ImportJava.awt.Graphics2D;ImportJava.awt.geom.Ellipse2D;ImportJava.awt.geom.Line2D;ImportJava.awt.geom.Rectangle2D;ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;ImportJavax.imageio.ImageIO;/*** Class Drawgraphics.java * Description java2d draw line, rectangle, ellipse, rotate graphic * Company Mapbar * Author CHENLL * Version 1.0 * Date 2012-7-20 pm 12:06:15*/ Public classdrawgraphics{Privatebufferedimage image; Privategraphics2d Graphics; Public voidinit () {intwidth=480,hight=720; Image=NewBufferedImage (WIDTH,HIGHT,BUFFEREDIMAGE.TYPE_INT_RGB); //Get the graphics contextGraphics =(graphics2d) image.getgraphics (); } /*** Create a (X1,Y1) to (X2,Y2) line2d Object *@throwsIOException*/ Public voidDrawLine ()throwsioexception{init (); Line2D Line=NewLine2d.double (2,2,300,300); Graphics.draw (line); Graphics.dispose (); Outimage ("PNG", "D:\\line.png"); } /*** Create an upper-left corner coordinate is (50,50), width is 300, height is 400 of a rectangle object *@throwsIOException*/ Public voidDrawRect ()throwsioexception{init (); Rectangle2D rect=NewRectangle2d.double (50,50,400,400); Graphics.draw (rect); Graphics.fill (rect); Graphics.dispose (); Outimage ("PNG", "D:\\rect.png"); } /*** created a top-left coordinate is (50,50), width is 300, height is 200 of an Ellipse object, if high, wide, it is a standard circle * *@throwsIOException*/ Public voidDrawEllipse ()throwsioexception{init (); Ellipse2d Ellipse=NewEllipse2d.double (50,50,300,200); Graphics.draw (ellipse); Graphics.fill (ellipse); Graphics.dispose (); Outimage ("PNG", "D:\\ellipse." PNG "); } /*** Output Drawn graphics *@paramtype *@paramFilePath *@throwsIOException*/ Public voidOutimage (String type,string FilePath)throwsioexception{imageio.write (Image,type,NewFile (FilePath)); } Public Static voidMain (string[] args)throwsioexception{drawgraphics DG=NewDrawgraphics (); Dg.drawline (); Dg.drawrect (); Dg.drawellipse (); }}
Java Graphics processing-java graphics2d