Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
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  
  Best way to make a 2d application  (Read 3474 times)
0 Members and 1 Guest are viewing this topic.
Offline JohnnyRascal

Senior Newbie




aspiring rascal


« Posted 2004-03-08 01:44:42 »

Howdy! I'm a fairly experienced C/C++ programmer, and I've made some simple game demos using openGL, but I'm currently writing a game for a college project using Java. I've done a lot of looking around for information on all the 50 billion different ways of making windows and drawing little bitmaps on them, and I've decided the following:

1. AWT sucks, so I want to use only Swing instead.
2. Swing automatically uses hardware accelerated backbuffers using volatileImage.
3. I don't want to use any external .dll files or classes.
4. I don't see any point using openGL for 2d if Java2D and Swing are just as fast.
5. I want to use an application, not an applet.
6. JFrame is the best container to use.
7. compatibleImage is the best way to make hardware accelerated sprites.

So, what'd be a good framework? Would my main class extend JFrame, or could I include the JFrame as a private variable inside my main class? Where would I implement the event and keyboard listeners? And how do I load and draw compatibleImages?

In short, could somebody point me to either a good tutorial, or show me a good code snippet for a basic framework?
Offline Jeff

JGO Coder




Got any cats?


« Reply #1 - Posted 2004-03-08 03:12:24 »

Quote
2. Swing automatically uses hardware accelerated backbuffers using volatileImage.


Actually SWING uses AWT for its rendering so someone has lead you astray.
By definition SWING can't do anything that AWT doesnt do (at least currently.)

Quote

3. I don't want to use any external .dll files or classes.
4. I don't see any point using openGL for 2d if Java2D and Swing are just as fast.
 

Okay so these are your givens.

Quote

5. I want to use an application, not an applet.


This is a good thing as it avoids many problems.

Quote

6. JFrame is the best container to use.


Best for what?  (See below)

Quote

So, what'd be a good framework? Would my main class extend JFrame, or could I include the JFrame as a private variable inside my main class?


Okay. so now its time to straighten you out a bit.  As mentioned above, Swing is a just a layer over AWT.  if you arent doing window/component GUIs there is really little point to it.

I am assuming you want to make a 2D game or other graphics intensive 2D application.

What you want to look at is active rendering with AWT.  This is covered in the (somewhat misnamed) FullScreen tutorial here:

http://java.sun.com/docs/books/tutorial/extra/fullscreen/

These techniques actually work in full screen or windowed mode.  For an example of a game skelleton that runs in either mode you can look at the Scroller demo.  (I believe its on the Wiki, if you can't find it there search for "Scroller" here as the URL has been posted multiple times.)

A fair warning thing:  This technique works great under 1.4 under Win32.  I believe Dmitri said it works about as well as it can under X given X's limitations.  Last time I tried it on the Mac it sucked performance-wise but Apple considers that a bug and is aware of it.  I don't know if its been fixed yet or not.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Online kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #2 - Posted 2004-03-08 05:40:49 »

Quote

7. compatibleImage is the best way to make hardware accelerated sprites.


This is not always true. On some platforms (windows) if an image goes over a given size even thought it will fit in the graphics card memory its not put there. For simple sprites compatibleImage fine but for big ones you might want to consider creating your own VolatileImage and managing it yourself.

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2004-03-08 07:38:43 »

Helpful tip: Stick to 256x256, and pack your sprites into these images.

Cas Smiley

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #4 - Posted 2004-03-08 08:02:35 »

Quote


Actually SWING uses AWT for its rendering so someone has lead you astray.
By definition SWING can't do anything that AWT doesnt do (at least currently.)

...
Okay. so now its time to straighten you out a bit.  As mentioned above, Swing is a just a layer over AWT.  if you arent doing window/component GUIs there is really little point to it.


Just to be clear...basically, AWT is two things: the underlying native code to do windows images etc, plus the toolkit/widget-set/whatever-you-call-it Smiley on top. Swing is an upgrade to the second part, but does none of the first part.

Hence, as Jeff said, "Swing uses AWT for it's rendering" but if you are going to use anything in AWT that you can do in Swing it's nearly always better to use Swing (this is a gross generalization, and java.sun.com has a tutorial specifically looking at this question in more detail, IIRC). The main reason for using Swing is that it has many of the same classes as AWT *BUT WITH FEWER BUGS* (some major bugs in AWT just didn't get fixed for many years because Swing had already come along). Swing also re-designed parts of AWT to be easier to use, better designed, more responsive, more feature-rich. In particular, the code you have to write to get your Panel/Canvas/whatever to "paint()" anything non-trivial is significantly less painful in Swing than in AWT.

