Implementing Swing into Canvas

Before I started JME, I was using Swing’s Graphics2D stuff to make games (which probably wasn’t the best of ideas). Can JPanel can be hooked onto the jmonkey canvas? Is it even a good idea?

Of course, in this case, I’m only interested in the GUI that swing provides.

public class Main extends JPanel {


    public static void main(String[] args) {
		new Main().run();
	}
    public void run() {
		// set window options
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setLocation(200, 100);
		window.setResizable(false);


        // hook JPanel onto the window
        // this is what i want to do
		window.add(this);

				
		// update loop
		while (true) {
			try {
				Thread.sleep(15);
			} catch (InterruptedException ex) {
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}


            // logic and animation here

			
			// repaint window
			window.repaint();
			window.setSize(811, 785);
			window.setVisible(true);
		}
	}

    @Override
    public void paintComponent(Graphics graphics) {
	    Graphics2D g = (Graphics2D)graphics;
	    super.paintComponent(graphics);
        // graphics here
    }
}

you should look at this.

1 Like

I’d advice against it. LWJGL3 has problems when using swing, you could stick with LWJGL2 but you would lock yourself with the old version and won’t be able to upgrade later.
Also v2 has other problems like it crashes with recent jvm builds (al least on linux).

I recommend you to try lemur as is the most “official” gui framework for jME and it has many similarities with swing.

You may also look into javafx, but the current implementation has some issues that have to be worked around. I have a branch that has the fixes in it, but I need to push it to github (I will one of these days when I get a second)

What kind of GUI, though?

For a basic game menu, Lemur (which I based loosely on Swing ideas because I’m a long time Swing developer) will take much less code for the same UI, as compare to Swing.

1 Like

Just keep in mind threading issues with Swing. Because Swing operates on its own thread(s), you will have to carefully enqueue operations for jme when operations originate from Swing. Javafx has good support for handling this as it is very apparent what operations take place on what threads within jfx, but I do not know if this is the case for Swing, perhaps someone else can comment on that. If you use Lemur, it operates on the render thread so no need to worry about any thread synchronization or enqueuing tasks.

1 Like

@codex as for performance issues,

You must be careful while updating the Jme scene graph(attach/detach nodes & baseAppStates mainly) from swing, just do

app.enqueue(()->{
//Jme code
});

From the swing thread to update the scene graph !

Do that to update swing util components from jme game :

SwingUtilities.invokeLater(()->{
//Swing code
});

Otherwise, I think no major performance issues like low fps or anything related.

This would be applied also to Android views (the UI thread) mainly the nodes/models/Geometries attachment/detachment(new things in your scene)).

But, it’s encouraged to use Lemur, it provides direct communication with jme since it’s geometric based, & you can also use Lemur to attach buttons & gui to game objects.