我不能让opengl在java eclipse中工作

时间:2016-05-30 15:50:40

标签: java eclipse opengl

我试图在java中用OpenGL制作一个Hello World。 但我总是得到这个错误:

  

线程中的异常" main" java.lang.NoSuchMethodError:com.jogamp.common.jvm.JNILibLoaderBase.addNativeJarLibs(Ljava / lang / Class; Ljava / lang / String; [Ljava / lang / String;)Z       在javax.media.opengl.GLProfile $ 1.run(GLProfile.java:131)       at java.security.AccessController.doPrivileged(Native Method)       在javax.media.opengl.GLProfile.initSingleton(GLProfile.java:122)       在javax.media.opengl.GLProfile.initSingleton(GLProfile.java:151)       在javax.media.opengl.GLProfile.getProfileMap(GLProfile.java:1688)       在javax.media.opengl.GLProfile.get(GLProfile.java:652)       在javax.media.opengl.GLProfile.get(GLProfile.java:668)       在JOGL.main(JOGL.java:13)

这是Renderer.java的代码:

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

class Renderer implements GLEventListener 
{
    private GLU glu = new GLU();

    public void display(GLAutoDrawable gLDrawable) 
    {
        final GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glTranslatef(-1.5f, 0.0f, -6.0f);
        gl.glBegin(GL2.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();             
        gl.glTranslatef(3.0f, 0.0f, 0.0f);
        gl.glBegin(GL2.GL_QUADS);               
        gl.glVertex3f(-1.0f, 1.0f, 0.0f);   
        gl.glVertex3f(1.0f, 1.0f, 0.0f);    
        gl.glVertex3f(1.0f, -1.0f, 0.0f);   
        gl.glVertex3f(-1.0f, -1.0f, 0.0f);  
        gl.glEnd();             
        gl.glFlush();
    }


    public void displayChanged(GLAutoDrawable gLDrawable, boolean  modeChanged, boolean deviceChanged) 
    {
    System.out.println("displayChanged called");
    }

    public void init(GLAutoDrawable gLDrawable) 
    {
        System.out.println("init() called");
        GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL2.GL_FLAT);
    }

    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) 
    {
        System.out.println("reshape() called: x = "+x+", y = "+y+", width = "+width+", height = "+height);
        final GL2 gl = gLDrawable.getGL().getGL2();

        if (height <= 0) // avoid a divide by zero error!
        {
        height = 1;
        }

        final float h = (float) width / (float) height;

        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, h, 1.0, 20.0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }


     public void dispose(GLAutoDrawable arg0) 
    {
    System.out.println("dispose() called");
    }

这是JOGL.java的代码:

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame; 
public class JOGL 
{
    public static void main(String[] args) 
    {
        // setup OpenGL Version 2
        GLProfile profile = GLProfile.get(GLProfile.GL3);
        GLCapabilities capabilities = new GLCapabilities(profile);

        // The canvas is the widget that's drawn in the JFrame
        GLCanvas glcanvas = new GLCanvas(capabilities);
        glcanvas.addGLEventListener(new Renderer());
        glcanvas.setSize( 300, 300 );

        JFrame frame = new JFrame( "Hello World" );
        frame.getContentPane().add( glcanvas);

        // shutdown the program on windows close event
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent ev) {
                System.exit(0);
            }
        });

        frame.setSize( frame.getContentPane().getPreferredSize() );
        frame.setVisible( true );
    }
}

如果你知道如何在eclipse中为java设置opengl的好教程 或者解决此错误的方法请告诉我。

0 个答案:

没有答案