AWTGLCanvas using in Swing

Hi,

i allready included a well working version of the AWTGears Demo as a Test in my Swing App. Looking at the CPU Usage i like it very much :slight_smile:
Now i would like to include it more in my new Project, but i have trouble with that AWT/Swing mixing in this case: I displayed a AWTGLCanvas, later the user clicks on a button and the Canvas will disapear(removed from JPanel), when the user clicks again the canvas will be added again to the JPanel, i also see that the Thread is running and outs the FPS each 5 Seconds but i cannot see any Gears again, the Panel remains black.Can someone give me a hint what to do ?

May i have to save the GL Context ?

Stay Tuned,
Jens

Post a quick sample.

Cas :slight_smile:

Ok, there is the Code that do the Switch between
both Views when user hits a Button:


public class PlaylistSpectrumPanel
      extends JPanel implements Toogleable
{
  private static Logger log = Logger.getLogger(PlaylistSpectrumPanel.class);

  /**
   * The Spectrum Panel
   */
  private final SpectrumPanel spectrumPanel;
  /**
   * The Playlist Panel
   */
  private final Playlist playlistPanel;

  private boolean isSpectrumShown = true;

  private ServerPanel serverPanel = new ServerPanel(new ServerTable());

  private JComponent lastFileView = null;

  private final AWTCanvasTest awtCanvas;

  public PlaylistSpectrumPanel(AWTCanvasTest awtCanvas, SpectrumPanel spectrumPanel, Playlist playlistPanel)
  {
      super();
      this.spectrumPanel = spectrumPanel;
      this.playlistPanel = playlistPanel;
      this.awtCanvas = awtCanvas;
      setLayout(new BorderLayout());
      add(awtCanvas, BorderLayout.CENTER);
      lastFileView = playlistPanel;

      System.out.println("awtCanvas.getPreferredSize():" + awtCanvas.getPreferredSize());

  }

  public void toogle()
  {
      toogleSpectrum();
  }

  /**
   * Switch between Spectrum and Playlist view
   */
  private final void toogleSpectrum()
  {
      if (isSpectrumShown)
      {
        //remove(spectrumPanel);
        remove(awtCanvas);
        add(lastFileView, BorderLayout.CENTER);
        awtCanvas.donotShow();
      }
      else
      {
        remove(lastFileView);
        switchView();
        //add(spectrumPanel, BorderLayout.CENTER);
        add(awtCanvas, BorderLayout.CENTER);
        awtCanvas.showAgain();
      }

      isSpectrumShown = isSpectrumShown ? false : true;
      revalidate();
      updateUI();
  }

  private final void switchView()
  {
      if (lastFileView.equals(playlistPanel))
      {
        lastFileView = serverPanel;
      }
      else
      {
        lastFileView = playlistPanel;
      }
      System.out.println("New View:" + lastFileView);
  }


}


Actual the SpectumPanel isnt used by this example,
AWTCanvasTest is the GLGear Test from LWJGL Examples (the heavyweight canvas), with this extension:

 
  public void showAgain()
  {
      canvas0.setVisible(true);
      canvas0.addNotify();
  }
  
  public void donotShow()
  {
      canvas0.setVisible(false);
      canvas0.removeNotify();
  }


I Tested a bit more whats wrong, for me it seems that i lost the Draw Context when removing the Canvas, but im not able to restore it,
the API Doc to makeCurrent in AWTGLVCanvas:


makeCurrent

public void makeCurrent()
                 throws LWJGLException

    Make the canvas' context current. It is highly recommended that the context is only made current inside the AWT thread (for example in an overridden paint()).

    Throws:
        LWJGLException

Looked at the LWJGL Sources paint(Graphics g) is declared as final,and seems to do what i tried …setting the GL Context again,so i was not able to overwrite it …

Also i implemented componentShown(…) and componentRemoved(…)…this Methods will not invoked when my canvas should show up again. The Shown Method should be called when adding the Canavs again, maybe a bug (But not responsible for my prob. ) ???

Anyone can help ?

thx,
jens

Hi again ,

strange things going on there:

When i change the glClearColor to a Random Color the background will change randomly, but the Gears will not drawn …

Anyone know how to setup GL again ? It should be done in componentResize …but it do now work for me, maybe someone has an example …

Sorry for flooding the board,
Jens