gpt4 book ai didi

java - 尝试在 java 中绘制图像时出现奇怪的错误?

转载 作者:行者123 更新时间:2023-12-01 13:00:33 25 4
gpt4 key购买 nike

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

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);
}
}
}

如果我将爆炸函数移动到主类中,它可以完美地工作,但是如果我尝试从另一个类调用它,我会收到上面的错误。如果有人能告诉我为什么会发生这种情况,我将非常感谢您的帮助。

非常感谢!

最佳答案

这样调用

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

关于java - 尝试在 java 中绘制图像时出现奇怪的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23547242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com