/* * $RCSfile$ * * Copyright (c) 2006 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision$ * $Date$ * $State$ */ import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.GraphicsConfiguration; import javax.media.opengl.*; import javax.media.opengl.glu.*; import javax.swing.JPopupMenu; public class PbufferDestroyBug extends javax.swing.JFrame { private MyCanvas canvas; /** * Creates new form PbufferDestroyBug */ public PbufferDestroyBug() { // Initialize the GUI components JPopupMenu.setDefaultLightWeightPopupEnabled(false); initComponents(); // Create canvas (and GL drawable) and add canvas to the drawing panel GLCapabilities capabilities = new GLCapabilities(); canvas = new MyCanvas(capabilities); drawingPanel.add(canvas, java.awt.BorderLayout.CENTER); } private static GraphicsConfiguration unwrap(AWTGraphicsConfiguration config) { if (config == null) { return null; } return config.getGraphicsConfiguration(); } class MyCanvas extends Canvas { private GL gl; private GLU glu; private GLContext context; private GLDrawable drawable; private GLPbuffer pbuffer; private GLContext pcontext; MyCanvas(GLCapabilities capabilities) { super(unwrap((AWTGraphicsConfiguration)GLDrawableFactory.getFactory(). chooseGraphicsConfiguration(capabilities, null, null))); drawable = GLDrawableFactory.getFactory().getGLDrawable(this, capabilities, null); context = drawable.createContext(null); GLCapabilities pcaps = new GLCapabilities(); pcaps.setDoubleBuffered(false); pbuffer = GLDrawableFactory.getFactory().createGLPbuffer(capabilities, null, 200, 200, null); } GLDrawable getDrawable() { return drawable; } public void display() { System.err.println("display()"); gl.glClearColor(0.1f, 0.1f, 0.4f, 0.0f); gl.glClear(GL.GL_COLOR_BUFFER_BIT); drawable.swapBuffers(); } private void initialize() { System.err.println("initialize()"); gl = context.getGL(); glu = new GLU(); } @Override public void addNotify() { super.addNotify(); drawable.setRealized(true); } @Override public void removeNotify() { context.destroy(); drawable.setRealized(false); } void createPbuffer() { ; } void destroyPbuffer() { destroyPbufferButton.setEnabled(false); // Create a new context on the Pbuffer and make it current pcontext = pbuffer.createContext(null); int status = pcontext.makeCurrent(); if (status == GLContext.CONTEXT_NOT_CURRENT) { throw new RuntimeException("Could not make pcontext current"); } // Release pcontext before destroying Pbuffer. Note that without // doing this, JOGL will crash if (releasePcontextCheckBox.isSelected()) { pcontext.release(); } // Destroy the pbuffer context and force a repaint pbuffer.destroy(); repaint(); } @Override public void paint(Graphics g) { int status = context.makeCurrent(); switch (status) { case GLContext.CONTEXT_CURRENT_NEW: initialize(); break; case GLContext.CONTEXT_NOT_CURRENT: throw new RuntimeException("Could not make context current"); } assert gl != null; display(); context.release(); } } // ---------------------------------------------------------------- /** 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. */ // //GEN-BEGIN:initComponents private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; guiPanel = new javax.swing.JPanel(); destroyPbufferButton = new javax.swing.JButton(); releasePcontextCheckBox = new javax.swing.JCheckBox(); drawingPanel = new javax.swing.JPanel(); jMenuBar1 = new javax.swing.JMenuBar(); fileMenu = new javax.swing.JMenu(); exitMenuItem = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Pbuffer Destroy Bug"); guiPanel.setLayout(new java.awt.GridBagLayout()); destroyPbufferButton.setText("Destroy Pbuffer"); destroyPbufferButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { destroyPbufferButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); guiPanel.add(destroyPbufferButton, gridBagConstraints); releasePcontextCheckBox.setText("Release pcontext"); releasePcontextCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); releasePcontextCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); guiPanel.add(releasePcontextCheckBox, gridBagConstraints); getContentPane().add(guiPanel, java.awt.BorderLayout.NORTH); drawingPanel.setLayout(new java.awt.BorderLayout()); drawingPanel.setPreferredSize(new java.awt.Dimension(500, 500)); getContentPane().add(drawingPanel, java.awt.BorderLayout.CENTER); fileMenu.setText("File"); exitMenuItem.setText("Exit"); exitMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitMenuItemActionPerformed(evt); } }); fileMenu.add(exitMenuItem); jMenuBar1.add(fileMenu); setJMenuBar(jMenuBar1); pack(); }// //GEN-END:initComponents private void destroyPbufferButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_destroyPbufferButtonActionPerformed canvas.destroyPbuffer(); }//GEN-LAST:event_destroyPbufferButtonActionPerformed private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed System.exit(0); }//GEN-LAST:event_exitMenuItemActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new PbufferDestroyBug().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton destroyPbufferButton; private javax.swing.JPanel drawingPanel; private javax.swing.JMenuItem exitMenuItem; private javax.swing.JMenu fileMenu; private javax.swing.JPanel guiPanel; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JCheckBox releasePcontextCheckBox; // End of variables declaration//GEN-END:variables }