Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Swing GUI in Games?  (Read 1696 times)
0 Members and 1 Guest are viewing this topic.
Offline Dreamcatchermatt

Junior Member





« Posted 2010-04-01 05:12:05 »

Hi all,

I have been playing with the basic game loop class from the tutorials section.

As part of my game's GUI i want to be able to use forms to let the players controll the game.

For example, there will probably be a Shop or market screen that will let players buy and sell goods. I know how to make this as a jFrame form, but i'm unsure how to intergrate this with the actual game rendering prosess.

I have managed to get a jPanel form with lables and text boxes to render onto the game screen, but i cant get mouse events to pass to it (clicking buttons, dragging slideres etc)

Has anyone out there tryed this or got it to work? Is it even possible?

Thanks,
Matt

EDIT: Ok, I have a jPanel which shows the current date and time as a jLabel. It has a blue background, a border, and the opacity is set to true.
When I tell the component to paint during my render loop, it will ONLY show up if I use paintComponents(myGraphics);
This only paints the lable, not the background and border. If I use any of the other paint methods, nothing paints at all.

Whats going on here? :/

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
// in my Game class constructor
public Game()
{
   frame = new jFrame("My Game");
   JPanel panel = (JPanel) frame.getContentPane();
   panel.setLayout(null);
   
   gui_topMenu = new GameGUI.TopMenu();
   panel.add(gui_topMenu);
   ...
}


and...

1  
2  
3  
4  
5  
6  
7  
8  
9  
// in my TopMenu class (extends jPanel)

public void render(Graphics2D g)
{
   // I have tryed each of these (seperately)
  this.paint(g); // this does nothing
  this.paintComponent(g) // this also does nothing
  this.paintComponents(g) // this only renders the controlls on the form, in this case the jLable
}


I must admit that I am still new to using the jFrame and jPanel classes :/
Offline pjt33
« Reply #1 - Posted 2010-04-05 09:06:38 »

Your render() method won't be called by Swing, so I presume you're calling it. What Graphics object are you passing it?

FWIW if you want to force a Swing component to repaint you should really just call repaint() on it and let Swing initiate a paint event. See http://java.sun.com/products/jfc/tsc/articles/painting/index.html
Offline Gudradain
« Reply #2 - Posted 2010-04-05 13:24:59 »

Hello

I have been playing with the basic game loop class from the tutorials section.

I guess you are talking about my tutorial. There is a sad point with using active rendering this way. You can't use Swing component on Canvas. That mean that you can't add button, label, etc directly on the game screen. You can always create your Gui around the Canvas on the JPanel.

If you were already doing this you should just let the passive rendering do the job, it work well.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dreamcatchermatt

Junior Member





« Reply #3 - Posted 2010-04-09 01:04:09 »

Thanks for your advice guys Smiley

Yes Gudradain, it was your tutorial. If I want to implement a complex GUI around active rendering, what are my options?

The GUI i have in mind would need buttons, 'toggle' buttons, radio and check boxes, listboxes & dropdowns and probably tables.
Offline pjt33
« Reply #4 - Posted 2010-04-10 18:31:50 »

Thanks for your advice guys Smiley

Yes Gudradain, it was your tutorial. If I want to implement a complex GUI around active rendering, what are my options?

The GUI i have in mind would need buttons, 'toggle' buttons, radio and check boxes, listboxes & dropdowns and probably tables.
There is an alternative (but a bit hacky) way of doing active rendering of a Swing component which would render the children: override paintComponent to do your rendering and create a method
1  
2  
3  
4  
5  
6  
public void render()
{
    Graphics g = getGraphics();
    if (g != null) paint(g);
    g.dispose();
}


Or you could switch to using OpenGL and a widget toolkit which works on it. I've used JOGL with FengGUI for this quite successfully; FengGUI also has a binding for LWJGL, and there are other OpenGL-on-Java widget toolkits.
Offline Dreamcatchermatt

