Differences in extends JComponent, JFrame, JPanel, & Canvas (mega noob question)

Hi JGO,

I am writing this thread because I have a question about extending JComponent, JPanel JFrame, and Canvas. In many different tutorials, I have noticed authors extending these three different classes in their main class. Each of these’s authors are extending different classes and now it has me wondering which ones I should be extending and why. Do you choose one of these classes depending on what kind of game your going to make? All tutorials that I have read have only been 2D so far. I have read it is important to learn 2D concepts and programming techniques before moving on to 3D. Also I have been learning swing from one of my books, but is it necessary to learn this if I am mainly going to write my own ui components? Should I still learn swing regardless?

If you have experience in this and you don’t mind helping a novice out, then I would sure appreciate your help. I hope this is relatively easy to answer.

Cheers,
Andrew

Using my somewhat limited knowledge of AWT/Swing/Java2D, here is what I know/assume:

JFrame is a window.
JPanel is a general purpose GUI container and/or component.
JComponent is the base class for most GUI component.
Canvas is a special AWT component that has some support for double buffering.

I recommend using Google for more reliable info.

JComponent/JFrame/JPanel are part of the Swing gui system, which is newer than AWT, the old gui system (that Canvas belongs to). JPanels are double-buffered by default.

Learning Swing is extremely easy and worth the effort. Even if you do switch to a GUI builder it’ll still be helpful info.

Swing is built on top of AWT!

More important feature of Canvas is that you can get a BufferStrategy from it and therefore use it for active rendering.

Would you please explain which is the “best” solution for developing games (and why)? 2d games specifically.

Thanks for the help folks! You guys rock! 8)

I just realized something, I don’t think anyone specifically answered your questions directly. I think it is mostly because Java is a programming language… not a game maker. In other words, those components (like JFrame, JComponent, etc.) just puts a frame into the screen while you, the coder, has to provide the artwork and logic.

[quote=“unenergizer,post:5,topic:45119”]
I think you may be asking the wrong question as… all of these play a part in getting a basic screen to show. There is no technical “best” solution, though double-buffering does help a lot with animation. In other words, whichever one helps you get a screen that you can design on the fastest would probably be the best.

[quote=“unenergizer,post:1,topic:45119”]
If you are using Java2D, you will be using one of these to create your game. None of these are better than the other solution as they are not based on speed. Some have double buffering which helps you during animations, but that is about as much as you’ll get from a frame. You can also control screen-size with it, but you have to be careful as some of the Swing components scale your work while others do not. Research through Google or follow your tutorials closely.

[quote=“unenergizer,post:1,topic:45119”]
This highly depends on what you are coding for…

If you are in it strictly to make games, I’d say just find the first solution and go with that.
If you are in it to learn more about Java in general, then by all means, learn Swing.

Writing your own UI components is fine. I usually don’t like the layouts that Swing gives to me by default anyway because I find them too limiting. Keep in mind that rolling your own components will ultimately take you longer to actually produce something.

Keep in mind any of these solutions are fine to use, and it won’t affect performance for your 2D game…

Oh, and I generalized a lot in this post because they were very technical in the posts above. So, if I were you, I’d pick my favorite tutorial and start coding. There is a lot more to game making than just the screen to use. You still have to look at game loops, animation, sound, controls, etc.

@ ctomni231. AAA+ Answer sir, thank you.

I definitely want to be a professional java developer and I know I need to learn swing. I think the excitement of learning game development makes me want to push everything else aside. I will do further research on the subject. When I start making my own 2d games I want them to be super polished and animated. I have seen sprite sheets for many snes games and I know what it takes, a lot of work. So I will choose the best for animation unless I learn otherwise. I have the core basics down and I am migrating into graphics, so I apologize if I ask “silly” questions.

Thanks again!