I followed the Swing Canvas tutorial for embedding JME was able to get the demo to work. Now want to to use a GUI I created with the Netbeans Swing editor and make instance as the tutorial claims you can do (rather than manually building the gui). I thought it was as simple as doing
JFrame window = new myNetBeanSwingGui();
window.add(ctx.getCanvas());
However this does not seem to be the case. All I get is just a blank Swing window. I even tried adding the ctx.getCanvas to a panel and then add that to the Jframe… but I had the same results. Have any hints on where to look/what to do to find out what I’m doing wrong? I don’t really want a direct answer to my problem as i’m trying to learn.
Just create a JFrame first and then add the code for the OpenGL panel like in the tutorial.
Isn’t that what i’m doing in the code? Does it make more sense to put the OpenGL panel within the myGUI class rather than adding it after I initialize it?
[java] public void run() {
// create new JME appsettings
AppSettings settings = new AppSettings(true);
settings.setWidth(640);
settings.setHeight(480);
// create new canvas application
Main canvasApplication = new Main();
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);
JFrame window = new myGUI();
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();
}[/java]
No, look again the examples: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:swing_canvas
Well I think I got something that works. I wasn’t very familiar with Swing did some further reading on how it worked. I decided to make a custom jframe class with a protected placeholder panel that I could add to. I think this is pretty standard practice, but if there is a better way let me know. Thanks Normem.
[java] public void run() {
// create new JME appsettings
AppSettings settings = new AppSettings(true);
settings.setWidth(640);
settings.setHeight(480);
// create new canvas application
Main canvasApplication = new Main();
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);
myGUI window = new myGUI();
window.jPanel1.setLayout(new FlowLayout());
window.jPanel1.add(ctx.getCanvas());
window.pack();
// Display Swing window including JME canvas!
window.setVisible(true);
canvasApplication.startCanvas();
}[/java]