Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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  
  Should I be using java.awt for games?  (Read 5792 times)
0 Members and 1 Guest are viewing this topic.
Offline dah01

Junior Member


Medals: 7



« Posted 2011-03-12 00:04:23 »

If not, what should I use instead? I have an example from brakeen's developing games in java and it imports a lot of java.awt packages. I'm told that awt is filthy and slow?
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2011-03-12 00:15:41 »

For beginners, it is recommended to start with Java2D (java.awt, java.awt.geom, java.awt.image)
From where did you hear that Java2D is filthy? It might be a bit slow on some CPUs and graphics cards (especially Intel since there is no hardware acceleration), but it runs perfectly fine.

As you advance, you should start learning OpenGL by using LWJGL, JOGL, Slick2D, etc...

Offline dah01

Junior Member


Medals: 7



« Reply #2 - Posted 2011-03-12 00:47:35 »

I just wasn't sure if there were swing alternatives.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #3 - Posted 2011-03-12 01:39:51 »

If you're a newbie at game programming, the odds of Java2D being your bottleneck are infinitesimal.

In general, Java2D is perfectly fine for most 2D games you might want to create (platformers, shooters, etc.).  If you want to create a 3D game, it simply isn't the tool for you (hence the name); there are better tools for that, such as LWJGL and JOGL.