Junior Member





« Reply #5 - Posted 2010-04-12 04:56:05 »

Ahh, thats interesting. I have been passing the Graphics (actualy, Graphics2D) into my render method from my game-loop render method.

for example;
1  
2  
3  
4  
5  
6  
7  
class myWidgit extends jPanel
{
   public void render(Graphics2D g)
   {
       paint(g);
   }
}
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
// main game loop render method

private void render()
{
    Graphics2D g = this.getGraphics();

    // render all my sprites, etc here ordered low z-index to high z-index

    // render my GUI:
   for(myWidgit x : myGuiArray)
    {
        if(x != null)
            x.render(g);
    }

    g.dispose();
}


Is this way of doing things wrong, or is there a better way of doing it, such as your example of using getGraphics() inside each objects render method?

Also, I have been using the netbeans drag and drop gui builder rather than hand-coding my gui elements. There's not anything in the auto generated code that would obviously cause problems with active rendering is there?

You'll have to forgive me, but I'm not sure what all those acronims mean...
I'll get on wikipedia Smiley
Offline pjt33
« Reply #6 - Posted 2010-04-12 08:10:09 »

A JComponent's paint method calls paintComponent and paintChildren. You shouldn't need to render the children explicitly, and I think the way you're doing it is buggy anyway (because each component will expect the Graphics object to be transformed to its origin, and probably to have a suitable clip).

Also, I have been using the netbeans drag and drop gui builder rather than hand-coding my gui elements. There's not anything in the auto generated code that would obviously cause problems with active rendering is there?
No idea, never used it. (I've scarcely touched Netbeans, and I actively dislike drag-and-drop GUI builders).
Offline Dreamcatchermatt

Junior Member





« Reply #7 - Posted 2010-04-12 14:04:47 »

Quote
because each component will expect the Graphics object to be transformed to its origin, and probably to have a suitable clip

ahhhhh.... *comprehension dawns* Cheesy

One problem I was haveing was all the swing GUI (when it would render) was always stuck to the origin, reguardless of its positioning.

Cool, I learned something today.

Have you come across TWL? I've not had an in-depth look at how it would intergrate, but it looks like what I want.
Offline pjt33
« Reply #8 - Posted 2010-04-12 17:01:43 »

ahhhhh.... *comprehension dawns* Cheesy

One problem I was haveing was all the swing GUI (when it would render) was always stuck to the origin, reguardless of its positioning.

Cool, I learned something today.

Have you come across TWL? I've not had an in-depth look at how it would intergrate, but it looks like what I want.
Seen it before, never used it. I came across it when I was evaluating widget toolkits for a project which was tied to JOGL, and since it seems to be LWJGL-only I didn't give it more than a glance. There may be someone else here who can comment on it.
Offline Dreamcatchermatt

Junior Member





« Reply #9 - Posted 2010-04-13 14:48:25 »

I'm going to have to leave this project for a few weeks untill all my 'real' uni work is finished for the year.

I plan to test out the TWI library and play with a few other ideas for rendering GUI, and I'll post my results here when I get them.

One more thing;
I'm used to building Event Driven GUIs. If I were to make my own button/checkbox/listView classes from scratch using the Graphics2D lib (and probably images, etc, theres a few threads on the forums about doing this).

How would I add Swing or WinForms(.net) style event functionality - onClick, onDrag, onSelectedIndexChange etc?

Does anyone have any books or wiki's thay would recoment me looking at on this topic?

Thanks for all your help guys!
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.

BurntPizza (14 views)
2014-09-19 03:14:18

Dwinin (32 views)
2014-09-12 09:08:26

Norakomi (58 views)
2014-09-10 13:57:51

TehJavaDev (80 views)
2014-09-10 06:39:09

Tekkerue (40 views)
2014-09-09 02:24:56

mitcheeb (62 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (31 views)
2014-09-07 01:12:14

Longarmx (37 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!