Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
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  
  How do you guys work with graphics?  (Read 1605 times)
0 Members and 1 Guest are viewing this topic.
Offline Seecon

Junior Newbie





« Posted 2009-08-23 04:17:34 »

I've been looking in to writing a game ... I know a bit of java .. hopefully enough for a basic game and I'm wondering how you all go about using images or sprites for your game? do you use the Graphics class.. or Graphics2D, or what..? Thanks.
Offline giacobua

Senior Newbie





« Reply #1 - Posted 2009-08-23 07:32:39 »

I use jogl
Offline Alan_W

JGO Knight


Medals: 8
Projects: 3


Java tames rock!


« Reply #2 - Posted 2009-08-23 08:30:23 »

It depends on the type of game.

For a 2D game that uses sprites and tiles, you can use the Graphics class in most cases.  You will need to draw on one buffer, whilst displaying a second buffer and then swap them over every time you complete drawing a frame.  The BufferStrategy class provides this functionality.  You can't use BufferStrategy with all AWT or Swing classes.  In particular it doesn't work with Panels.  The usual option is to place a Canvas in a Frame and apply a BufferStrategy to the Canvas.  For the sprites and Tiles use the Image class or the BufferedImage class.

The above system works fast because Java caches your sprites in off-screen video memory and uses the graphics card to blit them to the right place when you use drawImage.  Things slow done if you draw on the image and then draw it to the screen.  For instance if you use a BufferedImage and then draw on it with (say) fillRect, this happens on the main processor.  When you draw the image to the screen, it has to be copied to the video memory, which is a slow operation, so you lose performance.  This also happens if you use some of the advanced compositing functions in Graphics2D.  You would think these would be done in the graphics hardware, but that is not the case (at least by default), so this results in slow operations.  You can force Java to accelerate more graphics operations using the 'opengl' systems property, but this only works on later versions of Java and the amount of acceleration may vary with the platform.

If you need compositing or are writing a 3D game, then using the standard Java2D functions won't be fast enough.  At this point people usually use JOGL or the LWGL which provide an interface to OpenGL.  Either of these libraries work fine.  However these are not built into the default java installation so your game will have to get java to download them as part of your game installation.  This is a big overhead for a small game, but is becoming less of an issue as broadband speeds increase.  A drawback of using external libraries is that they need to be digitally signed.  This results in annoying dialog boxes popping up when using Webstart (or Applets) which can upset users.

The last option is to create a BufferedImage the same size as the screen and then use a succession of function calls to get the underlying byte array containing the display data.  You can then write to this in the same way you would write to a chunk of display memory.  The drawback is you have to write your own drawing primitives.  The advantage is the java Just-In-Time compiler compilers this all to native code at run-time, so it runs fast.  After creating a copy of your window this way, you then use drawImage to write it to the display.  While you can use a BufferStrategy here, in practice this works well enough without one.  The main application of this method is when writing games for the annual Java4k competition.  Here you are artificially constrained on application size and therefore cannot use libraries such as JOGL.

Lastly there are Scene engine libraries such as Java3D.  These are really intended for building up a 3D scene in non real-time.  There are also other game engines that build on top of JOGL or LWGL, but I haven't tried those, so can't comment on them.

Time flies like a bird. Fruit flies like a banana.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nate

JGO Kernel


Medals: 153
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #3 - Posted 2009-08-23 11:10:36 »

Shouldn't there be a FAQ thread with links to various libraries?

For 2D look at Slick, or Java 2D. For 3D look at jMonkeyEngine, Ardor3D, LWJGL, or JOGL.

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #4 - Posted 2009-08-23 11:49:32 »

If you're new to writing games and only know a bit of java then Graphics2D is an excelent starting point. It'll let you get graphics on screen quickly and let you worry about the actual game rather than the irrelevant technical details. Plus you can do some pretty amazing things with Graphics2D once you get your hand in. Smiley

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

Junior Newbie





« Reply #5 - Posted 2009-08-23 18:18:45 »

Thanks everyone for the advice. Yes hah I'm going to do a 2d. I'm pretty sure 3d is much too advance, and to add to that, I'm not a very good modeler  Grin.

Orangy, is there some sort of tutorial that kind of shows usage of the class..? I am in fact new, but I have a lot of common sense and could work well with a little guidance.

Much appreciated!

- Seecon
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2009-08-23 19:12:50 »

You might find this useful: http://www.cokeandcode.com/node/6

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

JGO Coder


Medals: 10



« Reply #7 - Posted 2009-08-23 20:21:29 »

These days you quite rarely use Graphics, as most objects that contain the getGraphics method also contain a createGraphics method for creating a Graphics2D object. Graphics2D can do more then Graphics.

Today, I use JOGL with my own graphics layer on top for creating 2D. For me this works very well. For you I would recommend using Java2D because it's a lot easier to setup, you will encounter less bugs as you develop and it is pretty fast and powerful. Plus over the next few years it will get faster and faster as more of it's underlying code takes advantage of more and more hardware acceleration (I've had JVM updates where changes to Java2D have literally doubled the speed of my game).

This guide on GameDev is a good place to start is it explains how to create a JFrame that is all setup and ready for rendering to.

Offline pjt33
« Reply #8 - Posted 2009-08-24 09:19:31 »

The last option is to create a BufferedImage the same size as the screen and then use a succession of function calls to get the underlying byte array containing the display data.  You can then write to this in the same way you would write to a chunk of display memory.  The drawback is you have to write your own drawing primitives.
I have two comments on this. Firstly, I prefer to use a BufferedImage backed by an int[] rather than a byte[]. Secondly, BufferedImage allows you to create a Graphics object, so you can use this approach to mix standard Graphics calls with custom processing. My Java4k 2009 game did this - I drew almost everything directly to the int[], but used the Graphics object for text, before finally blitting the BufferedImage to the applet's Graphics object.
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.

TehJavaDev (33 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (58 views)
2014-10-16 15:22:06

Norakomi (47 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!