Jmonkey Panel Freezes in Swing- aka no focus no motion

Hi,



I am trying to put a simple 3d object inside my Swing application.



This works for the most part however but if I click elsewhere on my GUI the Jmonkey 3d object freezes.



Essentially anytime I click a button or elsewhere on the GUI, aka the panel that has the 3d object is not in focus, the 3d motion stops. It returns when i click on the panel but i need it to always be moving. Also the frames per second will drop when i click to like 100 but will return to a much higher number quickly (probably because it didnt make any frames during the time it was not in focus).



Has anyone encountered this before??



I’ve attached the source code. If you click the button in the window the object stops spinning.



Thanks,

Simran Basi





import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.shape.Box;

import com.jme3.system.AppSettings;

import com.jme3.system.JmeCanvasContext;

import com.jme3.texture.Texture;

import java.awt.Dimension;

import java.awt.FlowLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextArea;



/**

  • you can embed a jme canvas inside a swing application
  • @author pgi

    /

    public class SwingCanvasTest extends SimpleApplication {





    protected Geometry platform, p_line;

    Node pivot;

    @Override

    public void simpleInitApp() {

    flyCam.setDragToRotate(true);

    Box b = new Box(Vector3f.ZERO, (float)2, (float).05, (float)1);

    platform = new Geometry(“red cube”, b);

    Material mat = new Material(assetManager,

    “Common/MatDefs/Misc/Unshaded.j3md”);

    mat.setColor(“Color”, ColorRGBA.Red);

    platform.setMaterial(mat);

    //rootNode.attachChild(platform);



    Box b2 = new Box(Vector3f.ZERO, (float)2.01, (float).1, (float).1);

    p_line = new Geometry(“yellow cube”, b2);

    Material mat2 = new Material(assetManager,

    “Common/MatDefs/Misc/Unshaded.j3md”);

    mat2.setColor(“Color”, ColorRGBA.Yellow);

    p_line.setMaterial(mat2);

    //rootNode.attachChild(p_line);





    pivot = new Node(“pivot”);

    rootNode.attachChild(pivot); // put this node in the scene



    /
    * Attach the two boxes to the pivot node. /

    pivot.attachChild(platform);

    pivot.attachChild(p_line);









    }



    /
    This is the update loop /

    @Override

    public void simpleUpdate(float tpf) {

    // make the player rotate

    pivot.rotate(0, 2
    tpf, 2tpf);



    //platform.rotate(0, 2
    tpf, 0);

    //p_line.rotate(0, 2*tpf, 0);

    }







    public static void main(String[] args) {

    java.awt.EventQueue.invokeLater(new Runnable() {



    public void run() {

    // create new JME appsettings

    AppSettings settings = new AppSettings(true);

    settings.setWidth(640);

    settings.setHeight(480);



    // create new canvas application

    SwingCanvasTest canvasApplication = new SwingCanvasTest();

    canvasApplication.setSettings(settings);

    canvasApplication.createCanvas(); // create canvas!

    JmeCanvasContext ctx = (JmeCanvasContext) canvasApplication.getContext();

    ctx.setSystemListener(canvasApplication);

    Dimension dim = new Dimension(640, 480);

    ctx.getCanvas().setPreferredSize(dim);



    // Create Swing window

    JFrame window = new JFrame(“Swing Application”);

    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    // Fill Swing window with canvas and swing components

    JPanel panel = new JPanel(new FlowLayout()); // a panel

    panel.add(ctx.getCanvas()); // add JME canvas

    panel.add(new JButton(“Swing Component”)); // add some Swing

    window.add(panel);

    window.pack();



    // Display Swing window including JME canvas!

    window.setVisible(true);

    canvasApplication.startCanvas();

    }

    });

    }

    }

app.setPauseOnLostFocus(false);

1 Like