Hello everybody!
I try to explain my problem…
I am trying to use GLJpanel as canvas instead GLCanvas. So, on my top frame, i put my JPanel and enclosing canvas; JPanel contains some gradient as background and a text at front. So, if i want to see my JPanel, i need to set up canvas.setOpaque(false) for transparent view, and in init() metod set gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f), i guess… well, at finally i got an exception…
here is the code:
package book;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLJPanel;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.jogamp.opengl.util.FPSAnimator;
public class Simple2DText extends JFrame implements GLEventListener {
private GLU glu;
private static final double Z_DIST = 7.0; // for camera position
private static Font font;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable() {
public void run() {
Simple2DText frame = new Simple2DText();
frame.add(makeRenderPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
// clear color and depth buffers
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
glu.gluLookAt(0,0,Z_DIST, 0,0,0, 0,1,0); // position camera
}
@Override
public void dispose(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
}
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2(); // don't make this gl a global!
glu = new GLU();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
drawable.addGLEventListener(this);
FPSAnimator animator = new FPSAnimator(60, true);
animator.start();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2 gl = drawable.getGL().getGL2();
if (height == 0)height = 1; // to avoid division by 0 in aspect ratio below
gl.glViewport(x, y, width, height); // size of drawing area
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0, (float)width/(float)height, 1, 100);
// FOV, aspect ratio, near & far clipping planes
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
private static JPanel makeRenderPanel() {
// JPanel renderPane = new JPanel();
font = new Font("SansSerif", Font.BOLD, 48);
JPanel renderPane = new JPanel() {
public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D) g;
int width = getWidth();
int height = getHeight();
g2d.setPaint( new GradientPaint(0, 0, Color.YELLOW, width, height, Color.BLUE));
g2d.fillRect(0, 0, width, height);
g2d.setPaint(Color.BLACK);
g2d.setFont(font);
g2d.drawString("Hello World", width/4, height/4);
}
};
renderPane.setLayout(new BorderLayout());
renderPane.setOpaque(false);
renderPane.setPreferredSize(new Dimension(400, 300));
// GLCanvas canvas = new GLCanvas(); // the canvas
// create the GLJPanel
GLCapabilities caps = new GLCapabilities(GLProfile.getDefault());
caps.setAlphaBits(8);
GLJPanel canvas = new GLJPanel(caps);
canvas.setOpaque(false);
// the animator uses fixed rate scheduling
renderPane.add(canvas, BorderLayout.CENTER);
return renderPane;
}
}
here is stack trace:
Exception in thread "AWT-EventQueue-0" javax.media.opengl.GLException: Unable to set pixel format 40 for device
context 0xffffffff8f011b0c: error code 0 at com.jogamp.opengl.impl.windows.wgl.WindowsWGLGraphicsConfigurationFactory.
updateGraphicsConfigurationGDI(WindowsWGLGraphicsConfigurationFactory.java:449)
at com.jogamp.opengl.impl.windows.wgl.WindowsWGLGraphicsConfigurationFactory.
updateGraphicsConfiguration(WindowsWGLGraphicsConfigurationFactory.java:247)
at com.jogamp.opengl.impl.windows.wgl.WindowsWGLGraphicsConfigurationFactory.
updateGraphicsConfiguration(WindowsWGLGraphicsConfigurationFactory.java:186)
at com.jogamp.opengl.impl.windows.wgl.WindowsWGLGraphicsConfiguration.
updateGraphicsConfiguration(WindowsWGLGraphicsConfiguration.java:140)
at com.jogamp.opengl.impl.windows.wgl.WindowsBitmapWGLDrawable.
create(WindowsBitmapWGLDrawable.java:153)
at com.jogamp.opengl.impl.windows.wgl.WindowsBitmapWGLDrawable.
<init>(WindowsBitmapWGLDrawable.java:61)
at com.jogamp.opengl.impl.windows.wgl.WindowsWGLDrawableFactory.
createOffscreenDrawableImpl(WindowsWGLDrawableFactory.java:368)
at com.jogamp.opengl.impl.GLDrawableFactoryImpl.createGLDrawable(GLDrawableFactoryImpl.java:117)
at com.jogamp.opengl.impl.GLDrawableFactoryImpl.createOffscreenDrawable(GLDrawableFactoryImpl.java:213)
at javax.media.opengl.awt.GLJPanel$SoftwareBackend.initialize(GLJPanel.java:1018)
at javax.media.opengl.awt.GLJPanel.createAndInitializeBackend(GLJPanel.java:618)
at javax.media.opengl.awt.GLJPanel.access$2200(GLJPanel.java:126)
at javax.media.opengl.awt.GLJPanel$PbufferBackend.initialize(GLJPanel.java:1121)
at javax.media.opengl.awt.GLJPanel.createAndInitializeBackend(GLJPanel.java:618)
at javax.media.opengl.awt.GLJPanel.paintComponent(GLJPanel.java:368)
at javax.swing.JComponent.paint(JComponent.java:1029)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1780)
at java.awt.Window.paint(Window.java:3375)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" javax.media.opengl.GLException:
Unable to set pixel format 40 for device context 0xffffffffa7010104: error code 1
at com.jogamp.opengl.impl.windows.wgl.WindowsWGLGraphicsConfigurationFactory.
updateGraphicsConfigurationGDI(WindowsWGLGraphicsConfigurationFactory.java:449)
at com.jogamp.opengl.impl.windows.wgl.WindowsWGLGraphicsConfigurationFactory.
updateGraphicsConfiguration(WindowsWGLGraphicsConfigurationFactory.java:247)
at com.jogamp.opengl.impl.windows.wgl.WindowsWGLGraphicsConfigurationFactory.
updateGraphicsConfiguration(WindowsWGLGraphicsConfigurationFactory.java:186)
at com.jogamp.opengl.impl.windows.wgl.WindowsWGLGraphicsConfiguration.
updateGraphicsConfiguration(WindowsWGLGraphicsConfiguration.java:140)
at com.jogamp.opengl.impl.windows.wgl.WindowsBitmapWGLDrawable.create(WindowsBitmapWGLDrawable.java:153)
at com.jogamp.opengl.impl.windows.wgl.WindowsBitmapWGLDrawable.<init>(WindowsBitmapWGLDrawable.java:61)
at com.jogamp.opengl.impl.windows.wgl.WindowsWGLDrawableFactory.
createOffscreenDrawableImpl(WindowsWGLDrawableFactory.java:368)
at com.jogamp.opengl.impl.GLDrawableFactoryImpl.createGLDrawable(GLDrawableFactoryImpl.java:117)
at com.jogamp.opengl.impl.GLDrawableFactoryImpl.createOffscreenDrawable(GLDrawableFactoryImpl.java:213)
at javax.media.opengl.awt.GLJPanel$SoftwareBackend.initialize(GLJPanel.java:1018)
at javax.media.opengl.awt.GLJPanel.createAndInitializeBackend(GLJPanel.java:618)
at javax.media.opengl.awt.GLJPanel.paintComponent(GLJPanel.java:368)
at javax.swing.JComponent.paint(JComponent.java:1029)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
at javax.swing.JComponent.paintChildren(JComponent.java:862)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1780)
at java.awt.Window.paint(Window.java:3375)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
any ideas where i was wrong? thanks.