我正在尝试制作一个小游戏,将功能从主类转移到子类。我已将函数从主类复制并粘贴到子类中,由于某种原因,我遇到此错误:

no suitable method found for drawImage(java.awt.Image,int,int,pony)
method java.awt.Graphics2D.drawImage(java.awt.BufferedImage, java.awt.BufferedImageOp, int, int) is not applicable; (actual argument java.awt.Image cannot be converted to java.awt.BufferedImage by method invocation conversion);


这是功能:

public void explode(Graphics g)
{
    Graphics2D g2d = (Graphics2D) g;

    int i=0;
    float angle = 0.03f;
    float PI = 3.14159f;
    int x2,y2;
    int r=40;

    while(r<200)
    {
        while (angle < 2 * PI)
        {
            x2 = (int)this.x + (int) (Math.cos(angle)*r);
            y2 = (int)this.y + (int) (Math.sin(angle)*r);

            g2d.drawImage(sparkles[i].getImage(), x2, y2,this);

            angle+=0.1;
            i+=1;
        }
        i=0;
        angle=0.03f;
        r+=5;
    }

}


这是主类中的函数调用:

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    for(int i=0;i<curPonies;i++)
    {
       if(ponies[i].colliding())
       {
          ponies[i].explode(g);
       }
    }
}


如果将explode函数移到主类中,则可以正常运行,但是如果尝试从另一个类中调用它,则会收到上述错误。如果有人能告诉我为什么会这样,我将非常感谢您的帮助。

非常感谢你!

最佳答案

用这种方式打电话

g2d.drawImage(sparkles[i].getImage(), x2, y2, null); // if observer is not known


Graphics#drawImage(Image img,int x,int y,ImageObserver observer)

java.awt.Graphics2Djava.awt.Graphics的子类。



在您的情况下,最后一个参数this必须实现ImageObserver

g2d.drawImage(sparkles[i].getImage(), x2, y2,this);
//last argument this must be an ImageObserver

10-05 21:12
查看更多