JmeCanvasContext JFrame wrong size not updating

Hi everyone.-

I have followed this tutorial, [https://jmonkeyengine.github.io/wiki/jme3/advanced/swing_canvas.html]
(https://jmonkeyengine.github.io/wiki/jme3/advanced/swing_canvas.html) , its very good, but i have created a JFrame with the editor and wanted to use it with this method, the problem is that the app doesnt close niether shows my button and i also want the applet to be centered in the canvas, can you help me achieve this? yes i know i am a begginer but please, some help would be nice, i have done a lot of progress since the first time i used JMonkeyEngine. Thanks. :grinning:

My code so far is.-

package atom.bin;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AppState;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeCanvasContext;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class BinaryWritter extends SimpleApplication {

   protected static int windowWidth;
   protected static int windowHeight;

   public BinaryWritter()
   {
	// TODO Auto-generated constructor stub
    }

public BinaryWritter(AppState... initialStates) {
	super(initialStates);
	// TODO Auto-generated constructor stub
}

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

DisplayMode window = Display.getDisplayMode();		
if(window.isFullscreenCapable()){
			windowWidth = window.getWidth();
			windowHeight = window.getHeight();
			
			try {
				Display.setDisplayMode(window);
				///musica msc = new musica();
				//Display.create();
			} catch (LWJGLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		java.awt.EventQueue.invokeLater(new Runnable() {
		      public void run() {
		         // ... see below ...
		    	  BinaryWritter bin = new BinaryWritter();
		  		
		  		
		  		//Runnable r = new energy();
		  		//new Thread(r).start();
		  		

		  		//JFrame Energy;
		  		//Energy = new energy();
		  		//Energy.pack();
		  		//Energy.setVisible(true);
		  				
		  		bin.setShowSettings(false);
		  		
		  		AppSettings settings = new AppSettings(true);
		  		
		  		settings.put("Width", windowWidth);

		  		settings.put("Height", windowHeight);

		  		settings.put("Title", "BinaryWritter");

		  		settings.put("VSync", true);
		  		
		  		bin.setSettings(settings);
		  		
		  		bin.createCanvas();
		  		
		  		JmeCanvasContext ctx = (JmeCanvasContext) bin.getContext();
		  		
		  		ctx.setSystemListener(bin);
		  		
		  		Dimension dim = new Dimension(800, 600);  ///Era 640 x 480
		  				
		  		ctx.getCanvas().setPreferredSize(dim);
		  				  		
		  		JFrame window = new JFrame("BinaryWritter");
		  		
		  		window.setBounds(100, 100, 450, 300);
				
				window.getContentPane().setLayout(null);
		  		
		  		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		  		
		  		JPanel panel = new JPanel(new FlowLayout()); // a panel
		  		// add all your Swing components ...
		  		JButton btnAceptar = new JButton("Aceptar");
		  		
		  		btnAceptar.setForeground(new Color(0, 0, 0));
				btnAceptar.setBackground(new Color(0, 153, 204));
				btnAceptar.setBounds(132, 216, 117, 25);
		  		
		  		btnAceptar.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e) {
						//JOptionPane.showMessageDialog(null, "Mensaje de Prueba");
						//btnAceptarActionPerformed(e);
						ctx.getCanvas().update(null);
										
					}
				});
		  		
		  		panel.add(btnAceptar);
		  	  	// add the JME canvas
		  		panel.add(ctx.getCanvas());
		  		
		  		//private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt)

		  		//{
		  			//SwingCanvasTest.ROTATE = !SwingCanvasTest.ROTATE;
		  			//ctx.getCanvas().update();

		  		//}
		  		
		  		window.add(panel);
		  		window.pack();
		  		window.setVisible(true);
		  		
		  		bin.startCanvas(true);

		      }
		    });				
	}

}

edit.-
I have modified the source thanks to all of you, but i have a error “Scene cannot be null”, i assume the assets cant be loaded but i dont know why. Here is the modified code.

package atom.bin;

import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AppState;
import com.jme3.asset.AssetManager;
import com.jme3.light.DirectionalLight;
//import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.system.AppSettings;
//import java.awt.BorderLayout;
import com.jme3.system.JmeCanvasContext;
//import com.jme3.texture.Texture2D;
import com.jme3.ui.Picture;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

//import java.awt.event.ActionEvent;

//import java.awt.event.ActionListener;
//import java.awt.EventQueue;

//import java.awt.BorderLayout;

//import javax.swing.JButton;
//import javax.swing.JFrame;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
//import atom.bin.energy;

//import com.jme3.material.Material;
//import com.jme3.math.Vector3f;
//import com.jme3.scene.Geometry;
import com.jme3.scene.Node;

//import com.jme3.scene.shape.Box;
import com.jme3.scene.Spatial;
//import com.jme3.math.ColorRGBA;
/**
 * @author jordi
 *
 */
public class BinaryWritter extends SimpleApplication {

	/**
	 * 
	 */
	
	protected static int windowWidth;
	protected static int windowHeight;
	public static AssetManager asManager;
	public static Node gRootNode;
	
	
	//private static JButton btnNewButton;

	public BinaryWritter() {
		// TODO Auto-generated constructor stub
		this.assetManager = asManager;
		this.rootNode = gRootNode;
				
	}

