我正在製作一個玩家在迷宮中漫遊的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