java图形 graphics2d_java图形处理-Java Graphics2D

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绘制直线,矩形,椭圆,旋转图形

* Company mapbar

* author Chenll

* Version 1.0

* Date 2012-7-20 下午12:06:15*/

public classDrawGraphics{privateBufferedImage image;privateGraphics2D graphics;public voidinit(){int width=480,hight=720;

image= newBufferedImage(width,hight,BufferedImage.TYPE_INT_RGB);//获取图形上下文

graphics =(Graphics2D)image.getGraphics();

}/*** 创建一个(x1,y1)到(x2,y2)的Line2D对象

*@throwsIOException*/

public void drawLine() throwsIOException{

init();

Line2D line=new Line2D.Double(2,2,300,300);

graphics.draw(line);

graphics.dispose();

outImage("PNG","D:\\Line.PNG");

}/*** 创建一个左上角坐标是(50,50),宽是300,高是400的一个矩形对象

*@throwsIOException*/

public void drawRect() throwsIOException{

init();

Rectangle2D rect= new Rectangle2D.Double(50,50,400,400);

graphics.draw(rect);

graphics.fill(rect);

graphics.dispose();

outImage("PNG","D:\\Rect.PNG");

}/*** 创建了一个左上角坐标是(50,50),宽是300,高是200的一个椭圆对象,如果高,宽一样,则是一个标准的圆

*

*@throwsIOException*/

public void drawEllipse() throwsIOException{

init();

Ellipse2D ellipse=new Ellipse2D.Double(50,50,300,200);

graphics.draw(ellipse);

graphics.fill(ellipse);

graphics.dispose();

outImage("PNG","D:\\ellipse.PNG");

}/*** 输出绘制的图形

*@paramtype

*@paramfilePath

*@throwsIOException*/

public void outImage(String type,String filePath) throwsIOException{

ImageIO.write(image,type,newFile(filePath));

}public static void main(String[] args) throwsIOException{

DrawGraphics dg= newDrawGraphics();

dg.drawLine();

dg.drawRect();

dg.drawEllipse();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值