KeyListener, MouseListener and repaint problem [solved]

I was working to create a Sudoku game using JFrame and JPanel.

I wasn’t sure where the mistakes are but when I tried to press a key, the key listener works but it’s not updating the graphics. It seems that the Panel only redraw it once but I did repaint all the tiles in a while loop.

Here is a sneak peak of the code :-

Sudoku class aka. main method
Tile class

It’s not too long cause it’s just a simple game and I didn’t start working into the logic yet.

You need to call repaint() on your JPanel whenever you want to redraw stuff.

I’ve heard using repaint is quite resource demanding and not recommended.
The way I’ve always made my game loops is to render to a BufferedImage and the render this image to the JFrame/JPanel. This prevents tearing and other problems too.

Where did you hear this?

That solution doesn’t make a ton of sense to me. Swing is already double-buffered so you shouldn’t have to do that yourself.

I think I read this is several StackOverflow questuons/articles (?). And yes, SWING has double-buffering but I found it not working correctly in many occasions, updating mid-way through the rendering process and causing stuttering and tearing.
I wanted to offer a solution that works, even though it isn’t the best option. It’s just, that I’ve seen this problem many times and nothing else seemed to help. I would be glad to finally hear about an more elegant solution than mine.

It sounds like you were drawing from two different threads. This is generally a bad idea.

Anyway you should probably just use a library that handles all of this stuff for you, like Processing (for beginners) or libGDX (for more advanced users).