How to accept user keyboard input?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to accept user keyboard input?

k0sar
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;

        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();
        }

        @Override
        public void keyPressed(KeyEvent e) {
                char pressed = e.getKeyChar();
                if (pressed == 'a') {
                        System.out.println('a');
                }
        }

        @Override
        public void keyReleased(KeyEvent arg0) {
               
        }
}


This is my code but it gives me an error when I try to register the canvas for the KeyListener. Is this the right way?
Reply | Threaded
Open this post in threaded view
|

Re: How to accept user keyboard input?

gouessej
Administrator
Hi

Sorry, you mix some code coming from different versions of JOGL, "com.sun.opengl.util.Animator" doesn't exist in JOGL 2 but com.jogamp.opengl.util.Animator does ;)
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: How to accept user keyboard input?

gouessej
Administrator
In reply to this post by k0sar
Moreover, you have to use an AWT key listener with an AWT/Swing component, you can't use a NEWT key listener with an AWT/Swing component without an adapter. Please look at our unit tests and our examples in the wiki.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: How to accept user keyboard input?

k0sar
In reply to this post by gouessej
Hey!

I got the code template from my teacher and I add a few libraries (to configure eclipse and I got a rar from him containing an older version of JOGL) myself in order to get it working (I was definitely confused about which library to import so I added all the files into a user library and added it to my project) because I use Eclipse and he uses Textpad.
Reply | Threaded
Open this post in threaded view
|

Re: How to accept user keyboard input?

gouessej
Administrator
Your teacher should simply use the very latest version of JOGL and point you to our detailed tutorial for the IDEs including Eclipse:
http://jogamp.org/wiki/index.php/Setting_up_a_JogAmp_project_in_your_favorite_IDE
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: How to accept user keyboard input?

k0sar
Yes he should. Thank you for your help.

On Sun, Nov 15, 2015 at 8:28 PM gouessej [via jogamp] <[hidden email]> wrote:
Your teacher should simply use the very latest version of JOGL and point you to our detailed tutorial for the IDEs including Eclipse:
http://jogamp.org/wiki/index.php/Setting_up_a_JogAmp_project_in_your_favorite_IDE

If you reply to this email, your message will be added to the discussion below:
To unsubscribe from How to accept user keyboard input?, click here.
NAML
--
Kosar F.