IMHO, if your game is going to have in-game menus which are used once the game has started (e.g. AlienFlux's menus are only accessible when the game is over; freerails menus are available all through the game) then it's worth using Swing *if* your alternative is to use AWT, and you aren't going to OpenGL everything. Ditto if you expect to have multiple rendering panels in a non-trivial arrangement (e.g. a complex inventory for an RPG, perhaps with nested containers). Just my 2 cents.

malloc will be first against the wall when the revolution comes...
Offline princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2004-03-08 08:10:26 »

Quote
AlienFlux's menus are only accessible when the game is over

Actually they can be used whenever or wherever... I use a very complete windowing toolkit in AF (vastly overengineered really). I just didn't put the menus in the middle of the game.

Cas Smiley

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #6 - Posted 2004-03-08 08:32:20 »

Quote

Actually they can be used whenever or wherever... I use a very complete windowing toolkit in AF (vastly overengineered really). I just didn't put the menus in the middle of the game.

Cas Smiley


Sorry, I meant in a gameplay sense. I.e. it's not a game where you'll expect to use those menus in the middle of a game Smiley.

malloc will be first against the wall when the revolution comes...
Offline JohnnyRascal

Senior Newbie




aspiring rascal


« Reply #7 - Posted 2004-03-08 17:08:42 »

Thanks for all the replies. To clarify things a bit, I don't want to use AWT because everyone seems to be saying Swing is faster and prettier because it extends AWT's functionality. For rendering in my game all I need to do is make a window, (in windowed mode) and draw small color-keyed sprites to it. (as quickly as possible too) If I could have some very simple menus on top of all that it would be bonus points, but I could just make menus with bitmaps too.

I've been doing some research on Swing, and it looks like JFrame is the actual main window of a Swing application, and it needs to have a JPane attached to it that you actually do the drawing on. Would it be a good idea to have my main class extend JFrame and implement runnable? Or should I have a seperate class that extends JFrame and includes a JPane as a private variable, and have my main class extend runnable? Or do I need runnable at all? And does Swing do double buffering automatically?

If somebody could hook me up with a good tutorial or show me a piece of code that makes a simple windowed rendering loop with Swing I would greatly appreciate it. And thanks for helping me out and all.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #8 - Posted 2004-03-08 17:19:48 »

Swing will do double buffering automagically, but its not very practical for game use. Instead you'll be better off using a BufferStratergy and sticking that in a JFrame. Use of a BufferStratergy also allows you to hide any differences between windowed and fullscreen modes.

Actually, now I think about it you need some sort of component to create the buffer stratergy on. I seem to remember using a Canvas for windowed mode (and ironically a Window for fullscreen mode).

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline JohnnyRascal

Senior Newbie




aspiring rascal


« Reply #9 - Posted 2004-03-08 17:30:20 »

What exactly is a bufferStrategy? Does it use volatileImage for back buffering? I remember reading on Sun's main site that Swing uses volatileImage for back buffering, so that's why I assumed I should use it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #10 - Posted 2004-03-08 18:43:45 »

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/BufferStrategy.html

I'm pretty sure it does use VolatileImages behind the scenes for the surfaces. The main advantage is that you manually control the flipping - draw your scene then manually call show() to display it. And in fullscreen mode it lets you use v-sync and the show() can use a proper pointer swap instead of the blit for better speed and consistancy.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline JohnnyRascal

Senior Newbie




aspiring rascal


« Reply #11 - Posted 2004-03-08 20:59:34 »

Is it possible to use VSync in windowed mode?

Also, I still really need to know if I my main class should extend the JFrame, or if it should include the JFrame as a private variable. I basically just really need to find a good tutorial for making the basic framework.
Offline Jeff

JGO Coder




Got any cats?


« Reply #12 - Posted 2004-03-09 05:36:26 »

It will vsync IF the underlying system supports it.

Win32 does not support Vsync for BLT to windows.  Period.

The two resources I pointed you at (Mikes article and my Scroller example) should have all the examples you need.  Mikes article also explains what a BufferStrategy is.  Go and read then come back here with specific questions.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #13 - Posted 2004-03-09 08:02:52 »

Quote
Win32 does not support Vsync for BLT to windows.  Period.
Ahh - now the funny thing is, I told this to my dad the other day, and he went ahead and showed me I was wrong with 1 line of code. I don't know if it universally works but I'm going to look into it.

Cas Smiley

Offline Kommi

Junior Member




All opinions will be lined up and shot!


« Reply #14 - Posted 2004-03-09 19:57:24 »

One of the members here is an author writting a 2d Java game programming book using 1.4.2. He posted the first chapter so you might want to look at it for some good examples. Note he also does not use anything that is not pure Java, so some of his solutions are creative, but some are not very the best way of programming as of 1.5 (like the thread.sleep work around which is no longer needed now that there is nanoTime(), also using a sleep loop instead of gametick / cienttick approach) Check the link here:http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=2D;action=display;num=1075798524

Kommi
Offline Jeff

JGO Coder




Got any cats?


« Reply #15 - Posted 2004-03-09 21:08:40 »

Quote

Ahh - now the funny thing is, I told this to my dad the other day, and he went ahead and showed me I was wrong with 1 line of code. I don't know if it universally works but I'm going to look into it.

Cas Smiley


Well properly DirectDraw does not support it.

With OGL for instance, I dunno.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #16 - Posted 2004-03-09 21:29:08 »

Quote

Ahh - now the funny thing is, I told this to my dad the other day, and he went ahead and showed me I was wrong with 1 line of code.


Too bad it is a whole line of code , or you could have posted it here  Roll Eyes

Offline princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #17 - Posted 2004-03-10 08:32:33 »

Actually it is a directdraw call, I just can't remember what it is... something like
1  
DD.WaitForVerticalBlank DDWAITVB_BLOCKBEGIN, 0

Used to be in the LWJGL but we took it out when I got rid of the DD init code. Might put it back in so we get vsync in windowed mode.

Cas Smiley

Offline Jeff

JGO Coder




Got any cats?


« Reply #18 - Posted 2004-03-11 01:46:08 »

HM.

Just because a call exists in the Win32 doesnt mean it actually works and certainly doesn't mean it works under all circumstances.  I know of plenty of API calls that don't.

I'd be curious to hear Chet or Dmitri's comments on this since I got  my info from the 2D group...

JK

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #19 - Posted 2004-03-11 08:00:54 »

It's worth a try I think, it's a neat little feature.

Cas Smiley

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #20 - Posted 2004-03-11 09:01:37 »

Quote
Just because a call exists in the Win32 doesnt mean it actually works and certainly doesn't mean it works under all circumstances.  I know of plenty of API calls that don't.


[innocent]Like fullscreen mode in java?[/innocent] (only works on windows) Wink

malloc will be first against the wall when the revolution comes...
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.

pw (16 views)
2014-07-24 01:59:36

Riven (16 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (16 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!