Swing GUI in Games?

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? :confused:



// 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…


// 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 :confused:

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

Hello

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.

Thanks for your advice guys :slight_smile:

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

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.

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

for example;


class myWidgit extends jPanel
{
   public void render(Graphics2D g)
   {
       paint(g);
   }
}



// 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 :slight_smile:

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).

No idea, never used it. (I’ve scarcely touched Netbeans, and I actively dislike drag-and-drop GUI builders).

[quote]because each component will expect the Graphics object to be transformed to its origin, and probably to have a suitable clip
[/quote]
ahhhhh… comprehension dawns :smiley:

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.

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!