嘿,我的问题是如何在 Java 的 Pong 游戏中将图片放在球对象上?下面是我当前对一个名为 Ball 的对象的代码,它扩展了一个抽象类 Shape。
package Software;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Ball extends Shape implements ActionListener {
public Image tennisBall;
public Ball() {
try {
tennisBall = ImageIO.read(new File("tennisBall.jpg"));
} catch (IOException ex) {
// handle exception...
}
setHeight(12);
setWidth(12);
speed = 5;
}
@Override
public void draw(Graphics g) {
// g.fillOval(getPositionX(), getPositionY(), (int)getWidth(), (int)getHeight());
g.drawImage(tennisBall, getPositionX(), getPositionY(), (int)getWidth(), (int)getHeight(), null);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}