First Post - Bufferstrategy, jpane and canvas?

Hey, everybody, this is going to be my first post here so here’s a little background.
I’m self taught and only moderately new to java. I have prior experience with C++ up to about the intermediate level but decided to learn something else. I’ve been reading books, posts and sourcecode to help me get the hang of JG basics. I’ve been reading through Mojang’s Catacomb Snatch while following JGO for a little while but I’m beginning to run into some questions I can’t seem to answer.

TLDR: I’m fairly new and I don’t understand some junk.

1: I feel like a have a fair grasp of how a game loop “should” be structured; I definitely like the fixed timestep version… But I don’t don’t really understand the difference between two of the posts I’ve seen here.
Eli’s fixed timestep loop here: http://www.java-gaming.org/topics/game-loops/24220/view.html
and ra4king’s timestep loop here: http://www.java-gaming.org/topics/about-on-my-main-loop/26222/msg/229028/view.html#msg229028

I understand some of the differences. What I’m really hung up on is the purpose of Eli’s interpolation. I don’t fully understand why it is necessary. And to clarify, I’m going with the assumption in this case that Eli’s code is more precise because both of these loops seem to be well respected and Eli’s is longer. :slight_smile:

2: I am not completely clear on some of the ways I’ve seen BufferStrategy implemented. I have followed TheNewBoston’s turorials on Youtube but they seem somewhat lax in the Java Gaming section. http://www.youtube.com/user/thenewboston?feature=watch
I have read Oracle’s documents on BufferStrategy, JFrame, Canvas etc. and I understand how BS works and why it’s great but I am not clear on the differences between JPanel, Canvas and how they act when working with BS. I have seen a BS created from a Window, JFrame, Canvas, and JPanel but I don’t understand why one would be preferred over the other. In Catacomb Snatch Notch uses a JFrame packed with a JPanel yet I see many examples of people creating a BS from a JFrame directly or more often from a Canvas. I am assuming that when you call Canvas.createBufferStrategy(2) it is really calling .createBufferStrategy(2) on the frame it’s packed in but idk. I’m beginning to have dreams related to this question so some clarity would REALLY help.
P.S. I have seen code where .pack() is not used. Why is it preferred or vice versa?

3: While it all seems both concise and clever (imho) I would really love an explanation of why ra4king uses a canvas in the link in number 1. I keep seeing “Oh god DON’T use swing and awt together!” but I’m not seeing a consensus on the issue so I’m at a loss as to why you would use Canvas over JPanel. What I think I understand is that JPanel is best used when using swing components like JButton and that awt has little conflict outside of interactions with those components. Tell me if I’m right: JPanel is more efficient for using components and since most of us on JGO wouldn’t be using them we prefer Canvas because it’s not at the mercy of the EDT. Is that even close?
In addition, I’m assuming that the purpose of ra4king’s daemon thread is because .sleep(int) is more accurate if it’s in constant use.

Lastly, thanks for reading and feel free to help me with forum etiquette cause this is my first real forum post, ever. And I’d love input from either ra4king or Eli here.

TLDR: Read the underlined junk. Also, I’m new, thanks.

I will let someone else get to the gameloop but I can help with the BS (no pun intended)

JPanel and most Swing components are double buffered automatically and I do not think canvas is. They also use passive rendering which is not what we want. Normally, you use a canvas because it is meant to be used to draw stuff with where a JPanel is not. I have used both and with a few extra settings in JPanel, they both work fine. They whole thing is like you said, don’t mux up AWT and Swing. The reason why we use BS is to keep things actively rendering and not passive which is what java2D wants to do. You also need to double buffer when you want animations to be smooth. pack() will automatically shrink all components into the smallest size in a JFrame. So if your frame is 1600x900 and your canvas is 800x600 it will make the JFrame 800x600. I may be wrong on this one.

Now comes what you probably don’t want to hear. Drop learning any rendering in Java2D. It is crap. Use libgdx or slick2D. No need to wonder about all the black magic java2D is doing and the reasons why.

