抽像Graphics类drawImage方法是abstract的,为什么Graphics的实例可以真接使用画图呢???????drawImage方法在什么地方被实现的????
 public void paint(Graphics g)
    {
        g.drawImage(i[currentimage],100,100,this);
        currentimage=++currentimage%4;
        try
        {            Thread.sleep(500);
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }        repaint();    }

解决方案 »

  1.   

    只有有实例就一定实现了方法,这是必然的Graphics的实例,具体那个实现了,你要看Graphics的实例是如何取得的,就可以找到了.
      

  2.   

    抽像Graphics类drawImage方法是abstract的,为什么Graphics的实例可以真接使用画图呢???????drawImage方法在什么地方被实现的????public void paint(Graphics g) 这里的g不过是Graphics类的一个对象, 但并不表明一定是由类Graphics生成的, 面向对象中, 可以把子类的引用直接赋值给父类, 所以g是Graphics的子类的对象, 由系统提供这个对象. 或者可能是由你的程序提供, 如在一个按钮的
    protected void paintComponent(Grapnics g) {
        BufferedImage buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        super.paintComponent(buf.getGraphics()); // 这里父类就使用了由你提供的buf图形的Graphics类的对象.    // 然后你可以在这个图像上做一些操作, 如Blur这个图像, 使用ConvolveOp类
        
        g.drawImage(buf, 0, 0, null); // 这里的这个g是由系统提供的, 把图像buf绘制到屏幕上.
    }
      

  3.   

    谢谢大家! g.drawImage(buf, 0, 0, null); // 这里的这个g是由系统提供的, 把图像buf绘制到屏幕上.那drawImage就是系统实现了??  java.awt.Graphics
    直接已知子类: 
    DebugGraphics, Graphics2D     
    DebugGraphics这个类实现了drawImage方法。那么这里的g是DebugGraphics类的对像吗?????????
      

  4.   

    一看知
    System.out.println(g.getClass().getName());
      

  5.   


    现在更闷了显示我编写的这个类,,,难道这个g就是类本身的对像???但是我在这个类里没有实现drawImage方法啊??我这个类继承的是Applet类,,,,超类也没有实现啊???麻烦讲的具体点。。谢谢
      

  6.   

    那就是Applet类的父类中的某个类已经实现了,就可以一层一层的查下便知.
      

  7.   

    已经全部查过了一直查到Object都没有闷
      

  8.   

    你确定Graphics 对象g
    System.out.println(g.getClass().getName()); 打印出的是你写的类吗if (g instanceof 类) {
    }
    看下是 由那个类加载器加载的
    System.out.println(g.getClass().getClassLoader().getClass().getName());
      

  9.   


    确定的我把全部代码发出来,,麻烦帮看一下package ImageTest;import java.applet.Applet;
    import java.awt.*;
    import java.lang.*;
    import java.awt.event.*;
    public class DoubleBuffer extends Applet {
    private Image[] i;
    private int totalimage=4;
    private int currentimage=0;
    private Image ImgBuf;
    private Graphics GraBuf;
    MediaTracker mt=new MediaTracker(this);
        public void init()
        {        ImgBuf=createImage(400,400);
            GraBuf=ImgBuf.getGraphics();
            GraBuf.setColor(Color.white);
            GraBuf.fillRect(0,0,400,400);
            i=new Image[totalimage];
            for(int u=0;u<totalimage;u++)
            {
                i[u]=getImage(getDocumentBase(),"h"+(u+1)+".gif");
                mt.addImage(i[u],u);
            }
            try {
                mt.waitForID(0);
            } catch (Exception ex) {
                ex.printStackTrace();
            }    }
        public void start()
        {
            currentimage=0;
            GraBuf.drawImage(i[currentimage],100,100,this);
            currentimage=1;
        }
        public void paint(Graphics g)
        {
           // g.drawImage(i[currentimage],100,100,this);
            g.drawImage(ImgBuf,100,100,this);
            if (mt.checkID(currentimage,true)) {
                GraBuf.fillRect(0,0,400,400);
                GraBuf.drawImage(i[currentimage],100,100,this);
                currentimage=++currentimage%4;
                System.out.print(getClass().getName());
            }else
            {
                g.drawString("Loading……",10,10);
            }        try
            {            Thread.sleep(500);
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }        repaint();    }
        public void update()
        {
            repaint();
        }
    }
      

  10.   

    找到了
    sun.java2d.SunGraphics2D
    上面那个类实现的
      

  11.   

    System.out.println(g.getClass().getName());//应该这样写,你要知道g是那实例化的当然得调用g了
    System.out.print(getClass().getName());//相当于this.getClass()那一定永远是自己了
      

  12.   

    System.out.println(g.getClass().getClassLoader()+"");这个类应该是由Bootstrap Loader 加载的,
      

  13.   

    Graphics是一个抽象类,不可能直接产生实例,但传入的参数必须是一个实例。这里的g事实上不属于Graphics类,而是其某个子类的一个实例。这个子类就是sun.java2d.SunGraphics2D,代码中看不出来,是通过JVM传入的,具体过程我也不是很清楚。