Java graphics - Stages of a game

前端 未结 1 927
夕颜
夕颜 2020-12-03 23:28

This is a topic that has been bugging me recently.

When creating a game in a JFrame, and you want to add the graphics to a JPanel with the paintComponent()

相关标签:
1条回答
  • 2020-12-04 00:01

    "Abandon all hope, ye who enter [through Swing to create games]!"

    To answer your question, JPanels are just a surface to draw on, like a chalkboard, or a piece of paper. They have nothing to do with stages, or anything else other than being a thing you can draw on, and a container for other sub-components.

    To address the game-specific question, I'd recommend you don't waste your time using Swing components to build a game interface. While it's possible, you're going to run into a bunch of performance problems that will just eat up your time. Based on your question you're already thinking about low-level stuff like Timers to schedule things, and overriding the paintComponent() method - a very common mistake, and a road that many a failed project spend time on just to find out it's not the best way.

    If your question were literally a path in a forest that you could see, you would notice these things:

    • It is well trodden (as it seems to be the most direct approach)
    • Just around the corner are several spiky death pits of performance, and obscure, non-cross-platform compatible tweaks
    • You have to figure out how to use Swing's painting manager to do what you want, when in most cases for most games, you want direct, deterministic control over your rendering pipeline/process
    • It is littered with the bodies of projects that try to use Swing for games, for weird (and silly) reasons such as, "I wanted to build a game that only uses the standard Java library." Well, the standard library is nice and all, but at its heart it was designed to solve business problems, and be really good at server and networking software, among other things. If you're not using at least some gamedev libraries, you're probably missing out on using the right tool for the right job. Swing was definitely not built to be used by games, as it serves basically no needs that game developers have, unless you're talking about the desktop apps/tools to build and edit levels and that sort of thing.

    Instead, use something like Slick2D (for 2D games), or jMonkeyEngine for 3D shtuff. These libraries are designed specifically for building games, and are going to save you from having to reinvent the proverbial wheel, just to get your game up and going. They also have their own communities that can help you with library-specific issues.

    On top of that, they have already solved some of the trickiest cross-platform issues, like dealing with various versions of OpenGL, and other technical details that I'll bet you don't really care about when at the end of the day, what you really want is, "Hey, just give me an [arbitrary resolution], graphics-accelerated surface to draw on, and don't make it complicated!"

    0 讨论(0)
提交回复
热议问题