If you insist on java2D then before you wonder all the magical ways of getting java2D to perform right, here is this.

If you would like, I can post my old classes that I used to get java2D to perform well but really you should just drop java2d.

Thanks for the help!

Now it makes sense, pack re sizes to fit the smallest piece.

Really, I don’t mind hearing that. I’ve seen that before on here quite often. The only reason I haven’t been looking into libraries is that I want to begin working on my projects the “right” way. I don’t mind the idea of a library, I just wonder, are they really more efficient? I’m afraid of using them as a crutch simply because its easier. If they’re actually better then I’m on board. :slight_smile:
If that’s the case, is there a pro/con page for the popular ones?

Welcome!

[quote=“BrassApparatus,post:3,topic:41522”]
Using Java2D when there’s LibGDX around is less like not using a crutch and more like breaking your leg intentionally :slight_smile: Java2D is a o.k. place for beginners to start, as it does not introduce some complexities relating to the use of libraries (e.g. some classpath stuff) and allows you to start at a very basic level, but it is generally lacking in features and performance compared to the OpenGL-based alternatives such as LibGDX, Slick2D or LWJGL (which is more of a basic wrapper that underlies both). Especially since you seem to have quite some programming experience just starting using one of these libraries may be a good idea. Or just try some extremely simple games using Java2D to get the hang of it.

With regards to 1: I never used interpolation in this way (too complex for my little brain), but kind of understood how it works after reading this brief explanation.

Thanks Grunnt.

I’m looking into LWJGL and libGDX. I have pretty much decided that for my purposes interpolation is overkill.
At this point, some advice / tutorial suggestions on the above would be really helpful.

Thanks again guys
EDIT: I am thinking I’m going lidGDX. This post seems to have confirmed what I was suspecting -->http://www.java-gaming.org/topics/lwjgl-vs-libgdx/28840/view.html

Also, would someone explain how to post a link as “link” instead of “http://”?

You can post as a link using [ url = http://stuff]title[ / url ] without the spaces ofcourse.

Tutorials and getting started info on LibGDX is available in the wiki. Good luck!

The JFrame will be a bit bigger than 800x600 to allow for an internal area (the Canvas) of 800x600 plus the window’s border.

The OpenGL to java bindings are all more efficient simply because OpenGL is a native, hardware accelerated, library. I really wouldn’t call LWJGL or LibGDX a crutch but, of the two LWJGL is definitely lower level as it’s mostly a direct binding to the native OpenGL libraries. I switched from Java2D to LWJGL simply because I like learning how things work and writing more things for myself as a learning experience instead of using more of a game-dev library like LibGDX. Whichever you choose good luck :slight_smile:

I knew in the back of my head that JFrame would be larger but I’d kind of forgotten it.

I tend towards that “Do it myself” mentality but I tihnk im going to try libGDX then maybe next time LWJGL. Thanks for the advice :slight_smile:

Well…err…since everything else has been covered, I’ll just drop in and say hi! :slight_smile:

Also, here is an earlier post of mine that explains Passive vs. Active rendering.

Actually there is one thing you could help explain. -See end of #3- I am assuming that you make a daemon thread and have it sleep simply because it makes the timing mechanisms more efficient if they’re running constantly. Or am I missing something?

The daemon thread forces the system to use the high precision timer. This will cause sleep(int) to be more accurate :slight_smile:

You can see my game engine. (It’s totally Java2D)

GEJ - Game Engine 4 Java

Thanks all! This has really cleared up a few things for me. :smiley: And its good to see that someone out there is doing Java2D seriously too.
tyty

A little semi-necro since I’m behind: Just to clarify, you only need this daemon thread trick on Windows. The precision of the scheduler timers on Linux and OSX are always pretty good and can be made better with system calls (involving JNI stuff you probably don’t want to mess with)

Thats what I was thinking but its really nice to have all the clarification. Ty :slight_smile: