我目前正在为我的compsci课程制作一个游戏,但我一直无法将图像导入我的applet用作纹理。在这种情况下,重要的类是我的Game.java,Obstacle.java和Resources.java。游戏是我的驱动程序类,Obstacle是我试图将图像放入的对象,而Resources是我实际加载图像的地方。
以下是每个班级的重要部分:
Game.java:
Resources image = new Resources(this);
obs = new Obstacle[16]
for(int i = 0; i < 16; i++){
obs[i] = new Obstacle((i*64), 0, 64, 64);
}
Obstacle.java:
private int x, y; //x pos, ypos
private int w, h; //width, height
Image img;
//Constructor
public Obstacle(int x, int y, int w, int h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.img = Resources.wall;
}
//paint obstacle
public void paint(Graphics g){
g.setColor(Color.lightGray);
g.fillRect(x, y, w, h);
g.setColor(Color.DARK_GRAY);
g.drawRect(x, y, w, h);
g.drawRect(x + 1, y + 1, w - 2, h - 2);
g.drawImage(img, x, y, Resources.game);
}
Resources.java:
static Image wall;
static URL url;
static Game game;
public Resources(Game ga){
try{
url = g.getCodeBase();
}catch(Exception e){
}
wall = g.getImage(url, "texture.png");
Resources.game = ga;
}
当我运行它时,我没有得到任何错误,但图像也没有出现。我只得到在paint方法中绘制的灰色方块。如果需要更多信息,请告诉我。
编辑:为了清晰起见,更改了一些变量名称