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...

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).

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.

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 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

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.

OK, I thought drawing on JComponent was automatically double buffered.

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.

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

Admittedly I'm pretty ignorant of the inner workings of Swing and AWT, but I'm going to take a bit of convincing that the "JComponent is just like Canvas+BufferStrategy but easier to use" argument is sound.

How are you triggering the buffer-flip for the JComponent? Through repaint()?

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

I actually disagree. If you don't know how to use Java2D correctly so it isn't hardware accelerated, it is incredibly slow.

What do you mean, "use Java2D correctly?" Do you mean things like "use Canvas/BufferStrategy for page flipping" and "create compatible images, stuff like ImageIO creates non-accelerated images"? If so then I guess you're right - that's not stuff a beginner would know, without having sources such as this forum. And it sucks that it's an API where there's many ways to do the same thing, some just yield slower results than others.

I still feel like, once you know a couple of basic pointers like those, most common sprite-based games (i.e., stuff a beginner should probably start with) can generally run on any hardware made in the past several years.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org