Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Is there a need for Canvas? when simply using drawImage()  (Read 323 times)
0 Members and 1 Guest are viewing this topic.
Offline QuantumFlux42

Junior Newbie





« Posted 2013-08-30 20:06:20 »

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.
Offline jonjava
« Reply #1 - Posted 2013-08-30 20:33:30 »

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.

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?

Offline QuantumFlux42

Junior Newbie





« Reply #2 - Posted 2013-08-30 20:55:16 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2013-08-30 21:26:30 »

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. [snip]

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)

This is more of a learning experiment anyways as I try to understand and learn the nuances of Java.

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.

Offline QuantumFlux42

Junior Newbie





« Reply #4 - Posted 2013-08-30 21:36:17 »

Makes sense, thanks for your quick replies Grin now to continue working on my engine.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (35 views)
2014-04-15 18:08:23

BurntPizza (31 views)
2014-04-15 03:46:01

UprightPath (46 views)
2014-04-14 17:39:50

UprightPath (29 views)
2014-04-14 17:35:47

Porlus (46 views)
2014-04-14 15:48:38

tom_mai78101 (67 views)
2014-04-10 04:04:31

BurntPizza (127 views)
2014-04-08 23:06:04

tom_mai78101 (227 views)
2014-04-05 13:34:39

trollwarrior1 (192 views)
2014-04-04 12:06:45

CJLetsGame (199 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!