	/**
	 * @param initialStates
	 */
	public BinaryWritter(AppState... initialStates) {
		super(initialStates);
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.jme3.app.SimpleApplication#simpleInitApp()
	 */
	@Override
	public void simpleInitApp() {
		// TODO Auto-generated method stub
		
		
		flyCam.setDragToRotate(true);
				 		
		DisplayMode window = Display.getDisplayMode();
		
		if(window.isFullscreenCapable()){
			windowWidth = window.getWidth();
			windowHeight = window.getHeight();			
					
			try {
				Display.setDisplayMode(window);	
						    	
				///musica msc = new musica();
				//Display.create();
			} catch (LWJGLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		java.awt.EventQueue.invokeLater(new Runnable() {
		      public void run() {
		         // ... see below ...
		    	  BinaryWritter bin = new BinaryWritter();
		    	  
		  		//Runnable r = new energy();
		  		//new Thread(r).start();
		  		

		  		//JFrame Energy;
		  		//Energy = new energy();
		  		//Energy.pack();
		  		//Energy.setVisible(true);
		  				
		  		bin.setShowSettings(false);
		  		
		  		AppSettings settings = new AppSettings(true);
		  		
		  		settings.put("Width", windowWidth);

		  		settings.put("Height", windowHeight);

		  		settings.put("Title", "BinaryWritter");

		  		settings.put("VSync", true);
		  		
		  		bin.setSettings(settings);
		  		
		  		bin.createCanvas();
		  		
		  		JmeCanvasContext ctx = (JmeCanvasContext) bin.getContext();
		  		
		  		ctx.setSystemListener(bin);
		  		
		  		Dimension dim = new Dimension(1024, 768);  ///Era 640 x 480
		  				
		  		ctx.getCanvas().setPreferredSize(dim);
		  				  		
		  		JFrame window = new JFrame("BinaryWritter");
		  		
		  		window.setBounds(100, 100, 450, 300);
				
				//window.getContentPane().setLayout(null);
		  		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		  		
		  		window.getContentPane().add(ctx.getCanvas());
		  		
		  		JPanel panel = new JPanel(new FlowLayout()); // a panel
		  		// add all your Swing components ...
		  		JButton btnInicio = new JButton("INICIO");
		  		
		  		btnInicio.setForeground(new Color(0, 0, 0));
				//btnInicio.setBackground(new Color(0, 153, 204));
				btnInicio.setBounds(189, 28, 117, 25);
		  		
		  		btnInicio.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e) {
						JOptionPane.showMessageDialog(null, "Mensaje de Prueba");
						//btnAceptarActionPerformed(e);
						//ctx.getCanvas().update(null);
						
										
					}
				});
		  		
		  		JButton btnServicios = new JButton("Servicios");
				btnServicios.setBounds(189, 65, 117, 25);
				//frame.getContentPane().add(btnServicios);
				
				JButton btnContacto = new JButton("Contacto");
				btnContacto.setBounds(189, 145, 117, 25);
				//frame.getContentPane().add(btnContacto);
				
				Spatial city = asManager.loadModel("BinaryWritter/src/binarywritter.obj");
				  //Material mat_default = new Material(asManager, "Common/MatDefs/Misc/ShowNormals.j3md");
				  //city.setMaterial(mat_default);
				gRootNode.attachChild(city);
				DirectionalLight sun = new DirectionalLight();

				sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f).normalizeLocal());

				gRootNode.addLight(sun);
				
				JButton btnGaleria = new JButton("Galeria");
				btnGaleria.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						//Texture2D texture = (Texture2D) assetManager.loadTexture("BinaryWritterX/ImagenesBinarywritter/binary-code-475664.jpg");
						//float width = texture.getImage().getWidth();
						//float height = texture.getImage().getHeight();
						
						Picture picture = new Picture("BinaryWritter");
						picture.setImage(bin.getAssetManager(), "Binaywritter/src/binary-code-475664.jpg", true);
						//final boolean useAlpha = true;
						picture.setPosition(settings.getWidth()/4, settings.getHeight()/4);
						picture.setWidth(settings.getWidth()/2);
						picture.setHeight(settings.getHeight()/2);						
						bin.getRootNode().attachChild(picture);
					}
				});

				btnGaleria.setBounds(189, 102, 117, 25);
				//frame.getContentPane().add(btnGaleria);
		  		
		  		panel.add(btnInicio);
		  		panel.add(btnServicios);
		  		panel.add(btnContacto);
		  		panel.add(btnGaleria);
		  	  	// add the JME canvas
		  		panel.add(ctx.getCanvas());
		  		
		  		//private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt)

		  		//{
		  			//SwingCanvasTest.ROTATE = !SwingCanvasTest.ROTATE;
		  			//ctx.getCanvas().update();

		  		//}
		  				  	  
		  		window.add(panel);
		  		window.pack();
		  		window.setVisible(true);
		
		  		
		  		bin.startCanvas(true);  		
		  		

		      }
		     
		    });		
		 
	}

}
1 Like

window.getContentPane().add(ctx.getCanvas());

1 Like

Thank you now it closes !!! Well not completly

1 Like

That wiki page is in need of updating.
TestCanvas.java

2 Likes

I also got the JDWP exit error JVMTI_ERROR_WRONG_PHASE(112) error, but dont know how to fix it.

1 Like

So maybe TestJMEDesktop is my solution, thank you

1 Like