爪哇 - 让显卡 [英] Java - get Graphics
问题描述
即时制作的Java Swing的游戏。我听说Swing组件不使用主动渲染(你只能重写paint方法),因为这个原因,我一直在使用BufferStrategy中与画布已经。现在我有发现从JComponent中和的JPanel中的的getGraphics()
方法。如果我们能做到积极渲染Swing组件,为什么游戏教程还覆盖的paint()
和的paintComponent()
<? / p>
永远不要使用的getGraphics
,它可以返回空
键,无非是最后的油漆周期的一个快照。
你画给它任何将在下一个周期的油漆被删除。在Swing你不控制油漆工艺和油漆周期可以为任意数量的原因,其中有许多你不拥有控制权或将通知启动(其他然后当油漆被称为)
最基本的答案是,如果你想在涂料工艺控制,你必须使用 BufferStrategy使用
或者实现你自己的关闭屏幕绘图程序。还有,通过它可以了Swing API中实现了真正活跃的喷漆工艺没办法,你可以把它假在一定程度上,但摆动仍然能够执行它自己的绘画周期,当它认为合适的。
看一看在AWT 绘画和Swing 和<一个HREF =http://docs.oracle.com/javase/tutorial/uiswing/painting/相对=nofollow>表演风俗画了解更多详细信息,有关如何在Swing绘画作品
im making a java swing game. I heard that swing components don't use active rendering(you can only override paint methods), and for that reason, i have been using BufferStrategy with Canvas. Now i have discover the getGraphics()
method from JComponent and JPanel. If we can do active render in swing components, why game tutorials still override paint()
and paintComponent()
?
Don't EVER use getGraphics
, it can return null
and is nothing more the a snap-shot of the last paint cycle.
Anything you paint to it will be removed on the next paint cycle. In Swing you DON'T control the paint process and a paint cycle could be initiated for any number of reasons, many of which you don't have control over or would notified of (other then when paint was called)
The basic answer is, if you want control over the paint process, you MUST use a BufferStrategy
or implement you own off screen drawing routines. There is NO means by which you can achieve a true active painting process within the Swing API, you can fake it to a certain extent, but Swing will still be able to perform it's own painting cycles when it sees fit.
Have a look at Painting in AWT and Swing and Performing Custom Painting for more details about how painting works in Swing
这篇关于爪哇 - 让显卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!