Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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  
  GUI with Java2d - Need help  (Read 2273 times)
0 Members and 1 Guest are viewing this topic.
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Posted 2012-03-10 20:57:14 »

Hello everyone! I hope this is the right place for this thread.

I need help to make a GUI for my game Alien Eyes Invasion!.

Right now I have a "GameGUI Entity" that it's very much like any other entity in the game (the ship, the shots, the aliens). That entity has a sprite of 800x600 pixels with borders and a bottom bar for displaying the game's info. Its center is transparent and the other entities check borders within it.
On the info bar, there is a text "Ship's hp" and an "empty life bar" drawn on it.
Upon it, I draw 10 entities which holds "first", "mid" and "last" health bar sprites, like this:

Picture of health bars:
First: Mid: Last:

Picture of how the GUI it is right now:


Picture of the game running (showing the health bar with some hits, allowing to see the empty effect):


The health bar is held in an ArrayList and depending on the Player's hp, they are allowed to be drawn or not (boolean field on the HealthBarEntity object).

This approach lets me draw a decent game GUI and show the player's life. But I hate it. I need help to make:
- Game Menu window (New game, Option...)
- In Game GUI to show Player Health, Current Level, Weapon Selected and the like.

Please point me to somewhere, I tried the search option in the forum before posting this but came to nowhere Smiley

Offline zngga
« Reply #1 - Posted 2012-03-10 22:13:32 »

I am assuming your using java2D for this, if you are that's great because you have access to some fairly simple techniques!

I always like to separate out the game render and gui render code.

so in your game loop have something like this:
(pseudo code)

while game running
>update game
>render game
>render gui
...
>sleep

in the update method you will calculate new positions for enemies, weapons fire, and the player. As well as increase / decrease health for various entities.

in the game render method, you draw the game (obviously) what I would do is draw to a bufferedImage (that is the same size as your window) and then paint that to the window's graphics content

finally in the gui render method, draw the gui to another bufferedImage, but this time make sure this image has alpha support
1  
BufferedImage image = new BufferedImage(witdh, height, BufferedImage.TYPE_INT_ARGB);

then simply draw it on top of the game render.

It is a really quick solution, but I hope it gets you thinking in a good direction.

My code never has bugs... it just develops unexpected features!
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #2 - Posted 2012-03-10 22:49:07 »

zngaa, just, thanks.  Kiss

I've been thinking here at work about making the separation you mention and you just made it very clear and simple, thanks!

But about the GUI itself, I need a bit more of help:

- First, is it ok  to do it as I did? One picture loaded into the sprite field of an entity? What if I need to show some numbers, should all be entities and sprites?

- Why do I need to make sure it has alpha support?

- What about the game menu? Should I do it with JPanel?? How do I use custom background and buttons then? Any insight would be very much apreciated.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zngga
« Reply #3 - Posted 2012-03-10 22:59:13 »

zngaa, just, thanks.

I've been thinking here at work about making the separation you mention and you just made it very clear and simple, thanks!

But about the GUI itself, I need a bit more of help:

- First, is it ok  to do it as I did? One picture loaded into the sprite field of an entity? What if I need to show some numbers, should all be entities and sprites?
This should be fine for your entities, if you need to show numbers just include that in their render methods
Quote
- Why do I need to make sure it has alpha support?
Using the way I suggested you would be drawing the game render first, then drawing an image representing the gui on top of the game render, if there isn't alpha support on that image it will simply cover up the game render. Which is no good, you want people to see the game render, haha!
Quote
- What about the game menu? Should I do it with JPanel?? How do I use custom background and buttons then? Any insight would be very much apreciated.
What I do is create game states, one for the menu another for the game. Something like this:
(pseudo code)
game state class
>init method
>>put all your initialization code here, does the state need access to images (sprites) or other system files, does it need to set up any specific output settings? Put all that here

>render method
>>put all the drawing stuff here

>enter method
>>call this method every time your changing from ANOTHER state to this state, if any settings need to be reset, this is where that happens

>leave method
>>call this method any time your changing from THIS state to another state



game loop
>update current state
>render current state
...
>sleep

I modify this post later to add in some code, but I am really busy now... about an hour.

My code never has bugs... it just develops unexpected features!
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #4 - Posted 2012-03-10 23:19:50 »

Hey! Don't you worry, this is a hobby, no need for hurry. You already done A LOT! If you want, come back tomorrow (it's 19:00 here) Smiley

I should learn about game states, because I really don't fully understand how states work, not the methods, they are ok, but the general concept and implementation it's what bugs me. Do states allow me to implement pause, escape to menu, and other stuffs like accessing a map?


Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #5 - Posted 2012-03-11 03:34:29 »

Game states allow you easily change the flow and the state of the game. This code shows how to implement Screens/States. With that, you can then call the current Screen's update and render methods from the main class.

Also, don't use 2 BufferedImages for game and GUI, especially since blending is slooowww using Java2D. Draw everything onto a BufferStrategy using Canvas, on 1 Graphics2D instance. If you don't know how to use BufferStrategy, this code should be helpful.

Good luck! Smiley

Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #6 - Posted 2012-03-11 12:49:22 »

I already told you I like you r4king, how many times do I need to repeat it Smiley
Thank you very much Grin

Offline evilfrenchguy

Junior Member


Medals: 3



« Reply #7 - Posted 2012-03-11 16:36:41 »

Great post ra4king.

Yeah, it's best to do all your rendering/painting with 1 Graphics2D object. If you use 2 BufferedImage objects, every render you need to get the graphics context from both images, which could cause unneeded slowdown. Not to mention that one is alpha-enabled.

I've yet to try BufferStrategy out, but it sounds good.
Offline zngga
« Reply #8 - Posted 2012-05-23 14:24:24 »

After doing some bench-marking I agree that 2 bufferedImages is slower than passing around a graphics2d object. Though having 2 images certainly made the rendering logic less complicated. But the basic premise remains the same. Game States with update and render methods.

My code never has bugs... it just develops unexpected features!
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 (81 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

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