Is there a need for Canvas? when simply using drawImage()

I am in the process of making a simple 2d game engine. My original setup consisted of a JFrame with a Canvas and then using .getVSyncedBufferCaps() option when creating my buffer strategy to get smooth 60fps in windowed mode. I draw all my stuff to an int array which is tied to a buffered image which then gets drawn. I’ve been reading more and more and even though its not really an issue anymore there is this debate about mixing AWT and Swing components. So just for the heck of it I removed the Canvas and used a JPanel. Since I couldn’t get the buffer strategy of the JPanel I looked online and a few examples were passing in a copy of the JFrame to the class extending the JPanel and then getting the buffer strategy from that. I then realized… whats even the point of having the Jpanel so I moved my render code (which simply draws a buffered image to the graphics context into the JFrame and removed the JPanel all together and sure enough everything was still working. So my question is why would it be necessary to even place a Canvas or a JPanel in a JFrame when I can simply get the BufferStrategy of the JFrame, then getDrawGraphics and draw on that? So far the only downside I’ve noticed is that my coordinates are offset and start at the top left of the actual window instead of the contentPane, but that can be accounted for easily.

[quote=“QuantumFlux42,post:1,topic:43913”]
There’s your reason. Further, if you want to add other components to your JFrame you’re going to draw it ontop of your game. Instead if you’re using a canvas you can use a layout manager to set stack them on top of each other or w/e.

Why would you manually account for the JFrame top bar yourself in your game code that has nothing to do with it whatsoever when you can just as easily use a component that does that for you?

I was able to fix the offset by using the getInsets() method. I don’t intend to use a layout manager or add any other components to the JFrame. This is more of a learning experiment anyways as I try to understand and learn the nuances of Java. So it sounds like if I am just drawing the buffered image there is no downside to doing it directly in the JFrame as opposed to the Canvas. I’m also assuming that there is no benefit to doing it this way either as I’m sure the canvas probably doesn’t add any significant overhead.

Now lets say I wanted to stay strictly swing and not rely on the Canvas component which is AWT… Is there a proper way to use either a JPanel or JComponent with a buffer strategy as opposed to simply overriding the paint method. Or would it be preferable to switch to AWT and use Frame with Canvas instead.

I’ve read that Swing is a lot faster than AWT (even though that’s weird to me because isn’t swing built on top of AWT). And also from the looks of it, since Java 6 there shouldn’t be issues mixing AWT (canvas in this case) with Swing (JFrame) components at all anymore right?

Swing is basically meant for GUI’s - built on top of AWT. And AWT is there to give you basic tools for drawing stuff. Also, Swing components are double buffered (uses a buffer strategy, if you will) by default. Swing uses its own thread to draw stuff iirc and so you should use/override its update/paint/paintcomponents - all of which serve a specific purpose. [HINT: Read the Swing Manual)

[quote=“QuantumFlux42,post:3,topic:43913”]
It’s not really much of a learning experiment in Java - only a meddling with the dark arts of Swing.

If it ain’t broke - don’t fix it.

Makes sense, thanks for your quick replies ;D now to continue working on my engine.