Swing canvas and LWJGL 3

Hello,

I’m working with JMonkey for years and i’ve tried tu update it to version 3.1beta2 with lwjgl3. It seems that within this configuration, i cannot create JMECanvas for an integration with Swing.

This code was functional with JMonkey 3.0 on LWJGL2:

package sample;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeCanvasContext;

public class SwingPanel extends JPanel {

  private static final long serialVersionUID = 1L;
  
  SimpleApplication application = null;
  
  public SwingPanel(){
    
      super();
    
      setLayout(new BorderLayout());
      
      AppSettings applicationSettings = new AppSettings(true);
      applicationSettings.setWidth(1024);
      applicationSettings.setHeight(768);

      application = new SimpleApplication(){

        @Override
        public void simpleInitApp() {
          Box b = new Box(1, 1, 1);
          Geometry geom = new Geometry("Box", b);
          Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
          mat.setColor("Color", ColorRGBA.Blue); 
          geom.setMaterial(mat);            
          rootNode.attachChild(geom);
        }
        
      };
      application.setSettings(applicationSettings); 
      application.setPauseOnLostFocus(false);

      application.createCanvas();
      JmeCanvasContext ctx = (JmeCanvasContext) application.getContext();
      
      add(ctx.getCanvas(), BorderLayout.CENTER);
  }
  
  
  public static void main(String[] args){
    SwingPanel pn = new SwingPanel();
    
    JFrame frame = new JFrame("SwingPanel");
    frame.setSize(new Dimension(640, 480));
    frame.setPreferredSize(new Dimension(640, 480));
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(pn, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    
  }
}

But now when o use it within JMonkey 3.1beta2 and jme3-lwjgl3 library i get the exception:

janv. 18, 2017 1:32:51 PM com.jme3.system.JmeDesktopSystem initialize
INFOS: Running on jMonkeyEngine 3.1-beta2
 * Branch: HEAD
 * Git Hash: a71fb28
 * Build Date: 2016-11-22
janv. 18, 2017 1:32:51 PM com.jme3.system.JmeDesktopSystem newContextLwjgl
GRAVE: CRITICAL ERROR: Context class is missing!
Make sure jme3_lwjgl-ogl is on the classpath.
java.lang.ClassNotFoundException: com.jme3.system.lwjgl.LwjglCanvas
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at com.jme3.system.JmeDesktopSystem.newContextLwjgl(JmeDesktopSystem.java:196)
	at com.jme3.system.JmeDesktopSystem.newContext(JmeDesktopSystem.java:279)
	at com.jme3.system.JmeSystem.newContext(JmeSystem.java:162)
	at com.jme3.app.LegacyApplication.createCanvas(LegacyApplication.java:511)
	at sample.SwingPanel.<init>(SwingPanel.java:50)
	at sample.SwingPanel.main(SwingPanel.java:58)

Exception in thread "main" java.lang.NullPointerException
	at com.jme3.system.JmeDesktopSystem.newContext(JmeDesktopSystem.java:280)
	at com.jme3.system.JmeSystem.newContext(JmeSystem.java:162)
	at com.jme3.app.LegacyApplication.createCanvas(LegacyApplication.java:511)
	at sample.SwingPanel.<init>(SwingPanel.java:50)
	at sample.SwingPanel.main(SwingPanel.java:58)

The class com.jme3.system.lwjgl.LwjglCanvas is not found and i’ve seen that it is no more present within the jme3-lwjgl3 jar.

My question is so: Is it possible to integrate rendering canvas within a Swing application using JMonkey 3.1beta2 and jme3-lwjgl3 ?

LWJGL3 based on GLFW, it doesn’t work with AWT/Swing. But you have another problem :slight_smile:

What is the another problem ?

LWJGL3 doesn’t have an implementation of the CanvasContext. I have my own implementation for JavaFXCanvas.

Ok, I had not gone so far… And is there a solution to integrate Jmonkey rendering within an AWT/Swing application while keeping LWJGL 3 as renderer ? Even if i have to write a CanvasContext too…

I’m maintaining an heavy application that use JMonkey rendering and i would try to keep the maximum of existing code…

It is my implementation for JavaFX, I think you can try to do the same for AWT…

How I remember, if you create a LWJGL3 context before creating a JFrame, it will work.

1 Like

Ok thanks a lot !! I’m going to write a Swing context and i will tell you if it works. But i have another question: Do you think that JavaFX should be preferred to Swing in order to create GUI ?

Swing is deprecated GUI framework. JavaFX is more modern. I use JavaFX to develop my own editor:

Hey Seinturier, did you get to write the Swing context?

Hello to all,

I’ve just committed an AppState dedicated to the rendering of JMonkey within an AWT component, I’ve posted a dedicated subject here ( AWT / Swing rendering with LWGJL 3.1.X ).

I can suggest to continue this discussion on the new thread.