Canvas issues

Well, I have finally figured out what the problem is; I am posting my solution here…



The issue is that the Canvas cannot be removed from the JPanel (at any time it seems); otherwise when the canvas is re-added graphics (mainly textures) become garbled or disappear entirely, or even worse can cause the previous hard error.  My solution is to never remove the canvas but rather to set it not visible ( Canvas.setVisible(false) ), however the layout must be re-adjusted if any JPanels are added at the layout position that the canvas is in.  Otherwise the Canvas will not behave according to the layout position it has been set at.



Typical scenario of reseting a JPanel set at the center using BorderLayout:


JPanel.getLayout().addLayoutComponent( BorderLayout.CENTER, Canvas );



I hope this helps someone else in the future :)

(Another solution might be to re-initalizing the graphics context, however it seems more elegant to only initialize it when its needed.)
basixs said:

The issue is that the Canvas cannot be removed from the JPanel (at any time it seems); otherwise when the canvas is re-added graphics (mainly textures) become garbled or disappear entirely, or even worse can cause the previous hard error. 

I'm running against this problem as well. I'm not in control of if/when the canvas is removed and added again because I'm using the flexdock framework to manage window docking, and it seems when ever I close a window it re-organizes swing components which results in the canvas losing textures. So far, I'm not much a fan of the new canvas implementations.

Alright, did a little research over in the LWJGL cvs browser and extended LWJGLCanvas with a few modifications. The problem is, every time you add the canvas to a container the GL context gets initialized, and every time you remove the canvas the context gets destroyed. This doesn't work for me. I've changed it so the GL context only gets created once, and it doesn't get destroyed until you call cleanup() on the canvas. With this you can freely remove the canvas and add it back without losing anything, not even textures.



Here it is for those who want:



import org.lwjgl.LWJGLException;

import com.jmex.awt.lwjgl.LWJGLCanvas;

/**
 * Custom LWJGL canvas implementation to allow full control of when the GL
 * context is initialized and destroyed.
 *
 * @author Andrew Carter
 */
public class CustomLWJGLCanvas extends LWJGLCanvas {

   /**  */
   private static final long serialVersionUID = 6392724324465695651L;

   /**    */
   private boolean glInitialized = false;

   /**
    * Constructor.
    *
    * @throws LWJGLException
    */
   public CustomLWJGLCanvas() throws LWJGLException {

   }

   /**
    * Overridden to prevent initializing the canvas every time it's added to a
    * component.
    */
   @Override
   protected void initGL() {

      if(!glInitialized) {
         super.initGL();

         glInitialized = true;
      }
   }

   /**
    * Overridden to prevent destroying the context when the canvas is removed.
    */
   @Override
   public void removeNotify() {

      // no-op
   }

   /**
    * Call this when done with the canvas.
    */
   public void cleanup() {

      // This destroys the context
      super.removeNotify();
   }
}

Thanx Nymon :slight_smile:



Although I have all the workarounds in place that I need, its great to know where the problem is.

Hey Nymon,



Just wanted to point you towards this:

http://www.jmonkeyengine.com/jmeforum/index.php?topic=9981.msg76267#msg76267



and give you a big thanx :D, without your debugging I would have been rather stumped for a solution on this one.