How To embed a PApplet in a Java Project

In my Project you can open multiple Files. For each File I want to show an UnfoldingMap Object in a JPanel (own Instance). When switching between files, the PApplet of the file, which no longer is shown, should by destroyed.

I´m able to show the PApplet, but when I switch to an other file, an Exception is thrown executing PApplet.destroy();

PApplettSubClass:

public class EmbeddedMap extends PApplet 
{
    public void setup()
    {
        size(getWidth(), getHeight(), OPENGL);
    }
    public void draw()
    {
        background(0);      
    }
}

Adding To Panel:

public void init(Object data)
{
    CardLayout cl = new CardLayout();
    JPanel panel = new JPanel(cl);

     if (mapApp == null)
     {
         panel.add(getMapApp(), mapApp.getName());
         cl.show( panel, mapApp.getName() );
     }
}

Creating PApplet:

public EmbeddedMap getMapApp() 
{
    if (mapApp == null)
    {
        mapApp = new EmbeddedMap(cgService);
        mapApp.setPreferredSize(new Dimension(500,200));
        mapApp.init();
     }
     return mapApp;
}   

Destroy / Remove from Panel:

public void pause() 
{
    if (mapApp != null)
    {
        mapApp.stop();
        mapApp.destroy();   //Exception thrown...
        panel.remove(mapApp);
        mapApp = null;
        System.gc();                        
    }
}

Exception:

java.lang.NullPointerException
    at jogamp.opengl.gl4.GL4bcImpl.glDeleteTextures(GL4bcImpl.java:4095)
    at processing.opengl.PJOGL.deleteTextures(PJOGL.java:1977)
    at processing.opengl.PGL.deleteSurface(PGL.java:374)
    at processing.opengl.PJOGL.deleteSurface(PJOGL.java:423)
    at processing.opengl.PGraphicsOpenGL.dispose(PGraphicsOpenGL.java:651)
    at processing.core.PApplet.dispose(PApplet.java:4145)
    at processing.core.PApplet.destroy(PApplet.java:1207)

Answers

Sign In or Register to comment.