请问Grahpics2D类的draw(Shape s)方法是如何实现绘制功能的?
看source该draw方法抽象 并且该类没有子类
谢谢如下面程序段所示:package demo;/**   @version 1.31 2004-05-03   @author Cay Horstmann*/import java.awt.*;import java.awt.geom.*;import javax.swing.*;public class DrawTest{     public static void main(String[] args)   {        DrawFrame frame = new DrawFrame();      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      frame.setVisible(true);   }}/**   A frame that contains a panel with drawings*/class DrawFrame extends JFrame{   public DrawFrame()   {      setTitle("DrawTest");      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);      // add panel to frame      DrawPanel panel = new DrawPanel();      add(panel);   }   public static final int DEFAULT_WIDTH = 400;   public static final int DEFAULT_HEIGHT = 400;  }/**   A panel that displays rectangles and ellipses. */class DrawPanel extends JPanel{     public void paintComponent(Graphics g)   {        super.paintComponent(g);      Graphics2D g2 = (Graphics2D) g;      // draw a rectangle      double leftX = 100;      double topY = 100;      double width = 200;      double height = 150;      Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);      g2.draw(rect);   }}

解决方案 »

  1.   

    那个程序不太好看
    就是这个程序:import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;public class DrawTest
    {
    public static void main(String[] args)
    {
    DrawFrame frame = new DrawFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }
    class DrawFrame extends JFrame
    {
    public DrawFrame()
    {
    setTitle("DrawTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    DrawPanel panel = new DrawPanel();
    add(panel);
    }public static final int DEFAULT_WIDTH = 400;
    public static final int DEFAULT_HEIGHT = 400;
    }class DrawPanel extends JPanel
    {
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;double leftX = 100;
    double topY = 100;
    double width = 200;
    double height = 150;
    Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
    g2.draw(rect);
    }
    }
      

  2.   

    呵呵,这部分的代码当然看不到的.最终JVM是通过调用OpenGL或其他(DX之类)来画图的.
      

  3.   

    也就是说 一个abstract方法有时候意味着它本身不被覆盖也能执行,因为有底层代码?
    可是我认为draw(S s)至少应该有调用底层代码的顶层代码
    譬如
    draw (Shape s)
    {
    JVM.底层代码....
    }
      

  4.   

    自己找到答案:Graphics2D其实是有3个子类的,分别是PeekGraphics,ProxyGraphics2D,SunGraphics2D,均重写了该 draw方法。但是这3个子类在文档中确实没有任何描述。相关查阅方法可以是在Eclipse中类右键Open hierarchy得到继承展开。而关于 sun包为什么不会出现在文档中及相关内容,请到网上搜下再参阅《为什么程序员不应调用“sun”包?》。关于这部份sun包的source,请到 http://www.java.net/download/jdk6/下载。谢谢flushtime