如果您正在编写游戏,您的玩家(以及其他游戏对象)不能仅由图像对象表示。你应该为此编写自己的类。
下面是一个例子:
import java.awt.*;
public class Player {
//X- and Y-Coordinates
public int x,y;
//Player graphic
Image img=Toolkit.getDefaultToolkit().getImage("path.to.your.image.file");
public Player(){
}
//Rendering method
//You should call this in your frame class where you override paint(Graphics g)
//or paintComponent(Graphics g)
//
//like this: player.draw(g,this)
public void draw(Graphics g,JPanel jp){
//Draw the Image at the right position
g.drawImage(img,x,y,jp);
}
//Get X coordinate
public int getX(){
return x;
}
//Get Y coordinate
public int getY(){
return y;
}
//Update method
//You should call this in your game loop
public void update(){
//Handle button presses, collision testing or other stuff relating
//the player object here
}
}
正如你所看到的,你必须在玩家的坐标直接访问和控制。你应该为这个游戏的所有对象写一个这样的类。
我认为你必须绘制这些图像,并且你的代码中必须有逻辑来定义它们的绘制位置。 – copeg