Graphics2D类继承于Graphics类,提供了几何学、坐标转换、颜色管理及文本排列等更高级的控制。Graphics2D类是java平台渲染二维图形,文字及图片的基础类,提供较好的对绘制形状,填充形状、旋转形状、及定义颜色的支持。
在AWT编程接口中,用户通过paint方法接收Graphics对象作为参数,若使用Graphics2D类,就需要在paint方法中进行强制转换。
public void paint(Graphics oldg){
Graphics2D new=(Graphics2D)oldg;
}
绘制形状:
public abstract void draw(shape s); 空心s;
public sbstract void fill(shape s); 实心s;
Line2D:线 Rectangle2D: 矩形 RoundRectangle2D:画圆角矩形 Ellipse2D:椭圆 GeneralPath:几何路径。
GeneralPath常用的方法有四个:
public void lineTo(float x,float y); 从当前坐标点到(x,y)画一条直线
public void moveTo(float x,float y); 移动到点(x,y)
public void qualTo(float x1,float y1,float x2,float y2); 以坐标点(x1,y1)为控制点,在当前坐标点和坐标点(x2,y2)之间插入二次曲线片断。
public void curveTo(float x1,float y1,float x2,float y2,float x3,float y3); 以(x1,y1)和(x2,y2)为控制点,在当前坐标点和(x3,y3)之间插入曲线片断。
在draw方法中提到Graphics2D的环境变量。所谓的环境变量是指设置画图和填充属性等,设置方法分别如下:
public abstract void setStroke(Stroke s); 设置笔画的粗细。
public BasicStroke(float width); 创建实线笔画宽度为width。
public abstract void setPaint(Paint paint);
设置Graphics2D环境的填充属性,其中paint的值可以渐变充值,渐变充值类:java.awt.GradientPaint,也可以为图形填充类java.awt.TexturePaint,构造函数:
public GradientPaint(float x1,flaot y2,Color.Color1,float x2,float y2,Color.Color2,boolean cyclic);在(x1,y1)和(x2,y2)之间颜色从Color1到Color2之间渐变,当cyclic为真,循序渐变。
eg:
import java.util.awt.*;
import java.applet.*;
import java.awt.geom.*;
public class GUI2D extends Applet{
public void paint(Graphics oldg){
Graphics2D g=(Graphics2D)oldg;
//设置笔画
BasicStroke stroke=new BasicStroke(10);
g.setStroke(stroke);
//画线
Line2D line=new Line2D.Float(0,0,20,30);
g.draw(line);
line=new Line2D.Float(50,50,100,50);
g.draw(line);
line=new Line2D.Float(50,50,50,100);
g.draw(line);
stroke=new BasicStroke(5);
g.setStroke(stroke);
//设置渐变填充
GradientPaint gt=new GradientPaint(0,0,Color.green,50,30,Color.blue,true);
g.setPaint((Paint)gt);
//画矩形
Rectangle2D rect=new Rectangle2D.Float(80,80,40,40);
g.draw(rect);
rect=new Rectangle2D.Float(100,100,40,40);
g.draw(rect);
//画椭圆
Ellipse2D ellipse=new Ellipse2D.Float(120,120,30,40);
g.draw(ellipse);
gt=new GradientPaint(0,0,Color.red,30,30,Color.yellow,true);
g.setPaint((Paint)gt);
ellipse=new Ellipse2D.Float(140,140,20,20);
g.draw(ellipse);
//画圆角矩形
RoundRectangle roundrect=new RoundRectangle2D.Float(160,160,40,40,20,20);
g.draw(roundrect);
roundrect=new RoundRectangle2D.Float(180,180,40,40,20,20);
g.fill(roundrect);
//画几何图形
GeneralPath path=new GeneralPath();
path.moveTo(150,0);
path.lineTo(160,50);
path.curveTo(190,200,240,140,200,100);
g.fill(path);
}
}
绘制文本
Graphics2D提供了一个文本布局(TextLayout)对象,用于实现各种字体或段落文本的绘制。其构造函数为:
public TextLayout(String string,Font font,FontRnderContext frc); 通过字符串string和字体font构造布局。
public void draw(Graphics2D g2,floatx,float y);将这个TextLayout画到Graphics2D对象上的x,y坐标处。
public Rectangle2D getBounds(); 返回TextLayout对象的区域。
eg:
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
import java.awt.font.*;
public class GUIText extends Applet{
public void paint(Graphics oldg){
Graphics2D g=(Graphics2D)oldg;
//设置字体
Font font1=new Font("Courier",Font.PLAN,24);
Font font2=new Font("helvetica",Font.BOLD,24);
FontRenderContext frc=g.getFontRnderContext();
String str=new String("这是一个文本布局类的实现");
String str2=new String("扩充绘制文本的功能");
//构造文本布局
TextLayout layout=new TextLayout(str,f1,frc);
Point2D loc=new Point2D.Float(20,50);
//绘制文本
layout.draw(g,(float)loc.getX(),(float)loc.getY());
//设置边框
Rectangle2D bounds=layout.getBounds();
bounds.setRect(bounds.getX()+loc.getX(),
bounds.getY()+loc.getY(),
bounds.getwidth();
bounds.getheight();
g.draw(bounds);
layout=new Layout(str2,f2,frc);
g.setColor(Color.red);
layout.draw(g,20,80);
}
}
绘制图像
绘制图像用到BufferedImage类,BufferedImage类是用来存放图像数据的可访问的缓冲。构造函数:
public BufferedImage(int width,int height,int iamgeType); 使用宽度(width)、高度(height)和imageType类型构造BufferedImage对象。
public Graphics2D createGraphics()
用图片填充椭圆的具体过程如下:
(1):创建一个Graphics2D,可以画到BufferedImage中。
BufferedImage buf=new BufferedImage(img.getWidth(this),img.getHeight(this),BufferedImage.TYPE_INT_ARGB);
创建一个临时Graphics2D对象。
Graphics tmpG=buf.createGraphics();
将图像画入临时缓冲中:
tmpG.drawImage(img,10,10,this);
(2):用TexturePaint类进行填充
public TexturePaint(BufferedImage tetr,Rectangle2D anchor) 构造TexturePaint对象,需要一个Rectangle2D对象来存放该对象:
Rectangle2D rect=new Rectangle2D.Float(0,0,h,w);
TexturePaint t=new TexturePaint(buf,rect);
(3):然后设置填充模式,并进行填充。
g.setPaint(t);
g.fill(new Ellipse2D.Float(100,50,60,60));
eg:
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
import java.awt.front.*;
import java.awt.image.*;
import java.net.*;
public class GUIImage extends Applet{
public void paint(Graphics oldg){
Graphics2D g=(Graphics2D)oldg;
try{
URL imgURL=new URL(getDocumentBase(),"simple.gif");
Image img=getImage(imgURL);
int h=img.getHeight(this);
int w=img.getWidth(this);
//构造缓冲图像对象
BufferedImage buf=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
//放入临时图形类
Graphics tmpG=buf.createGraphics();
tmpG.drawImage(img,10,10,this);
g.drawImage(buf,10,20,this);
//设置透明颜色对象
Color transBlue=new Color(0,0,255,200);
g.setColor(transBlue);
GeneralOPath path=new GeneralPath();
path.moveTo(60,0);
path.lineTo(50,100);
path.curveTo(160,230,240,140,200,100);
g.fill(path);
transBlue=new Color(0,0,225,140);
g.fill(new Ellipse2D.Float(100,100,50,50));
Rectangle2D rect=new Rectangle2D.Float(0,0,h,w);
//图片填充
TexturePaint t=new TexturePaint(buf,rect);
g.fill(new Ellipse2D.Float(100,50,60,60));
}
catch(Exception e){
System.out.println("ERROR:"+e.getMessage());
}
}
}