Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JFrame & JPanel Differences with Repaint()

I have been playing about with some simple painting of Graphics2D and have some extremely good help from the community here.

I managed to get the flickering resolved from my "bouncy balls" by moving the code away from the main JFrame class and into a JPanel which I then added to the JFrame class, can anyone tell me why this would make such a difference?

like image 357
Daniel Avatar asked Apr 24 '26 13:04

Daniel


1 Answers

When you draw in a JComponent's paintComponent method (such as a JPanel's), you use Swing which uses double-buffering when drawing by default. Drawing directly in a JFrame's paint method will only allow AWT type drawing since the JFrame directly inherits from Frame, a heavy weight container, and since AWT graphics does not use double buffering by default and this will lead to choppy animation.

like image 67
Hovercraft Full Of Eels Avatar answered Apr 26 '26 02:04

Hovercraft Full Of Eels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!