Java JFrame Double Buffering Problem

Posted 21 June 2012 - 02:06 AM

Hey guys, my friend is making a minesweeper game in Java using JSwing and his code isn't working. The code compiles correctly but the image isn't displayed correctly. Before he had the double buffering implemented, it displayed fine, but with it, it goes off the screen in the left top corner and the correct tile that is being hovered over isn't being displayed correctly. Its high lighting the tile above the cursor. I don't understand how adding double buffering broke the program but it would be very nice if someone could please tell me what is wrong with the code

Replies To: Java JFrame Double Buffering Problem

Re: Java JFrame Double Buffering Problem

Posted 21 June 2012 - 03:56 AM

JPanel are double buffered by default you complicate your life a lot for nothing.
You should only implement double buffering if you play a very intensive application like in:http://www.dreaminco...way-to-repaint/
but let's assume it is an academical exercise.

That does not require Thread, use a swing.Timer instead. Swing is nt thread safe, without having look at your code to deeply but I guess you hang in a thread and your paint() method is never called.
When you do your own repaint you have to setIgnoreRepaint(true) in your Canvas.

Also, if I take the double buffering out of the program, then the program flickers a lot. So, I think that the double buffering is needed. Do you see anything else wrong with the program that would cause it to render like this?

Re: Java JFrame Double Buffering Problem

Posted 21 June 2012 - 03:42 PM

And by the way, a Timer of 16 millisecond is useless.
The hardware that refresh the screen executes every 16.6 milliseconds, so a Timer that calls back at less than 18-20 millis will simply draw for nothing

Ok get MineField and Tile from a previous post, if they haven't changed.
Still need Constant

Also we are playing with milliseconds here and each of them is important
In Title not very good idea to

g.setColor(new Color(0, 255, 0, 100));

create that Color only once, probably as a static variable shared by all instance
No need to create a new Color object every time tile.render() is called