Swing menus and the old heavy and light problem

Hi,

I’ve made a 3d game in java that uses what seems to be heayweight, it runs inside a full screen swing window. My current idea is to use swing to create a menu system for the game. I really wanted to use swing because of Synth and making really good looking menus seems fairly straight forward. I had the idea that I would be able to to put swing buttons and so forth over my game as it ran, but after doing a lot of reading this does not seem possible because of the lightwieght nature of swing and the heavyweight nature of my game.

I dont really have much experience in this area and was wondering if anyone had some ideas of way around my problem?

Thanks

As long as you put your Swing controls inside a heavyweight that will appear on top of the 3d heavyweight you should be OK.

For menus you can do the usual thing to make Swing use heavyweight pop-ups. If you can get away with making the Swing button areas rectangular then you should be OK for that too, just put a Swing widget inside a heavyweight Panel to make sure it draws on top of the other heavyweight. Hmm… there may be some issues with z-order… I would try to write up a quick test app.

I know nothing about Java3D, but won’t you run into threading issues with Swing’s Event Dispatch Thread (EDT) if you try to include Swing menus?

Or is the Java3D rendering thread somehow coordinated with the EDT?

I am interested because I have had to jump through hoops to get Swing working in my direct-rendering 2D game without deadlock.

Thanks :slight_smile:
Keith

Thanks swpalmer if you could whip somthing up and let me know how its goes, I’m curretly a little stumped by the whole thing.

/*
 * Main.java
 *
 * Created on May 17, 2006, 11:32 PM
 */

package heavyswing;

import java.awt.Canvas;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Window;
import javax.swing.JButton;

/**
 *
 * @author Scott Palmer
 */
public class Main
{
	
	/** Creates a new instance of Main */
	public Main()
	{
	}
	
	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) throws InterruptedException
	{
		Window window = new Window(null);
		window.setLayout(null);
		window.setBounds(10,10,220,200);

		JButton swingButton = new JButton("I'm Swing!");
		window.add(swingButton);
		swingButton.setLocation(10,10);
		swingButton.setSize(swingButton.getPreferredSize());
		
		JButton swingButton2 = new JButton("I'm Swing on a Heavyweight!");
		Panel heavyPanel = new Panel(null);
		heavyPanel.add(swingButton2);
		swingButton2.setLocation(0,0);
		swingButton2.setSize(swingButton2.getPreferredSize());

		window.add(heavyPanel);
		heavyPanel.setLocation(10,40);
		heavyPanel.setSize(swingButton2.getPreferredSize());

		// Add a heavyweight canvas
		Canvas canvas = new FancyCanvas();
		// comment out the next line to see the other Swing button
		window.add(canvas); 
		canvas.setLocation(0,0);
		canvas.setSize(220,200);
		
		// It seems that the components are drawn in the reverse order in
		// which they were added. But is it always so?
		
		window.setVisible(true);
		
		Thread.sleep(10000);
		window.setVisible(false);
		window.dispose();
	}
}

// This could be a 3D canvas
class FancyCanvas extends Canvas
{
	@Override
	public void paint(Graphics g)
	{
		g.drawLine(0,0,getWidth(),getHeight());
		g.drawLine(0,getHeight(),getWidth(),0);
	}
}

Thanks for your help! I have a problem though, when I use the panel the panel gets drawn the but not the button :(. I’m going to have a play with it and see if I can fix it. If you have any ideas let me know.

Also I was thinking is it possible to draw the button maually? I was thinking of calling draw each loop of my 3d app.