在任何其他物体/图画前面画一些东西
我正在制作一个小游戏,我需要更多帮助。我想这样做,以便如果玩家 1 和玩家 2 完成,在任何对象或绘图前面的整个屏幕上都会弹出一个白色屏幕。我现在正在使用它作为代码:
if( isFinishedP1 == true && isFinishedP2 == true ){
Graphics2D b = buffer.createGraphics();
System.out.println("Both are finished, drawing WhiteScreen!");
b.setColor( Color.WHITE );
b.fillRect(0, 0, 800, 600);
b.dispose();
}
我的控制台说它们都已完成,但它不会绘制白屏。我没有看到任何东西,而且我怀疑它是在背景和物体后面绘制的。我将白色屏幕(实际上是一个矩形)放置在 (0,0)(分别为 x 和 y 坐标),我的窗口为 800x600(宽度 x 高度)。
如何在另一个对象前面绘制矩形,或者有更好的方法吗?白屏的目的是充当“游戏终局屏幕”,您可以在其中选择是否要再次执行或进入下一个级别。当我执行这段代码时没有错误。
I am making a small game, and I need some more help with it. I want to make it so that if Player1 and Player2 finished, a white screen will pop up over the whole screen in front of any object or drawing made. I'm using this as code at the moment:
if( isFinishedP1 == true && isFinishedP2 == true ){
Graphics2D b = buffer.createGraphics();
System.out.println("Both are finished, drawing WhiteScreen!");
b.setColor( Color.WHITE );
b.fillRect(0, 0, 800, 600);
b.dispose();
}
My console says that they are both finished, but it won't draw the white screen. I don't see anything, and I have my suspicions that it's drawing behind the background and objects. I place the white screen, which is actually a rectangle, at (0,0) (x and y coordinates respectively), and my window is 800x600 (width x height).
How do I draw Rectangles in front of another object, or is there a better way to do this? The purpose of the white screen is to act as a "endgame screen" where you can select if you want to do it again, or go to the next level. There are no errors when I execute this code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是 GlassPane 的用途。
That is what the GlassPane is for.
在您展示的代码中,您只是绘制到某个缓冲区中,而不指定它来自哪里以及去往哪里。
一般来说,您应该覆盖 Paint 方法,如果您在其他所有内容之后绘制一个白色矩形,它看起来就像在其他所有内容之上。
In the code you are showing you just draw into some buffer whithout specifying where it comes from and where it goes to.
In general you should overwrite the paint method and if you draw a white rectangle after everything else it will look like it is on top everything else.