Quick Question

I’m currently learning java so I can build a game and I have a little question.
When wanting to create a java game window that shows up on the users desktop, what are the best packages to use: Swing or AWT?

Also can someone please explain the difference between the JPanel, JFrame, and JWindow?
Thanks

JUST SAYING, I MAY BE WRONG, THIS IS JUST WHAT I HAVE HEARD!

A JPanel goes inside a JFrame/JWindow.
A JFrame/Window is the actual container.
A JWindow can be fullscreened (but is a little glitchy)

I recommend just using JPanel and JFrame unless you want fullscreen

Obviously, I favour Swing over AWT, but in the end (LWJGL) Display WINS!*

*For games, not GUI. :wink:

I pretty much agree with HeroesGraveDev. I also recommend Swing.

I’ve got the following link bookmarked, as I find it hard to remember all the Swing components and their details.
http://docs.oracle.com/javase/tutorial/uiswing/components/index.html

A JFrame is a “Top Level Container”. You can get a good explanation of JFrame in the section: “Using Top-Level Containers.” I’ve never used a JWindow. I should look into it!

A JPanel is a container in which one can draw and/or put other components (such as buttons, sliders, textfields, etc.). It is possible to draw on a JFrame, but you have to get to a special layer of JFrame. I find it simple enough to just add a JPanel to a JFrame and go from there. Separating out the “top-layer” functionality from the game play is most often the right way to go anyway. If your game is mostly on a JPanel, then it is easier to make top-level changes such as creating an Applet version of the game.

If you are only intending to draw animations, and not place any components like buttons or labels, one can use a JComponent instead of a JPanel. But for getting started JPanel’s are a pretty decent, all-purpose solution.

Welcome to JGO! I hope you find a lot of great info and inspiration here.

I prefer Canvas over JPanel for the actual game window.

Here you go another Canvas supported. Me! I never do a benchmark but with my feel I can tell canvas is faster, at least here.

Why use the higher abstraction layer of the JPanel, if the lower one gives you everything you need: Use Canvas (IMO, somehow “convention” too)

Whoa so much confusion in this thread. Here’s a brief overview:

  • Swing is built on AWT. Swing draws its components using AWT’s Graphics object, so there’s no point in using Swing unless you want a “pretty” and cross-platform GUI (buttons, dropdowns, etc…)

  • @philfrei, JFrame extends JWindow, so they are virtually the same except JFrame has the decorations (title bar, border, X/min/max buttons)

  • JPanel is a container used for organizing GUI elements, NOT to draw into as millions of online tutorials teach >.>

  • Since Java2D (AWT) is somewhat slow, it’s best to not use Swing (you don’t need it, it’s best to build your own GUI) and use the purest abstraction: java.awt.Canvas as mentioned by jonjava and co.

For more information on how to make Java2D games, read this post.

For more information on LWJGL, read this post.

@ra4king – Thanks for clearing things up. But I think saying JPanel is NOT for drawing upon is slightly inaccurate. JPanel allows you to draw on it! Why would a component that allows drawing NOT be for drawing? True, there are other components that specialize in drawing (I mentioned JComponent in my earlier post in this regard) and are to be preferred if you are only drawing. But if you want to mix in some drawing with some components, JPanel is “OK”. That is part of why it is characterized as being “general purpose”.

IMHO, for someone just getting started with Java, using JFrame and JPanel is fine. There is plenty enough to learn and accomplish without needing to get into the intricacies of the Full-Screen Exclusive Mode API. But sure, Canvas is preferred for advanced graphics & animation, as is explained in this tutorial:

http://docs.oracle.com/javase/tutorial/extra/fullscreen/index.html

I just think it is good to have a footing in Java basics before tackling this tutorial.
We can agree to disagree. :-*

All Swing and AWT components allow you to draw, none of them are final and their paint/paintComponent methods are there :wink:

However, it is not meant for drawing.

Either way, there isn’t much point to bother with AWT/Swing these days. Just jump onto LWJGL through any of its high level libraries like Slick2D then learn the low-level intricacies of OpenGL.

Idk…I think I will just stick to g2d.drawImage(blah blah blah) and everything just magically appears on the screen. :wink:

JFrame doesn’t extend JWindow! JFrame extends Frame which extends Window, and JWindow extends Window. JWindows are really aimed at non-top-level windows - ie. windows that have a parent window. They shouldn’t show up in window lists / task bars, and are used for things like heavyweight popups (menus, tooltips, etc.)

It is worth noting that as well as not showing up in window lists, a JWindow without a parent is not focusable. If you want an undecorated top-level window you should really use a Frame or JFrame and setUndecorated(true). A JWindow is not just an undecorated JFrame!

Ah it extends AWT’s Window, not JWindow. Excuse my poor memory :stuck_out_tongue:

Well, I know it’s not an age thing! :stuck_out_tongue:

Actually, it’s more of an issue telling people they’re “virtually the same” - they’re treated quite differently in places by the OS.