AWT 提供了一些用户接口的构件,但是它不提供一些类似的纯粹的绘制图形的对象,例如 Rectangle、Polygon 和 Point 类没有任何绘制图形的能力。换句话说它们不具备 draw 方法,仅仅可以设置和的到它们代表的几何实体的信息。
    为了代替那些纯粹的、可绘制图形的对象,AWT 使用了一种简单的模式。每个 AWT 构件完全来自于它自己的 java.awt.Graphics 对象。
    java.awt.Graphics 是一个抽象类,其作用是定义一个真正的工具,用来接受图形操作。
表一:传递一个对 Graphics 的引用的 JDK 方法
    
        
            | java.awt | 
            Canvas | 
            paint(Graphics g) | 
        
        
             | 
            Component | 
            paint(Graphics g) | 
        
        
             | 
            Component | 
            paintAll(Graphics g) | 
        
        
             | 
            Component | 
            print(Graphics g) | 
        
        
             | 
            Component | 
            printAll(Graphics g) | 
        
        
             | 
            Component | 
            update(Graphics g) | 
        
        
             | 
            Container | 
            paint(Graphics g) | 
        
        
             | 
            Container | 
            paintComponents(Graphics g) | 
        
        
             | 
            Container | 
            print(Graphics g) | 
        
        
             | 
            Container | 
            printComponents(Graphics g) | 
        
        
             | 
            ScrollPane | 
            printComponents(Graphics g) | 
        
        
            | java.beans | 
            Property-Editor | 
            paintValue(Graphics g, Rectangle r) | 
        
        
             | 
            Property-EditorSupport | 
            paintValue(Graphics g, Rectangle r) | 
        
    
表二:返回 Graphics 引用的 JDK 方法
    
        
            | java.awt | 
            Component | 
            getGraphics() | 
        
        
             | 
            Image | 
            getGraphics() | 
        
        
             | 
            PrintJob | 
            getGraphics() | 
        
        
             | 
            Graphics | 
            create() | 
        
        
             | 
            Graphics | 
            create(intx, int y, int w, int h) | 
        
    
Graphics 类履行2个主要的职责:
    · 设置和获取图形参数。
    · 在输出设备中执行图形操作。
得到构件的 Graphics 的引用有2种方法:
    · 重载 表一 中的方法(传递 Graphics 的引用)
    · 调用 表二 中的方法(返回 Graphics 的副本)
Graphics 对象的寿命
    除了使用 表二 的方法得到的 Graphics 的副本外,使用 表一 的方法得到的 Graphics 的引用只有在方法的执行过程中才有效(例如重载的 paint() 和 update() 等方法)。一旦方法返回,引用将不再有效。
    通过使用 表二 的方法得到的 Graphics 的对象使用完后需要调用 Graphics.dispose() 方法处理。
// 程序片断
public void someMethodInAComponent(){
 Graphics g = getGraphics();
 
 if(g != null){
  try{
   // ...
   // ...
  }
  finally{
   g.dispose();
  }
 }
}
Graphics 类还提供2个方法创建 Graphics 对象:
    · Graphics create() 
        创建精确的 Graphics 副本。
    · Graphics create(int x, int y, int w, int h)
        创建一个副本,但是,变元指定一个平移量 (x, y) 和一个新的剪贴矩形 (x, y, w, h)。create(int, int, int, int) 返回的 Graphics 的原点被转换成 (x, y) 的坐标,但是剪贴矩形转换为原剪贴矩形和指定矩形的交集。
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class CreateTest extends Applet{
 private Image image;
 
 public void init(){
  image = getImage(getCodeBase(),"lena.jpg");
  try{
   MediaTracker mt = new MediaTracker(this);
   mt.addImage(image,0);
   mt.waitForID(0);
  }
  catch(InterruptedException e){
   e.printStackTrace();
  }
 }
 
 public void paint(Graphics g){
  Graphics copy = g.create(image.getWidth(this),0,image.getWidth(this),image.getHeight(this));
  
  try{
   System.out.println("g: " + g.getClip().toString());
   System.out.println("copy: " + copy.getClip().toString());
   
   g.drawImage(image,0,0,this);
   copy.drawImage(image,0,0,this);
  }
  finally{
   copy.dispose();
  }
 }
}