Question about updating JmeCanvasContext

I was following the tutorial here:



https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:swing_canvas



… and it works fine. But I wanted to create my own custom JFrame class (which I did). I added a button to the class, and when it is pressed I want the event handler to update the SwingCanvasTest application. So the custom JFrame class is quite simple, and looks like this:



[java]public class NewJFrame extends javax.swing.JFrame

{

private SwingCanvasTest canvas;

private JmeCanvasContext ctx;

/** Creates new form NewJFrame /

public NewJFrame(SwingCanvasTest canvas, JmeCanvasContext ctx)

{

this.canvas = canvas;

this.ctx = ctx;

initComponents();

}

/
* This method is called from within the constructor to

  • initialize the form.
  • WARNING: Do NOT modify this code. The content of this method is
  • always regenerated by the Form Editor.

    */

    @SuppressWarnings("unchecked")

    // <editor-fold defaultstate="collapsed" desc="Generated Code">

    private void initComponents() {

    jPanel1 = new javax.swing.JPanel();

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setText("jButton1");

    jButton1.addActionListener(new java.awt.event.ActionListener() {

    public void actionPerformed(java.awt.event.ActionEvent evt) {

    jButton1ActionPerformed(evt);

    }

    });

    jPanel1.add(jButton1);

    jPanel1.add(ctx.getCanvas());

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

    getContentPane().setLayout(layout);

    layout.setHorizontalGroup(

    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 756, Short.MAX_VALUE)

    );

    layout.setVerticalGroup(

    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)

    );

    pack();

    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)

    {

    SwingCanvasTest.ROTATE = !SwingCanvasTest.ROTATE;

    ctx.getCanvas().update();

    }

    // Variables declaration - do not modify

    private javax.swing.JButton jButton1;

    private javax.swing.JPanel jPanel1;

    // End of variables declaration

    }[/java]



    But the line:



    [java]ctx.getCanvas().update();[/java]



    doesn’t cause the jME canvas to get updated. For instance, all that is showing in the canvas is a blue cube, and when I press the button on the JFrame, I want the cube to rotate… which it does if I then click on the canvas with my mouse. But I just want to force a refresh automatically.



    Any thoughts?

Something I noticed is this:



If I don’t happen to click on the JmeCanvasContext at all, then whenever I press the JButton, the cube starts/stops rotating as expected. But as soon as I click on the canvas, if I then click the rotate button, the cube does not rotate until I click the canvas again!



Weird…

You are not supposed to update the panel yourself… You have to start an application.

normen said:
You are not supposed to update the panel yourself.. You have to start an application.


Here is my main class just to clarify what I've done:

[java]
public class SwingCanvasTest extends SimpleApplication {

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
AppSettings settings = new AppSettings(true);
settings.setWidth(640);
settings.setHeight(480);

SwingCanvasTest canvasApplication = new SwingCanvasTest();
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 MainFrame(canvasApplication, ctx);
window.setVisible(true);

canvasApplication.startCanvas();
}
});
}

private Geometry player;
public static boolean ROTATE = false;

@Override
public void simpleInitApp() {

flyCam.setDragToRotate(true);

Box b = new Box(Vector3f.ZERO, 1, 1, 1);
player = new Geometry("blue cube", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
player.setMaterial(mat);
rootNode.attachChild(player);

}

/* This is the update loop */
@Override
public void simpleUpdate(float tpf) {
// make the player rotate
if (ROTATE)
player.rotate(0, 2*tpf, 0);
}
}[/java]

Does that help? Looks to me like I've started the application as you suggest.

I thought I answered this post already?

Apologies! I tried deleting the previous because I thought I had posted in the wrong place. If someone can delete this thread, please do!