jogl problem1
Posted by
dim_9999 on
Feb 10, 2011; 12:29pm
URL: https://forum.jogamp.org/jogl-problem1-tp2465712.html
Hi! well... lets go! :) Here is the code:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.media.nativewindow.NativeSurface;
import javax.media.nativewindow.NativeWindowFactory;
import javax.media.nativewindow.awt.AWTGraphicsConfiguration;
import javax.media.opengl.GL2;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLDrawable;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLProfile;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ActiveRenderingCubeGL1 extends JFrame implements WindowListener {
private CubeCanvasGL1 canvas;
public ActiveRenderingCubeGL1(long period) {
setLayout(new BorderLayout());
setResizable(false);
add(makeRenderPanel(period), BorderLayout.CENTER);
addWindowListener(this);
pack();
setVisible(true);
}
private JPanel makeRenderPanel(long period) {
JPanel renderPane = new JPanel();
renderPane.setLayout(new BorderLayout());
renderPane.setOpaque(false);
GLProfile profile = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(profile);
AWTGraphicsConfiguration config = AWTGraphicsConfiguration.create(this, caps, caps);
NativeSurface surf = NativeWindowFactory.getNativeWindow(this, config);
canvas = new CubeCanvasGL1(300, 300, period, config, caps, surf);
canvas.setSize(new Dimension(300, 300));
canvas.setFocusable(true);
canvas.requestFocus(); // canvas has focus, so receives key events
renderPane.setSize(canvas.getPreferredSize());
renderPane.add(canvas, BorderLayout.CENTER);
return renderPane;
}
public static void main(String[] args) {
int fps = 60;
long period = (long) 1000.0 / fps;
new ActiveRenderingCubeGL(period * 1000000L); // ms --> nanosecs
}
}
class CubeCanvasGL1 extends Canvas implements Runnable {
private volatile boolean isRunning = false;
private Thread animator;
private GLDrawable drawable; // the rendering 'surface'
private GLContext context; // the rendering context (holds rendering state
// info)
private GL2 gl;
private GLU glu;
private int cubeDList;
private int panelWidth, panelHeight;
private long period;
public CubeCanvasGL1(int pwidth, int pheight, long period,
AWTGraphicsConfiguration config, GLCapabilities caps,
NativeSurface surf) {
panelWidth = pwidth;
panelHeight = pheight;
this.period = period;
drawable = GLDrawableFactory.getFactory(caps.getGLProfile()).createGLDrawable(surf);
context = drawable.createContext(null);
}
@Override
public void run() {
initRender();
renderLoop();
context.destroy();
System.exit(0);
}
private void initRender() {
// TODO Auto-generated method stub
}
private void renderLoop() {
while (isRunning) {
makeContentCurrent();
renderScene(); // rendering
}
}
private void renderScene() {
// TODO Auto-generated method stub
}
private void makeContentCurrent() {
try {
while (context.makeCurrent() == GLContext.CONTEXT_NOT_CURRENT) {
System.out.println("Context not yet current...");
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void addNotify() {
// TODO Auto-generated method stub
super.addNotify();
drawable.setRealized(true); // canvas can now be used for rendering
// initialize and start the animation thread
if (animator == null || !isRunning) {
animator = new Thread(this);
animator.start();
}
}
}
here is the stack trace:
Exception in thread
"main" java.
lang.
ClassCastException: javax.
media.
nativewindow.
awt.
AWTGraphicsConfiguration cannot be cast to com.
jogamp.
opengl.
impl.
windows.
wgl.
WindowsWGLGraphicsConfiguration
at com.jogamp.opengl.impl.windows.wgl.WindowsWGLDrawable.setRealizedImpl(WindowsWGLDrawable.java:70)
at com.jogamp.opengl.impl.GLDrawableImpl.setRealized(GLDrawableImpl.java:155)
at book.CubeCanvasGL.addNotify(ActiveRenderingCubeGL.java:325)
at book.ActiveRenderingCubeGL.<init>(ActiveRenderingCubeGL.java:33)
at book.ActiveRenderingCubeGL1.main(ActiveRenderingCubeGL1.java:60)
I can't realize how to make that cast correctly or something else...? any suggestions? thanks.