我正在制作一个玩家在迷宫中漫游的2D游戏。 Swing简单覆盖
我想实现某种形式的“黑暗”,甚至一些周围玩家一个透明状黑色包围一样简单,像这样的:
我用摇摆中发现的问题尽管这是可能的,但这意味着必须重新绘制所有内容,每当它发生时就会产生恼人的“闪烁”效果。有没有办法做出某种叠加,或者只是在Swing中做这些事的一个好方法?我现在对图形用户界面/可视化的东西不是很有经验,所以如果可能的话,我想坚持用Swing。
编辑:这是我的方法来绘制背景,即在地板,墙壁和退出:
public final void paintBG(Graphics g){
g.setColor(Color.LIGHT_GRAY); // Screen background
g.fillRect(0, 0, getWidth(), getHeight());
// Draw the Walls of the maze
// scalex and y are for scaling images/walls within the maze since I let users specify how big they want the maze
for (int j = 0; j < this.height; j++, y += scaley) {
x = 20;
for (int i = 0; i < this.width; i++, x += scalex) {
if (!(maze[j][i].northwall.isBroken())) // If the north wall isn't broken
{
g.drawImage(walltile, x, y, scalex, scaley/5, null); // Draw a wall there (image, xpos, ypos, width, height, observer)
}
if (!(maze[j][i].eastwall.isBroken())) // etc
{
g.drawImage(walltile, x + scalex, y, scalex/5, scaley, null);
}
if (!(maze[j][i].southwall.isBroken())) {
g.drawImage(walltile, x, y + scaley, scalex, scaley/5, null);
}
if (!(maze[j][i].westwall.isBroken())) {
g.drawImage(walltile, x, y, scalex/5, scaley, null);
}
if ((j == mazeinfo.getTargetM()) && (i == mazeinfo.getTargetN())) {
// Draw the exit
g.drawImage(jeep, x + (scalex/2), y + (scaley/2), cx, cy, null);
g.setColor(Color.LIGHT_GRAY);
if (maze[j][i].northwall.isEdge()) {
// Paint over the edge creating a 'way out'
g.fillRect(x, y, scalex, scaley/4);
} else if (maze[j][i].eastwall.isEdge()) {
g.fillRect(x + scalex, y, scalex/4, scaley);
} else if (maze[j][i].southwall.isEdge()) {
g.fillRect(x, y + scaley, scalex, scaley/4);
} else if (maze[j][i].westwall.isEdge()) {
g.fillRect(x, y, scalex/4, scaley);
}
}
}
}
}
我再有“paintPlayer”和“paintEnemy”的方法每次移动时画的精灵。背景仅在开始时绘制一次。
谢谢你的代码 - 请出示您的组件的实际油漆或的paintComponent方法以及。 –
见编辑回答。 –
对于[示例](http://stackoverflow.com/questions/15488853/java-mouse-flashlight-effect/15489299#15489299),[示例](http://stackoverflow.com/questions/15309611/how-to -draw-a-transparent-background/15309868#15309868),[示例](http://stackoverflow.com/questions/23709070/how-to-disable-java-awt-graphics-fillrectint-x-int-y- int-width-int-heights/23709320#23709320),[example](http://stackoverflow.com/questions/18388942/clear-portion-of-graphics-with-underlying-image/18392674#18392674) – MadProgrammer