Drawing something in front of any other object/drawing
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.
That is what the GlassPane is for.
JComponent glass = myframe.getGlassPane();
glass.setOpaque(true);
glass.setBackground(Color.WHITE);
glass.setVisible(true);
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.
精彩评论