Graphics2D & Shape - 绘制2D图形

来源:http://blog.csdn.net/rexih/article/details/46445225


Graphics2D类包含绘制直线,矩形,椭圆等方法

Component的paintComponent方法会自动获取一个Graphics2D对象,但是参数列表定义的是Graphics对象,通过强转来使用

Graphics2D g2d=(Graphics2D)g;


绘制具体图形的类 : Line2D,Rectangle2D,Ellipse2D,Point2D


绘制图形的步骤:

1.获取一个Graphics2D对象

2.创建一个实现Shape接口的图形对象,设置好其属性

3.调用Graphics2D的draw 方法


像素坐标的类型:

1.最初是int型,表示屏幕上的像素坐标

2.Java 2D库采用浮点型

a.这样可以转换成更有意义的单位,比如微米,英寸

b.屏幕使用单精度即可,而且float存储空间小,计算速度可能比double快

3.为了方便通常使用Double。 float在赋值时需要手动添加f,double型的返回值需要强转成float


所以,图形类通常有两个静态内部类作为子类Float和Double,比如Rectangle2D.Float/Rectangle2D.Double

对于这两个子类:

1.子类只覆盖了超类的方法,所以可以用超类的对象变量直接引用。Rectangle2D rect=new Rectangle2D.Double(...);

2.Rectangle2D的方法返回值都是double,引用的对象是Rectangle2D.Float的话,调用方法,比如getWidth,返回值也是double


常用(特殊)的构造图形的情况:

1.矩形 知道对角点

使用rect.setFrameFromDiagonal(px,py,qx,qy);

或者参数为2个Point2D对象 rect.setFrameFromDiagonal(p,q);

2.矩形 知道中心点和对角点

使用rect.setFrameFromCenter(px,py,qx,qy);rect.setFrameFromCenter(p,q);

3.椭圆 知道中心点和长 宽

ellipse.setFrameFromCenter(cx,cy,cx-width/2,cy-height/2);

画圆:ellipse.setFrameFromCenter(cx,cy,cx+radius,cy+radius);

4.知道椭圆的矩形框架

ellipse.setFrame(rect);

5.画直线

new Line2D.Double(sx,sy,ex,ey);

new Line2D.Double(p,q);


[java]  view plain  copy
  1. import java.awt.*;  
  2. import java.awt.geom.*;  
  3. import javax.swing.*;  
  4.   
  5. public class DrawTest {  
  6.   
  7.     public static void main(String[] args) {  
  8.         EventQueue.invokeLater(new Runnable(){  
  9.             public void run() {  
  10.                 DrawImg f=new DrawImg();                  
  11.             }             
  12.         });  
  13.     }  
  14. }  
  15.   
  16. class DrawImg extends JFrame{     
  17.     DrawImg(){  
  18.         setLocationByPlatform(true);  
  19.         setTitle("draw test");  
  20.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  21.         getContentPane().add(new DrawComponent());  
  22.         pack();       
  23.         setVisible(true);  
  24.     }     
  25. }  
  26.   
  27. class DrawComponent extends JComponent{  
  28.     private static final int DEFAULT_W=600;  
  29.     private static final int DEFAULT_H=600;  
  30.     DrawComponent(){  
  31.         setPreferredSize(new Dimension(DEFAULT_W,DEFAULT_H));  
  32.     }  
  33.     public void paintComponent(Graphics g){  
  34.         Graphics2D g2d=(Graphics2D)g;  
  35.           
  36.         double centerX=DEFAULT_W/2;  
  37.         double centerY=DEFAULT_H/2;  
  38.           
  39.         double conerX=centerX+100;  
  40.         double conerY=centerY+100;  
  41.           
  42.         Rectangle2D rect =new Rectangle2D.Double();  
  43.         rect.setFrameFromCenter(centerX,centerY,conerX,conerY);  
  44.           
  45.         Ellipse2D ellipse=new Ellipse2D.Double();  
  46.         ellipse.setFrame(rect);  
  47.           
  48.         Ellipse2D circle=new Ellipse2D.Double();  
  49.         double radius=Point2D.distance(centerX, centerY, conerX, conerY);  
  50.         circle.setFrameFromCenter(centerX,centerY,centerX+radius,centerY+radius);  
  51.           
  52.         Line2D line= new Line2D.Double(conerX,conerY,conerX-200,conerY-200);  
  53.         Line2D line2= new Line2D.Double(conerX-200,conerY,conerX,conerY-200);  
  54.           
  55.           
  56.         g2d.draw(rect);  
  57.           
  58.         g2d.draw(ellipse);  
  59.         g2d.draw(circle);  
  60.           
  61.         g2d.draw(line);  
  62.         g2d.draw(line2);          
  63.     }     
  64. }