Should I be using java.awt for games?

If not, what should I use instead? I have an example from brakeen’s developing games in java and it imports a lot of java.awt packages. I’m told that awt is filthy and slow?

For beginners, it is recommended to start with Java2D (java.awt, java.awt.geom, java.awt.image)
From where did you hear that Java2D is filthy? It might be a bit slow on some CPUs and graphics cards (especially Intel since there is no hardware acceleration), but it runs perfectly fine.

As you advance, you should start learning OpenGL by using LWJGL, JOGL, Slick2D, etc…

I just wasn’t sure if there were swing alternatives.

If you’re a newbie at game programming, the odds of Java2D being your bottleneck are infinitesimal.

In general, Java2D is perfectly fine for most 2D games you might want to create (platformers, shooters, etc.). If you want to create a 3D game, it simply isn’t the tool for you (hence the name); there are better tools for that, such as LWJGL and JOGL.

Slick2D is a popular alternative to Java2D with a similar API. It uses LWJGL under the covers and may provide better performance overall than Java2D. The tradeoff is that you have to deliver native code with your game (not a problem for desktop games, but annoying for applets, and complicates things if you’re a newbie).

Which is why I mentioned Java2D for beginners, then once they’re comfortable with their skills, they may venture into OpenGL with Slick2D and finally into 3D with LWJGL, etc…

Swing is only for GUI, like buttons, text boxes, drop down lists, etc…
AWT GUI tools use the underlying OS to draw the components, so in Windows they would be gray and blocky, while with Swing, you can customize as much as possible.

Thanks for answering my questions guys. That’s mainly what I wanted to know was if I was going to run into issues kixing canvas with swing or if there was a swing class i could use instead. all of the key listeners are awt as well, are they not?

Never mix Swing GUI Components with AWT GUI components, that could lead to bad things.
If you want to use Applet or Frame, use a Canvas to draw.
If you want to use JApplet or JFrame, use JComponent to draw.

And yes, look into java.awt.event for all listeners. You can also look into javax.swing.event which has some useful listeners like MouseInputListener, which combines MouseListener and MouseMotionListener.

Glad to help ;D

When you say draw do you mean for animation? Like, if I want a decent animation thread, I should be using System.getNanos(cant remember the name and im on my phone) and should be drawing on just a regular jframe with a content pane of jcomponent? since that is the root of all swing components does that mean i could draw on a panel? What graphics objects should i use? Is it necassary to implement page flipping nowadays or can is there a prebaked solution?

If you want to use Swing, you should create a class that extends JComponent and draw in its paint(g) method.

And there is only 1 Graphics object: Graphics2D. Graphics is used for backwards compatibility but essential all Graphics objects you get are of Graphics2D and you need to cast to use it: Graphics2D g2 = (Graphics2D)g;

And yes, there is prebaked solution for double buffering: BufferStrategy :smiley:
All you do is createBufferStrategy(int) on your JComponent or JFrame and then getBufferStrategy() to get the BufferStrategy object.
You manually call paint(g) with the Graphics object you get when call getDrawGraphics() and then show() on to show the image on the screen. Both those methods are in BufferStrategy.

PS: It’s System.nanoTime() but careful, it is ONLY used for measuring difference between two points in time. It is precise, but not accurate :wink:

Hope that helped ;D

Perfect! Thanks!

Glad to help :slight_smile:

OK, I thought drawing on JComponent was automatically double buffered. I know drawing on JPanel is. No need to bother with BufferStrategy there. Just make the first draw command a clear and draw your new positions after that. Very simple for beginners, and works fine for puzzle games and simple animations.

I have Hexara http://www.hexara.com/gameNS.html set at repaints on a JPanel every 33 msec. It’s pretty simple and works fine. When you click and drag the tiles around, handling is smooth. No stressing nanowhatsis values either. Just send in repaints via a Timer (after triggering the updates with the same Timer). It works. It’s pretty easy. :slight_smile:

That’s correct. All JComponents are double-buffered, AWT components are not. That’s one of the benefits of using Swing. To get double buffering with an AWT component, you have to use the Canvas/BufferStrategy approach.

Two reasons people still recommend using Canvas/BufferStrategy for games is that you usually don’t use pre-baked components in a game, but rather roll your own, and using AWT saves the overhead of loading Swing stuff (which is unnoticeable to a user, but you do load a LOT of stuff as soon as you create the first JComponent - LookAndFeel-related classes).

Quick fix on my post, createBufferStrategy is ONLY available for java.awt.Canvas and java.awt.Window (and its subclasses: JWindow, Frame, JFrame).

As BoBear said, all JComponent’s are already double buffered but JFrame is NOT, so if you want to use JFrame or JApplet, you need to draw on a JComponent so you wouldn’t have to worry about double buffering.

I think it’s common practice to be only doing things to panels or components in the contentPane of a frame.

No the best practice is to draw on a Canvas or a JComponent that you add to the content pane of a JFrame or JApplet.

That’s what I mean, maybe I didn’t word it well enough. :smiley:

So, which should I use, JComponent + JFrame or Canvas with Frame? Which is the better way to work/learn/better performance/is what all the cool kids do/ and why?

You can add both to JFrame.

JComponent - automatic double buffering, easier, good for beginners
Canvas - no double buffering, tricky to set up set up with double buffering, recommended to progress into.

TL;DR

No, Swing/AWT is a powerful GUI toolkit with a bit of a clunky API. It is even worse trying to make games with it. Use libgdx! :slight_smile: