我正在使用awt创建一个java applet。我知道它已经过时但是我们的教授要求我们使用它,所以请耐心等待。我的问题在于图像的分层。截至目前,为了将图像叠加在另一个上面,我必须在显示其他图像之前先显示我想要在另一个上面的每个图像。这使事情变得非常复杂和混乱。有没有办法(媒体跟踪器或其他东西)使图像图像1 始终位于另一个图像图像2 之上?
此外,我正在制作的节目是一款地下城爬行式游戏。屏幕由5x5区域的图像块填充,每块32像素。类似的东西:
[W][W][ ][ ][W]
[W][ ][ ][ ][W]
[W][W][W][ ][ ]
[ ][W][ ][ ][ ]
[ ][ ][ ][ ][ ]
其中[W]是墙图像,[]是路径图像。问题是,我希望这组图像始终显示,并且只在他移动图像时重新绘制角色。是否有一种方法可以在调用重绘时不会清除这些图像?
这是我的代码:
public class GUI extends Applet implements KeyListener {
int imageSize = 32;
int dimensions = imageSize*5; //makes the display 5x5
Image pathImage, wallImage, playerImage;
MediaTracker mt;
public void init() {
this.setSize(dimensions, dimensions);
mt = new MediaTracker();
pathImage = getImage(getCodeBase(), "path.gif"); //all the images are stills
wallImage = getImage(getCodeBase(), "wall.gif");
playerImage = getImage(getCodeBase(), "player.gif");
mt.addImage(pathImage, 0);
mt.addImage(wallImage, 1);
mt.addImage(playerImage, 2);
addKeyListener(this);
}
public void paint(Graphics g) {
try {
tracker.waitForAll();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//this first one prints the first three layers from the top of the 5x5 display
int x = 0;
int y = 0;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 5; j++) {
switch(Global.currentMap.getTileMap(player.getXCoordinate() + j - 2,
player.getYCoordinate() + i - 2).getImageID()) {
case "path":
g.drawImage(pathImage, x, y, imageSize, imageSize, this);
break;
case "wall":
g.drawImage(wallImage, x, y - imageSize/2, imageSize, imageSize*3/2,
this);
break;
}
x += imageSize;
}
y += imageSize;
x = 0;
}
//this one draws the player so that it's on top of the top 3 layers
g.drawImage(bird[face], imageSize*2, imageSize, imageSize, imageSize*2, this);
//this one draws the last two layers so they're on top of the player
for(int i = 3; i < 5; i++) {
for(int j = 0; j < 5; j++) {
switch(Global.currentMap.getTileMap(player.getXCoordinate() + j - 2,
player.getYCoordinate() + i - 2).getImageID()) {
case "wall":
g.drawImage(wallImage, x, y - imageSize/2, imageSize, imageSize*3/2,
this);
break;
case "path":
g.drawImage(pathImage, x, y, imageSize, imageSize, this);
break;
}
x += imageSize;
}
y += imageSize;
x = 0;
}
}
上面的代码不是相同的代码。但我希望它说明了我必须对图像进行分层的观点。
以下是示例输出: http://imgur.com/PjW6eTo
感谢。