swaping the canvas?

hi all,

Could anyone point me in the direction of showing me how to swap an old canvas with a new one…?

I have the following code to set the initial canvas (which works 100% of the time)


public final Animator animator;
GLCanvas cvs = setupGLPanel(640, 480);
animator = new Animator(cvs);
add(constructInterface(cvs), BorderLayout.CENTER);      
show();
animator.start();

but i need to be able to replace this canvas with a totally new one at some point in time. Is there any sample code to indicate how to do this?


Stop animator
remove old canvas
register new canvas obj with animator (trouble with this as it is final)
add canvas to layout??

Cheers,

ribot.

Can’t you do it the ‘normal’ way?
(replacing components on a container)

animator.stop();
frame.remove(cvs);
GLCanvas cvs2 = ...;
// create new animator
frame.add(cvs2, BorderLayout.CENTER);
frame.validate();
frame.repaint(); // ?

Thanks for the reply skippy. I have now coded the following, which produces a static non-animated image of my new canvas. But due to the having the Animator object set to final status, I am unable to register the new canvas with it = thus no animation. Anyone know how to get around this?



      private void changeLab(String labPathName) {
            String className = "components." + labPathName + ".Renderer";
            
            GLEventListener glRenderer;
            try {
                  glRenderer = (GLEventListener)Class.forName(className).getConstructor(new Class[] {Integer.TYPE, Integer.TYPE}).newInstance(new Object[] {new Integer(100), new Integer(100)});
                  //canvas.addGLEventListener(glRenderer);
                  
                  // stop old animator
                  animator.stop();
                  // remove the old canvas from the layout
                  panel.remove(cvs);
                  // create a new canvas
                  cvs = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
                  // register the new lab to the canvas
                  cvs.addGLEventListener(glRenderer);
                  // the following line is a prob as animator is set to final.  :(
                  //animator = new Animator(cvs);
                  // add the new canvas to the gui panel
                  panel.add(cvs, BorderLayout.CENTER);
                  panel.validate();
                  // restart the animation - but I think this is still linked to the old canvas,
                  // thus, no animation. just a static picture.
                  animator.start();
                  
            } catch(Exception ex) {
                  Debug.outln("Failed to change lab: " + ex);
            }
      }


You will need a new Animator.

any pointers as to what I am doing wrong? I have uncommented the line that creates another Animator, and am able to swap the canvas and animate, but an error command is output during the process:

2004-03-03 16:44:15.109 java[9117] *** _NSAutoreleaseNoPool(): Object 0xaa11d20 of class NSSurface autoreleased with no pool in place - just leaking

The second time the canvas is swapped, a much bigger problem is output to the command line:


java.lang.IllegalMonitorStateException: current thread not owner
      at net.java.games.jogl.impl.JAWT_DrawingSurface.Unlock0(Native Method)
      at net.java.games.jogl.impl.JAWT_DrawingSurface.Unlock(JAWT_DrawingSurface.java:60)
      at net.java.games.jogl.impl.macosx.MacOSXOnscreenGLContext.unlockSurface(MacOSXOnscreenGLContext.java:236)
      at net.java.games.jogl.impl.macosx.MacOSXOnscreenGLContext.free(MacOSXOnscreenGLContext.java:155)
      at net.java.games.jogl.impl.GLContext.invokeGL(GLContext.java:265)
      at net.java.games.jogl.impl.macosx.MacOSXOnscreenGLContext.invokeGL(MacOSXOnscreenGLContext.java:79)
      at net.java.games.jogl.GLCanvas.displayImpl(GLCanvas.java:182)
      at net.java.games.jogl.GLCanvas.display(GLCanvas.java:82)
      at net.java.games.jogl.Animator$1.run(Animator.java:104)
      at java.lang.Thread.run(Thread.java:552)
2004-03-03 16:48:23.612 java[9120] *** _NSAutoreleaseNoPool(): Object 0xa91b0a0 of class NSSurface autoreleased with no pool in place - just leaking

Any hints would be greatly appreciated! Thanks.

Is there any sample code that I have missed that contains canvas swapping code?

If you can post a simple, working test program that exhibits this behavior I will take a look at it.

[quote]If you can post a simple, working test program that exhibits this behavior I will take a look at it.
[/quote]
Check pm