I'm currently learning computer science in high school and using "ReadyToProgram Java". We're trying make a pong game using basic shapes and classes etc. The way my game updates its view is to clear the whole screen and then redraw the paddle and pong in a while loop. It's flashes constantly! When I used Turing in the past grade, we had a command called view.update, where it runs the program off screen and only when you use view.update the program will update the screen. Is there something similar in java? Thanks!
-
Are you using Swing? – PM 77-1 Nov 23 '14 at 14:28
-
no, only readytoprogram java. It's a basic platform for learning how to program in java. – ProgrammingGangsta Nov 23 '14 at 14:32
1 Answers
The concept you're looking for is known as double buffering. In general, the concept is that you have an off-screen buffer that you do your graphical operations to, then copy the off-screen buffer to the on-screen display.
I don't know exactly what graphical toolkit you're using for your work. AWT can do this with Graphics.drawImage(). See Double Buffering with awt for one discussion of how to do this. In Swing, this can be handled with JComponent.setDoubleBuffered()
If you are using something else, you may wish to look it up using the exact phrase "double buffering".
Update:
The other answer, of course, is to not clear the entire screen and redraw. Given how the pong objects move, you should be able to clear just the portion of each paddle and ball that has moved.
-
Thanks I'll look it up! As for your second solution, I was thinking about doing it that way, but the assignment requires me to print the score as the players are playing. The only commands I know are print and println. So if a player scores the score will be printed after the old score, I've tried pinrting "/b/b/b/b" but it doesn't seem to delete the old score and only print /b/b/b/b haha. So my only option was to clear the whole screen in order to be able to update the score and have it in the same location. If you know a solution to this, it would be amazing! – ProgrammingGangsta Nov 23 '14 at 14:43