[SOLVED] Start a SimpleApplication class inside a Canvas (javaFx or awt)

Hello !
so , i want to make a vehicle selector like this one in my Game Client , but simpler :

So, is there a Simple Way to start any game from a jfx Canvas or awt Canvas inside JFrame or a Stage containing other widgets(buttons,comboboxes,etc), i want to step aside from using OpenGL .

i have found this example but it didnot work for me using awt Canvas on a JFrame !

the example :

    public static void createCanvas(String appClass){
        AppSettings settings = new AppSettings(true);
        settings.setWidth(640);
        settings.setHeight(480);

        try{
            Class clazz = Class.forName(appClass);
            app = (LegacyApplication)clazz.newInstance();
        }catch (ClassNotFoundException ex){
            ex.printStackTrace();
        }catch (InstantiationException ex){
            ex.printStackTrace();
        }catch (IllegalAccessException ex){
            ex.printStackTrace();
        }

        app.setPauseOnLostFocus(false);
        app.setSettings(settings);
        app.createCanvas();
        app.startCanvas();

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

thank you.

1 Like

well , i found this example too https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/jme3-examples/src/main/java/jme3test/awt/TestSafeCanvas.java#L24 followed this part & added it :

 app.createCanvas();
        app.startCanvas(true);

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

now i am hearing my ambient music only , i cannot visualize the game in the canvas or even the canvas borders

1 Like

okay , concerning JFrame & awt i have solved my problem , its my fault , i have dragged & dropped canvas from nb platte & used it & this didnot work ( donot know why ) so i made a panel & created a canvas Instance then added it to the JPanel & it works :

                                    map.createCanvas();
                                    map.startCanvas(true);
                                    JmeCanvasContext context = (JmeCanvasContext) map.getContext();
                                    Canvas canvas = context.getCanvas();
                                    canvas.setSize(config.getWidth(), config.getHeight());
                                    jPanel7.add(canvas);

but for javaFx i didnot try it ,

EDIT in JFrame JPanel :

1 Like
2 Likes

@jayfella Thank you !