如何在 JOGL 中接受用户键盘输入


package graphics_activity01;
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 javax.swing.JFrame;
import com.jogamp.newt.event.KeyEvent;
import com.jogamp.newt.event.KeyListener;
import com.sun.opengl.util.Animator;
public class Activity01 extends JFrame implements GLEventListener, KeyListener   {
GLCanvas canvas;
Animator an;
public Activity01() {
    super("KeyListener Activity");
    canvas = new GLCanvas();
    an = new Animator(canvas);
    add(canvas);
    canvas.addGLEventListener(this);
    canvas.setFocusable(true);
    canvas.addKeyListener(this);
    setSize(800, 500);
    setVisible(true);
    setLocationRelativeTo(null);
    an.start();
    canvas.requestFocus();
}
public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    GLU glu = new GLU();
    gl.glClearColor(0f, 0f, 0f, 0.0f);
    gl.glMatrixMode(GL2.GL_PROJECTION);
    glu.gluOrtho2D(-400, 400, -250, 250);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
}
boolean firstRun = true;
// This is not important currently. 
public void display(GLAutoDrawable drawable) {
    /**
    GL2 gl = drawable.getGL().getGL2();
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
    float[] color = {0.5f,0.0f,0.0f};
    gl.glColor3fv(color ,0);
    gl.glPointSize(50);
    gl.glBegin(GL2.GL_POINTS);
        gl.glVertex2i(100, 100);
        gl.glVertex2i(-100, 100);
        gl.glVertex2i(100, -100);
        gl.glVertex2i(-100, -100);
        gl.glVertex2i(0,0);
    gl.glEnd();
    **/
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
}
public void dispose(GLAutoDrawable drawable) {
}
public static void main(String[] ar) {
    new Activity01();
}
// I want to test this!
@Override
public void keyPressed(KeyEvent e) {
    char pressed = e.getKeyChar();
    if (pressed == 'a') {
        System.out.println('a');
    }
}
@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub
}
}

我似乎无法为密钥侦听器注册画布。我得到(类型组件中的方法addKeyListener(KeyListener(不适用于参数(Activity01((。我做这些对吗?

你实现了错误的KeyListener,还有另一个

java.awt.event.KeyListener

相关内容

  • 没有找到相关文章

最新更新