简体   繁体   English

Java JFrame & JPanel 与 Repaint() 的差异

[英]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.我一直在玩一些简单的Graphics2D绘画,并从这里的社区获得了一些非常好的帮助。

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? 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?

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.当您在 JComponent 的 paintComponent 方法(例如 JPanel 的)中绘制时,您使用 Swing,默认情况下在绘制时使用双缓冲。 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.直接在 JFrame 的 paint 方法中绘图将只允许 AWT 类型的绘图,因为 JFrame 直接继承自 Frame,一个重量级容器,并且由于 AWT 图形默认不使用双缓冲,这将导致 animation 不稳定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM