如何获得KeyListener以使其与JOGL一起正常使用?

时间:2019-05-02 12:51:24

标签: java opengl keylistener jogl

我正在尝试使用KeyListener来更改 rtri 的值,从而更改动画的工作方式。我的代码有点遍了,所以提前对不起!

我尝试在setup()内创建KeyListener的实例,并使用.addKeyListener()将其添加到画布。

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.FPSAnimator;

public class Frame implements GLEventListener, KeyListener {

    public final double pi2 = (2 * Math.PI);
    private GLU glu = new GLU();
    private float rtri = 0.0f;
    private static ArrayList<Star> stars = new ArrayList<Star>();
    private static float zFloat = 1.0f;

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyChar() == KeyEvent.VK_UP) {
            rtri += 0.5f;
            System.out.println(rtri);
            System.out.println("Increase");
        }
        else if (e.getKeyChar() == KeyEvent.VK_DOWN) {
            rtri -= 0.5f;
            System.out.println(rtri);
            System.out.println("Decrease");
        }
    }


    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void keyTyped(KeyEvent e) {  
    }

    @Override
    public void display(GLAutoDrawable drawable) {
            final GL2 gl = drawable.getGL().getGL2();

        gl.glClear( GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );
        gl.glLoadIdentity(); // Reset The View
        gl.glTranslatef( 0.0f, 0.0f, -2.0f ); // Move the plane of stars

        gl.glRotatef( rtri, 0.0f, -2.0f, zFloat ); // Rotate the plane

        for (int sNum = 0; sNum < stars.size(); sNum++) {
            drawStar(stars.get(sNum), gl);
            }

        gl.glFlush();
            rtri+= 5.0f;

    }

    @Override
    public void dispose(GLAutoDrawable drawable) {

    }

    @Override
    public void init(GLAutoDrawable drawable) {
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, 
        int height) {
        final GL2 gl = drawable.getGL().getGL2(); 
           // get the OpenGL 2 graphics object  
        if(height <=0)
        height =1;
           //preventing divided by 0 exception height =1;
        final float h = (float) width / (float) height;
           // display area to cover the entire window
        gl.glViewport(0, 0, width, height);
           //Transforms projection matrix
            gl.glMatrixMode(GL2.GL_PROJECTION);
            gl.glLoadIdentity();
            glu.gluPerspective( 45.0f, h, 1.0, 20.0 );
           //transforming model view gl.glLoadIdentity();
            gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void setup() {
        final GLProfile profile = GLProfile.get(GLProfile.GL2);
        GLCapabilities capabilities = new GLCapabilities(profile);


        final GLCanvas canvas = new GLCanvas(capabilities);
        Frame bFrame = new Frame();

        canvas.addGLEventListener(bFrame);
        canvas.addKeyListener(this);
        canvas.setSize(900, 900);

        final JFrame frame = new JFrame("Test Window");

        frame.add(canvas);
        frame.setSize(900, 900);
        frame.setVisible(true);

        final FPSAnimator animator = new FPSAnimator(canvas, 60,true);
        animator.start();
    }

    public static void main(String[] args) {
        new Frame().setup();

    }

}

在我的keyPressed中,进行了设置,以便如果按向上箭头,它将增加rtri,输出其值,并打印单词“ Increase”。当我运行程序时,动画效果很好,但是按任意一个键都不会影响我的程序,也不会在控制台中打印所需的项目。

0 个答案:

没有答案