我有一个简单的自定义画布
import com.sun.opengl.util.awt.TextRenderer;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import java.awt.*;
import static javax.media.opengl.GL.*;
import static javax.media.opengl.GL2.*;
public class MyCanvas extends GLCanvas implements GLEventListener {
private GLU glu;
public MyCanvas() {
this.addGLEventListener(this);
}
TextRenderer renderer;
@Override
public void init(GLAutoDrawable drawable) {
renderer = new TextRenderer(new Font("SansSerif", Font.BOLD, 36));
GL2 gl = drawable.getGL().getGL2();
glu = new GLU();
gl.glClearColor(1f, 1f, 1f, 1f);
gl.glClearDepth(1.0f);
gl.glEnable(GL_DEPTH_TEST);
gl.glDepthFunc(GL_LEQUAL);
gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
gl.glShadeModel(GL_SMOOTH);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2 gl = drawable.getGL().getGL2();
if (height == 0) height = 1;
float aspect = (float)width / height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0, aspect, 0.1, 100.0);
gl.glMatrixMode(GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -6.0f);
gl.glColor3f(0.5f, 0.5f, 0.5f);
gl.glBegin(GL_TRIANGLES);
gl.glVertex3f(0.0f, 1.0f, 0.0f);
gl.glVertex3f(-1.0f, -1.0f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, 0.0f);
gl.glEnd();
renderer.beginRendering(100, 100);
renderer.setColor(1.0f, 1f, 1f, 1f);
renderer.draw("Text to draw", 0, 0);
renderer.endRendering();
}
@Override
public void dispose(GLAutoDrawable drawable) { }
}
我有一个简单的JFrame来显示这个画布。
import com.jogamp.opengl.util.FPSAnimator;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class App extends JFrame {
private static final int CANVAS_WIDTH = 640;
private static final int CANVAS_HEIGHT = 480;
private static final int FPS = 60;
public App() {
GLCanvas canvas = new MyCanvas();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
final FPSAnimator animator = new FPSAnimator(canvas, FPS, true);
this.getContentPane().add(canvas);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
new Thread() {
@Override
public void run() {
if (animator.isStarted()) animator.stop();
System.exit(0);
}
}.start();
}
});
this.setTitle("JOGL 2.0 Setup (GLCanvas)");
this.pack();
this.setVisible(true);
animator.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(App::new);
}
}
当我运行这个程序时,有一个以下的例外:
Exception in thread "AWT-EventQueue-0-FPSAWTAnimator#00-Timer0" com.jogamp.opengl.util.AnimatorBase$UncaughtAnimatorException: java.lang.RuntimeException: javax.media.opengl.GLException: Caught NoSuchMethodError: javax.media.opengl.GL2.glBufferData(IILjava/nio/Buffer;I)V on thread AWT-EventQueue-0
at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:84)
at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:449)
at com.jogamp.opengl.util.FPSAnimator$MainTask.run(FPSAnimator.java:176)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Caused by: java.lang.RuntimeException: javax.media.opengl.GLException: Caught NoSuchMethodError: javax.media.opengl.GL2.glBufferData(IILjava/nio/Buffer;I)V on thread AWT-EventQueue-0
at com.jogamp.common.util.awt.AWTEDTExecutor.invoke(AWTEDTExecutor.java:58)
at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:103)
at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:201)
at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:202)
at javax.media.opengl.Threading.invoke(Threading.java:221)
at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:525)
at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:77)
... 4 more
Caused by: javax.media.opengl.GLException: Caught NoSuchMethodError: javax.media.opengl.GL2.glBufferData(IILjava/nio/Buffer;I)V on thread AWT-EventQueue-0
at javax.media.opengl.GLException.newGLException(GLException.java:75)
at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1311)
at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:1131)
at javax.media.opengl.awt.GLCanvas$11.run(GLCanvas.java:1394)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:301)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.NoSuchMethodError: javax.media.opengl.GL2.glBufferData(IILjava/nio/Buffer;I)V
at com.sun.opengl.util.awt.TextRenderer$Pipelined_QuadRenderer.<init>(TextRenderer.java:1725)
at com.sun.opengl.util.awt.TextRenderer$Glyph.draw3D(TextRenderer.java:1445)
at com.sun.opengl.util.awt.TextRenderer.internal_draw3D(TextRenderer.java:805)
at com.sun.opengl.util.awt.TextRenderer.draw3D(TextRenderer.java:519)
at com.sun.opengl.util.awt.TextRenderer.draw(TextRenderer.java:494)
at com.pms.efficiency.MyCanvas.display(MyCanvas.java:70)
at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:690)
at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:672)
at javax.media.opengl.awt.GLCanvas$10.run(GLCanvas.java:1383)
at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1277)
... 16 more
Exception in thread "AWT-EventQueue-0" javax.media.opengl.GLException: Caught NoSuchMethodError: javax.media.opengl.GL2.glBufferData(IILjava/nio/Buffer;I)V on thread AWT-EventQueue-0
at javax.media.opengl.GLException.newGLException(GLException.java:75)
at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1311)
at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:1131)
at javax.media.opengl.awt.GLCanvas$11.run(GLCanvas.java:1394)
at javax.media.opengl.Threading.invoke(Threading.java:223)
at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:525)
at javax.media.opengl.awt.GLCanvas.paint(GLCanvas.java:579)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264)
at sun.awt.RepaintArea.paint(RepaintArea.java:240)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:358)
at java.awt.Component.dispatchEventImpl(Component.java:4967)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.NoSuchMethodError: javax.media.opengl.GL2.glBufferData(IILjava/nio/Buffer;I)V
at com.sun.opengl.util.awt.TextRenderer$Pipelined_QuadRenderer.<init>(TextRenderer.java:1725)
at com.sun.opengl.util.awt.TextRenderer$Glyph.draw3D(TextRenderer.java:1445)
at com.sun.opengl.util.awt.TextRenderer.internal_draw3D(TextRenderer.java:805)
at com.sun.opengl.util.awt.TextRenderer.draw3D(TextRenderer.java:519)
at com.sun.opengl.util.awt.TextRenderer.draw(TextRenderer.java:494)
at com.pms.efficiency.MyCanvas.display(MyCanvas.java:70)
at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:690)
at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:672)
at javax.media.opengl.awt.GLCanvas$10.run(GLCanvas.java:1383)
at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1277)
... 28 more
TextRenderer中的问题(在Canvas display()方法中)。
renderer.beginRendering(100, 100);
renderer.setColor(1.0f, 1f, 1f, 1f);
renderer.draw("Text to draw", 0, 0);
renderer.endRendering();
没有这个代码可以:我在白色背景上看到黑色三角形。但我需要在GLCanvas上添加一些文字。