Difference between using Canvas and JPanel for a 2D game engine (and Buffering?)

≯℡__Kan透↙ 提交于 2019-12-12 04:37:21

问题


I learned from various tutorials to use a Canvas on top of a JFrame to render images for a 2D Game Engine. I had to create a BufferStrategy to keep the image smooth and such, but recently I've heard using a JPanel instead to render images. Should I use JPanel? If so, do I still need to create a BufferStrategy or is it integrated. Am I just really misinformed and am I even making sense? I'm a beginner so feel free to try to correct me and guide me- I am very overwhelmed with this for some reason :P Thanks.


回答1:


BufferedStrategy

Uses "active painting". It gives you direct control over when something gets painted

  • Provides better control over the frame rate
  • Has a low level connection, and provides access to the, hardware acceleration

Swing

Uses "passive painting". There is a RepaintManager which is in control of scheduling when and what gets painted, this is posted onto the Event Dispatching Thread and get's processed along with all the other events.

  • No direct control of the painting system
  • Variable frame rate

Both

Are linked through the same rendering pipeline, typically this is either DirectX or OpenGL

Game Engines

Depends. Most game engines HAVE to start with a Canvas, as this is generally the only way to link Java to the underlying rendering engine. Most game engines are low level native bindings or light wraps over DirectX or OpenGL, giving you access to those APIs (in)directly

A few engines may still use Swing directly, but they would be limited and may be intended for situations where you don't need 3D or a high/consistent frame rate.

Chooice

This is a matter of personal opinion. Do you need a constant frame rate? Do you want to do 3D or is 2D enough? Do you want to perform low level functionality (texture manipulation etc)?

Useful references

  • Passive vs. Active Rendering
  • Painting in AWT and Swing


来源:https://stackoverflow.com/questions/43441169/difference-between-using-canvas-and-jpanel-for-a-2d-game-engine-and-buffering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!