Graphics是抽象类,在paint方法中怎么能作为一个参数呢,实际运行中是为什么可以直接调用它中的抽象方法drawString ,drawPolygon等?

解决方案 »

  1.   

    Graphics作为一个抽象类是不能够直接调用它的抽象方法的,但是在paint(Graphics g)方法中传入的是一个继承了Graphics的具体类ProxyGraphics(在SUN的JDK中),因此你在使用drawPolygon方法时事实上是在调用Graphics子类的方法。
      

  2.   

    一般在paint方法中,都要类型转换
    Graphics2D g2=(Graphics2D)Graphics;
    然后调用这个类的方法就可以实现画图
      

  3.   

    Graphics的直接已知子类:DebugGraphics, Graphics2D,怎么没有看到你说的 
    ProxyGraphics,可以测试出它来吗?
      

  4.   

    给你一段代码,我在网上看到的。import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JApplet;
    import sun.print.ProxyGraphics;public class StrinDrawing extends JApplet {
     Graphics b; Color str = null; public void init() {
      b = new ProxyGraphics(b);
     } public void paint(Graphics g) {
      Color str = Color.blue;
      g.setColor(str);
      g.drawString("欢迎你来到这里!", 20, 30); }} 
      

  5.   

    import sun.print.ProxyGraphics;
    并不是JDK中自带的
      

  6.   


    对的,只能调用Graphics的方法,一般都会把转化成Graphics2D,因为这类实现了更多的方法,而且实现了对图形的多态绘制,draw(Shape s)