[SOLVED] Launching from a swing interface

I have an application I'm trying to launch from a swing GUI. I'm having problems with multiple launches - actually, everything becomes very messed up with the graphics, nothing is visible, although monotonous funky chunks sort of resemble my scene.



I created a test, and these problems remain even if the application consists of only a single box - specifically, lighting doesn't seem to work for it (I think all the other issues are connected to this, but I'm using textures and shaders, so If it makes sense to make a more sophisticated test I definitely will). I don't really have any idea where to start looking for issues, so I though maybe someone could help me out.



The test is a simple jme application, and a swing launcher for it:



import com.jme.app.SimpleGame;
import com.jme.math.Vector3f;
import com.jme.scene.shape.Box;

public class SimpleTest extends SimpleGame
{
   @Override
   protected void simpleInitGame()
   {
      rootNode.attachChild(new Box("", Vector3f.ZERO, 5,5,5));      
   }    
}




import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

import com.jme.app.AbstractGame.ConfigShowMode;


public class SimpleSwingLauncher extends JFrame
{
   public SimpleSwingLauncher()
   {
      JButton launcher = new JButton();
      
      ActionListener l = new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent e)
         {
            SimpleTest test = new SimpleTest();
            test.setConfigShowMode(ConfigShowMode.NeverShow); /* There's a separate issue with the configuration dialog, there's a separate thread about this ( http://www.jmonkeyengine.com/jmeforum/index.php?topic=10666.msg81095#msg81095 ) */
            test.start();
         }         
      };
      
      launcher.addActionListener(l);      
      this.add(launcher);
   }
   
   
   public static void main(String[] args)
   {
      SimpleSwingLauncher slauncher = new SimpleSwingLauncher();
      slauncher.setSize(400, 400);      
      slauncher.setVisible(true);
   }
}

You will need to use momoko_fans context system for multiple canvases (which is what I believe you are trying to do). 

http://www.jmonkeyengine.com/jmeforum/index.php?topic=7168.0

Well, I really only need one canvas at a time. Isn't there a more direct solution? Doesn't using a multiple canvas system for this seem like a hack?



The scenario I'm trying to achieve is really simple: open game from menu -> close game and return to menu -> open game from menu again, just like the first time - initialize from scratch, etc. Opening the game works for the first time, but not the second time.



I'd be okay with completely destroying everything before launching the second time, If I knew what or how to destroy… I really have no idea what resource could mess things up for reinitialization :frowning:

I think the best, easiest solution for your particular issue then is to just have one canvas (ever) and re-initialize that one when needed.



(You are currently using a native window, check out the canvas implementation; which can just be added directly to your JFrame…)

I think It's actually better for me to use a native window (I think), because I'm usually launching my application fullscreen (I'm not sure if that's possible with a canvas?).



To give some background, I have this science application for testing people with virtual mazes (there's a whole hundred-year history of this type of research with animals, and human research is being conducted today with different virtual mazes). There are lots of different configurations for the experiment protocol that can be switched, saved and loaded, which are then launched in batch for the person undergoing the experiment to work through.



All the configurations are in a swing GUI, but I wanted to launch the 3D part from the GUI, but separately, like a common 3D application that would have been launched from an .exe file. In fact, that was a workaround I was considering - to compile the two modules separately and have one launch the other.



So the ideal solution would be to be able to launch the native window multiple times, and not use canvases. Doesn't it generally seem like a bug - that it can be launched one time, and not more than one time?

basixs said:

I think the best, easiest solution for your particular issue then is to just have one canvas (ever) and re-initialize that one when needed.

(You are currently using a native window, check out the canvas implementation; which can just be added directly to your JFrame...)


Yes, that's one solution.

For a software engineering class I built a checkers swing/jme-canvas game where you log into a server to play against others. Players would be able to create/join tables in the lobby and play/observe games. We just had a single canvas in another window and for new games just re-initialize the scene. When games were initialized, the canvas would fade in, and would fade out when the player returns to the lobby.

You might try calling Game.finish() (when exiting) to see if that helps…

basixs said:

You might try calling Game.finish() (when exiting) to see if that helps...


That's how SimpleGame quits when pressing "ESC", the issue is still there... (easily testable on the test in the initial post - just quit using "ESC"  }:-@)

I think I found the solution. It appears that the system provider must be reset, or it must explicitly destroy the display system.



In the test launcher in the initial post, this should be the launch procedure



   SimpleTest test = new SimpleTest();
   DisplaySystem.resetSystemProvider();
   test.setConfigShowMode(ConfigShowMode.NeverShow);            
   test.start();



or this:


   SimpleTest test = new SimpleTest();   
   test.setConfigShowMode(ConfigShowMode.NeverShow);            
   test.start();
   DisplaySystem.getSystemProvider().disposeDisplaySystem();



Both ways require proper manual resource handling (releasing textures manually, etc).

Thanx for posting what you figured out, hopefully it will help someone in the future :slight_smile: