TestSafeCanvas errors

Hello!
I’m trying to run a template file TestSafeCanvas, but I get errors:
compile-single:
run-single:
java.lang.ClassNotFoundException: jme3test.post.TestRenderToTexture
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:375)
at jme3test.awt.TestCanvas.createCanvas(TestCanvas.java:214)
at jme3test.awt.TestCanvas.main(TestCanvas.java:260)
Exception in thread “main” java.lang.NullPointerException: Cannot invoke “com.jme3.app.LegacyApplication.setPauseOnLostFocus(boolean)” because “jme3test.awt.TestCanvas.app” is null
at jme3test.awt.TestCanvas.createCanvas(TestCanvas.java:226)
at jme3test.awt.TestCanvas.main(TestCanvas.java:260)
Java Result: 1

Can anyone please tell me what I’m doing wrong?

Could we we your source for the (minimal that reproduces the problem) application? Or if this is an example project somewhere then could you link to it

1 Like
package jme3test.awt;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeCanvasContext;
import java.awt.Canvas;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

public class TestSafeCanvas extends SimpleApplication {

    public static void main(String[] args) throws InterruptedException{
        AppSettings settings = new AppSettings(true);
        settings.setWidth(640);
        settings.setHeight(480);

        final TestSafeCanvas app = new TestSafeCanvas();
        app.setPauseOnLostFocus(false);
        app.setSettings(settings);
        app.createCanvas();
        app.startCanvas(true);

        JmeCanvasContext context = (JmeCanvasContext) app.getContext();
        Canvas canvas = context.getCanvas();
        canvas.setSize(settings.getWidth(), settings.getHeight());

        

        Thread.sleep(3000);

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                app.stop();
            }
        });
        frame.getContentPane().add(canvas);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        Thread.sleep(3000);

        frame.getContentPane().remove(canvas);

        Thread.sleep(3000);

        frame.getContentPane().add(canvas);
    }

    @Override
    public void simpleInitApp() {
        flyCam.setDragToRotate(true);

        Box b = new Box(1, 1, 1);
        Geometry geom = new Geometry("Box", b);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
        geom.setMaterial(mat);
        rootNode.attachChild(geom);
    }
}
1 Like

That test is known to fail with LWJGL v3:

As I understand it, the workarounds are (1) to use LWJGL v2 (in other words, jme3-lwjgl instead of jme3-lwjgl3) or else (2) avoid using Swing/AWT.

I get these errors both in jMonkeyEngine SDK and in IDEA, experiments in jme3-lwjg do not change the result. And disabling swing is unacceptable for me, since I am interested in the combination with swing, this is the essence of this example

If you can read around all of the extra stuff, here is an app that builds an IDE-like window with a JME window in it.

No matter what, lwjgl3 won’t work… it has to be lwjgl2.

Just tried TestSafeCanvas and it works fine for me with lwjgl2.

1 Like

You said you tried the SDK. Did you do it the following way:

  1. File | New Project,
  2. Choose Category: JME3, JME3 Tests
  3. Run project
  4. Choose TestSafeCanvas

For me, this works out of the box.

Edit: Instead running with TestChooser, find TestSafeCanvas in IDE and run it independently (in SDK, right click the file in the projects view and select run.

By the way, I noticed when running via TestChooser it bypasses the main method (which has all the swing stuff) and runs it as a regular JME app (not embedded in JFrame).

I needed to change mainClassName in jme3-examples build.gradle to jme3test.awt.TestSafeCanvas to successfully test the app.

For reference, this is how it should look like. (see the Windows title, it is named “Test”)

This seems to be the case yes. Is it correctly formulated the whole thing? TestChooser assumes that everything that is LegacyApplication is invoked differently, main method it used if this is not the case (in TestSafeCanvas is should always use the main?).