Slick2D is a popular alternative to Java2D with a similar API.  It uses LWJGL under the covers and may provide better performance overall than Java2D.  The tradeoff is that you have to deliver native code with your game (not a problem for desktop games, but annoying for applets, and complicates things if you're a newbie).
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #4 - Posted 2011-03-12 02:05:57 »

Slick2D is a popular alternative to Java2D with a similar API.  It uses LWJGL under the covers and may provide better performance overall than Java2D.  The tradeoff is that you have to deliver native code with your game (not a problem for desktop games, but annoying for applets, and complicates things if you're a newbie).
Which is why I mentioned Java2D for beginners, then once they're comfortable with their skills, they may venture into OpenGL with Slick2D and finally into 3D with LWJGL, etc...

I just wasn't sure if there were swing alternatives.
Swing is only for GUI, like buttons, text boxes, drop down lists, etc...
AWT GUI tools use the underlying OS to draw the components, so in Windows they would be gray and blocky, while with Swing, you can customize as much as possible.

Offline dah01

Junior Member


Medals: 7



« Reply #5 - Posted 2011-03-12 04:55:36 »

Thanks for answering my questions guys.  That's mainly what I wanted to know was if I was going to run into issues kixing canvas with swing or if there was a swing class i could use instead. all of the key listeners are awt as well, are they not?
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #6 - Posted 2011-03-12 05:14:58 »

Never mix Swing GUI Components with AWT GUI components, that could lead to bad things.
If you want to use Applet or Frame, use a Canvas to draw.
If you want to use JApplet or JFrame, use JComponent to draw.

And yes, look into java.awt.event for all listeners. You can also look into javax.swing.event which has some useful listeners like MouseInputListener, which combines MouseListener and MouseMotionListener.

Glad to help Grin

Offline dah01

Junior Member


Medals: 7



« Reply #7 - Posted 2011-03-12 06:36:57 »

When you say draw do you mean for animation? Like, if I want a decent animation thread, I should be using System.getNanos(cant remember the name and im on my phone) and should be drawing on just a regular jframe with a content pane of jcomponent? since that is the root of all swing components does that mean i could draw on a panel? What graphics objects should i use? Is it necassary to implement page flipping nowadays or can is there a prebaked solution?
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #8 - Posted 2011-03-12 21:10:46 »

If you want to use Swing, you should create a class that extends JComponent and draw in its paint(g) method.

And there is only 1 Graphics object: Graphics2D. Graphics is used for backwards compatibility but essential all Graphics objects you get are of Graphics2D and you need to cast to use it: Graphics2D g2 = (Graphics2D)g;

And yes, there is prebaked solution for double buffering: BufferStrategy Cheesy
All you do is createBufferStrategy(int) on your JComponent or JFrame and then getBufferStrategy() to get the BufferStrategy object.
You manually call paint(g) with the Graphics object you get when call getDrawGraphics() and then show() on to show the image on the screen. Both those methods are in BufferStrategy.

PS: It's System.nanoTime() but careful, it is ONLY used for measuring difference between two points in time. It is precise, but not accurate Wink

Hope that helped Grin

Offline dah01

Junior Member


Medals: 7



« Reply #9 - Posted 2011-03-13 04:54:27 »

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

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #10 - Posted 2011-03-13 04:56:40 »

Glad to help Smiley

Offline philfrei
« Reply #11 - Posted 2011-03-13 11:05:34 »

OK, I thought drawing on JComponent was automatically double buffered. I know drawing on JPanel is. No need to bother with BufferStrategy there. Just make the first draw command a clear and draw your new positions after that. Very simple for beginners, and works fine for puzzle games and simple animations.

I have Hexara http://www.hexara.com/gameNS.html set at repaints on a JPanel every 33 msec. It's pretty simple and works fine. When you click and drag the tiles around, handling is smooth. No stressing nanowhatsis values either. Just send in repaints via a Timer (after triggering the updates with the same Timer). It works. It's pretty easy.  Smiley

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #12 - Posted 2011-03-13 15:33:23 »

OK, I thought drawing on JComponent was automatically double buffered.

That's correct.  All JComponents are double-buffered, AWT components are not.  That's one of the benefits of using Swing.  To get double buffering with an AWT component, you have to use the Canvas/BufferStrategy approach.

Two reasons people still recommend using Canvas/BufferStrategy for games is that you usually don't use pre-baked components in a game, but rather roll your own, and using AWT saves the overhead of loading Swing stuff (which is unnoticeable to a user, but you do load a LOT of stuff as soon as you create the first JComponent - LookAndFeel-related classes).
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #13 - Posted 2011-03-13 19:47:06 »

Quick fix on my post, createBufferStrategy is ONLY available for java.awt.Canvas and java.awt.Window (and its subclasses: JWindow, Frame, JFrame).

As BoBear said, all JComponent's are already double buffered but JFrame is NOT, so if you want to use JFrame or JApplet, you need to draw on a JComponent so you wouldn't have to worry about double buffering.

Offline dah01

Junior Member


Medals: 7



« Reply #14 - Posted 2011-03-14 00:07:33 »

I think it's common practice to be only doing things to panels or components in the contentPane of a frame.
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #15 - Posted 2011-03-14 00:10:08 »

No the best practice is to draw on a Canvas or a JComponent that you add to the content pane of a JFrame or JApplet.

Offline dah01

Junior Member


Medals: 7



« Reply #16 - Posted 2011-03-14 00:21:45 »

That's what I mean, maybe I didn't word it well enough.  Cheesy
Offline dah01

Junior Member


Medals: 7



« Reply #17 - Posted 2011-03-14 00:24:53 »

So, which should I use, JComponent + JFrame or Canvas with Frame? Which is the better way to work/learn/better performance/is what all the cool kids do/ and why?
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #18 - Posted 2011-03-14 00:27:07 »

You can add both to JFrame.

JComponent - automatic double buffering, easier, good for beginners
Canvas - no double buffering, tricky to set up set up with double buffering, recommended to progress into.

Offline Nate

JGO Kernel


Medals: 128
Projects: 3
Exp: 14 years


Esoteric Software


« Reply #19 - Posted 2011-03-14 10:12:20 »

TL;DR

No, Swing/AWT is a powerful GUI toolkit with a bit of a clunky API. It is even worse trying to make games with it. Use libgdx! Smiley

Offline dishmoth
« Reply #20 - Posted 2011-03-14 11:43:08 »

JComponent - automatic double buffering, easier, good for beginners
Canvas - no double buffering, tricky to set up set up with double buffering, recommended to progress into.

Admittedly I'm pretty ignorant of the inner workings of Swing and AWT, but I'm going to take a bit of convincing that the "JComponent is just like Canvas+BufferStrategy but easier to use" argument is sound.

How are you triggering the buffer-flip for the JComponent?  Through repaint()?

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #21 - Posted 2011-03-14 12:28:34 »

How are you triggering the buffer-flip for the JComponent?  Through repaint()?
Yeah I know, not quite active rendering. Which is why it's good for beginners. Grin

Offline dishmoth
« Reply #22 - Posted 2011-03-14 21:02:57 »

How are you triggering the buffer-flip for the JComponent?  Through repaint()?
Yeah I know, not quite active rendering. Which is why it's good for beginners. Grin
Good for beginners who like to debug synchronization glitches between the game thread and the paint (EDT) thread. Roll Eyes

Offline Gudradain
« Reply #23 - Posted 2011-03-14 21:58:58 »

Check out the tutorial section.

This is a template to get you started right away.

It use a frame + a canvas. (well in the example it's JFrame I know that people above told you it's not recommended but it doesn't make any difference really).
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #24 - Posted 2011-03-14 22:29:36 »

If you're a newbie at game programming, the odds of Java2D being your bottleneck are infinitesimal.
I actually disagree. If you don't know how to use Java2D correctly so it isn't hardware accelerated, it is incredibly slow.

See my work:
OTC Software
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #25 - Posted 2011-03-14 23:04:24 »

How are you triggering the buffer-flip for the JComponent?  Through repaint()?
Yeah I know, not quite active rendering. Which is why it's good for beginners. Grin
Good for beginners who like to debug synchronization glitches between the game thread and the paint (EDT) thread. Roll Eyes


For what it's worth, there shouldn't be any synchronization issues if you use a javax.swing.Timer to handle your updates, as it's called on the EDT.
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #26 - Posted 2011-03-14 23:40:40 »

If you're a newbie at game programming, the odds of Java2D being your bottleneck are infinitesimal.
I actually disagree. If you don't know how to use Java2D correctly so it isn't hardware accelerated, it is incredibly slow.

What do you mean, "use Java2D correctly?"  Do you mean things like "use Canvas/BufferStrategy for page flipping" and "create compatible images, stuff like ImageIO creates non-accelerated images"?  If so then I guess you're right - that's not stuff a beginner would know, without having sources such as this forum.  Smiley  And it sucks that it's an API where there's many ways to do the same thing, some just yield slower results than others.

I still feel like, once you know a couple of basic pointers like those, most common sprite-based games (i.e., stuff a beginner should probably start with) can generally run on any hardware made in the past several years.
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #27 - Posted 2011-03-14 23:48:02 »

I do not recommend using Timer at all. It has many problems that can affect the performance of your game. I have done extensive testing and found that making your own loop is more efficient.

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 (27 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (193 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!