0
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

}
}

I can't seem to register the canvas for the KeyListener. I get (The method addKeyListener(KeyListener) in the type component is not applicable for the arguments(Activity01)). Am I doing any of this right?

3
  • Duplicate question: forum.jogamp.org/…
    – gouessej
    Nov 15, 2015 at 17:26
  • 1
    The code above uses an obsolete version of JOGL, please don't use it.
    – gouessej
    Nov 15, 2015 at 17:29
  • @gouessej I asked both question.
    – Django
    Nov 16, 2015 at 6:20

1 Answer 1

2

You are implementing the wrong KeyListener, there is another in

java.awt.event.KeyListener
2
  • Thanks a lot! It got me so confused!
    – Django
    Nov 14, 2015 at 20:20
  • This is exactly what I explained to him: forum.jogamp.org/…
    – gouessej
    Nov 15, 2015 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.