addKeyListener cannot be added to GLAutoDrawable

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

addKeyListener cannot be added to GLAutoDrawable

Mohara
Maze.java
MoveControl.java
Renderer.java
I can`t seem to be able to add a KeyListener to GLAutoDrawable. I read the this tutorial:
https://docs.google.com/file/d/0B9hhZie2D-fENGE3ZmZkOGItZGYzZC00ZDgzLTg0NTAtMTk1MTAwMzYxYzNk/edit?hl=en&pli=1
Also read the javadocs and it seems that GLAutoDrawable is indeed a component and should be able to receive a KeyListener.

I needed the GLAutoDrawable to receive key inputs so I can move things in my screen.
I am new here and with JOGL. Just learning everything as university homework and so, I would appreciate some help.

code:

package f091183.mohara.jogl.maze;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
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;
import com.jogamp.opengl.util.*;
import f091183.mohara.jogl.maze.Renderer;

public class Maze{

        public static void main(String[] args)
    {
               
    // Setup OpenGL Default Version
    GLProfile profile = GLProfile.getDefault();
    GLCapabilities capabilities = new GLCapabilities(profile);
   
    // Setting canvas for the JFrame
    GLCanvas glcanvas = new GLCanvas();//capabilities);
    glcanvas.addGLEventListener(new Renderer());
    glcanvas.setSize( 800,600);// (1600, 900 );
   
    // Setting frame
        JFrame frame = new JFrame( "Labirinth" );
        frame.setSize( 800,600);// (1600, 900 );
        frame.add(glcanvas);
        //frame.setUndecorated(true); // hide window border
        frame.setVisible(true);
       
        // Close the program on windows close event
        frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent ev)
            {
                System.exit(0);
            }
        });
       
        // Frame key listener
        frame.addKeyListener(new KeyAdapter()
        {

        public void keyPressed(KeyEvent e)
        {
        // Close on ESC key press
        if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
        {
        new Thread(new Runnable()
        {
        public void run()
        {
        System.exit(0);
        }
        }).start();

        }
       
        if(e.getKeyCode()==KeyEvent.VK_S)
        {
        new Thread(new Runnable()
        {
        public void run()
        {
       
        double move=0.00833333333;
        //player.setPlayerY(Math.round( (player.getPlayerY()-move) * 10000.0 ) / 10000.0);
        }
        }).start();
       
        }
        }
                });
       
        // Animator to make game update loops
        FPSAnimator animator = new FPSAnimator(glcanvas, 1);
        animator.add(glcanvas);
        animator.start();
    }
}

// Maze class >> as in tutorials, it should process the data

package f091183.mohara.jogl.maze;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt;
 
class Renderer implements GLEventListener, KeyListener
{
        static final public char[] maze = {'w','w','w','w','w', 'w','w','w','w','w', 'w','w','w','w','w', 'w','w','w','w','w',
                                                                           'w',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ',' ',' ',' ','w', ' ',' ',' ',' ','S',
                                                                           'w',' ','w','w','w', 'w','w','w','w',' ', 'w',' ','w',' ',' ', ' ','w',' ','w','w',
                                                                           'w',' ',' ','w',' ', ' ',' ',' ','w',' ', ' ',' ','w',' ','w', ' ','w','w','w','w',
                                                                           'w',' ','w','w','w', 'w',' ','w','w','w', 'w','w','w','w','w', 'w','w',' ',' ','w',  
                                                 /*ln 5*/   'w',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ','w','w',' ','w',
                                                                           'w','w','w','w','w', 'w','w','w','w','w', 'w','w','w','w','w', ' ','w',' ',' ','w',
                                                                           'w',' ',' ',' ',' ', ' ',' ',' ',' ',' ', ' ',' ',' ',' ','w', ' ','w','w',' ','w',
                                                                           'w',' ','w',' ','w', 'w','w','w','w','w', 'w','w','w',' ',' ', ' ',' ','w',' ','w',
                                                                           'w',' ','w',' ',' ', ' ',' ',' ',' ',' ', ' ',' ','w','w','w', 'w','w','w',' ','w',
                                                        /*ln 10*/  'w',' ','w',' ','w', 'w','w','w','w','w', 'w',' ','w',' ',' ', ' ',' ',' ',' ','w',
                                                                           'w',' ','w','w','w', ' ',' ',' ',' ',' ', 'w',' ','w',' ','w', 'w','w','w','w','w',
                                                                           'w',' ',' ',' ',' ', ' ','w','w','w',' ', 'w',' ','w',' ',' ', ' ',' ',' ',' ','w',
                                                                           'w','w','w','w','w', 'w','w',' ','w',' ', 'w',' ','w','w','w', ' ','w','w',' ','w',
                                                                           'w',' ',' ',' ',' ', ' ',' ',' ','w',' ', 'w',' ','w',' ',' ', ' ','w',' ',' ','w',
                                              /*ln 15*/    'w',' ','w','w',' ', 'w','w','w','w',' ', 'w',' ','w',' ','w', 'w','w','w','w','w',
                                                                           'w',' ',' ','w',' ', 'w',' ',' ',' ',' ', 'w',' ','w',' ',' ', ' ','w',' ',' ','w',
                                                                           'w','w',' ','w',' ', 'w','w','w','w',' ', 'w',' ','w','w','w', ' ','w','w',' ','w',
                                                                           'w',' ',' ','w',' ', ' ',' ',' ',' ',' ', 'w',' ',' ',' ',' ', ' ',' ',' ',' ','w',
                                                                           'w','E','w','w','w', 'w','w','w','w','w', 'w','w','w','w','w', 'w','w','w','w','w',
                                                                                }; //      y 5  y 10 y 15
        public static MoveControl player = new MoveControl();
   
        // Canvas initialization
        public void init(GLAutoDrawable gLDrawable)
    {  
    System.out.println("init() called");
        GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
       
    }
   
    // Canvas action
    public void display(GLAutoDrawable gLDrawable)
    {
    update(gLDrawable);
        render(gLDrawable);
    }
   
    // Reads the changes in the screen
    private void update(GLAutoDrawable gLDrawable)
    {
    double move = 0.00833333333;
    int mazeXY=0;
  /*
    player.setPlayerY(Math.round( (player.getPlayerY()-move) * 10000.0 ) / 10000.0);
     */
    System.out.println("pY: " + player.getPlayerY());
    // Position of player in maze. Checking for wall after >>move Y-<<
    for(double counterX=0.9; counterX >=-1; counterX=counterX-0.1)
    {
     
    for(double counterY=0.9; counterY >=-1; counterY=counterY-0.1, mazeXY++)
    {
      double XP = Math.round(player.getPlayerX()* 100.0 ) / 100.0;
      double XC = Math.round(counterX*100.0) / 100.0;
      double YP = Math.round(player.getPlayerY()* 100.0 ) / 100.0;
      double YC = Math.round(counterY*100.0) / 100.0;
      if( (XP == XC) && (YP == YC))
      {
      if(maze[mazeXY+20]== 'w')
      {
      player.setPlayerY(Math.round( (player.getPlayerY()+move) * 10000.0 ) / 10000.0);
      }
     
      }
 
    }
    }
    }

        // Rends the canvas
    private void render(GLAutoDrawable gLDrawable)
    {
        GL2 gl = gLDrawable.getGL().getGL2();
        int mazeXY=0;
       
        // CLear screen
        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();

        // Draw the maze with walls
        for(double counterX=0.9; counterX >=-1; counterX=counterX-0.1)
        {
       
        for(double counterY=0.9; counterY >=-1; counterY=counterY-0.1, mazeXY++)
            {
        // Check if position is a wall
        if(maze[mazeXY]=='w')
        {
                //The wall
                    gl.glTranslated(0.05, 0.05, 0);
                gl.glBegin(GL2.GL_QUADS);
                    gl.glColor3f(1, 0, 0);
                    gl.glVertex2d(counterX-0.05, counterY-0.05); // [ - ; - ]
                gl.glColor3f(0, 0, 1);
                gl.glVertex2d(counterX+0.05, counterY-0.05);   // [ + ; - ]
                                gl.glColor3f(1, 1, 0);
                                gl.glVertex2d(counterX+0.05, counterY+0.05);   // [ + ; + ]
                                gl.glColor3f(0, 1, 0);
                                gl.glVertex2d(counterX-0.05, counterY+0.05);   // [ - ; + ]
        }
            }      
        }
       
      //The wall
    gl.glTranslated(0.05, 0.05, 0);
                gl.glBegin(GL2.GL_QUADS);
    gl.glColor3f(1, 1, 1);
    gl.glVertex2d(player.getPlayerX()-0.033, player.getPlayerY()-0.033); // [ - ; - ]
                gl.glVertex2d(player.getPlayerX()+0.033, player.getPlayerY()-0.033);   // [ + ; - ]
        gl.glVertex2d(player.getPlayerX()+0.033, player.getPlayerY()+0.033);   // [ + ; + ]
        gl.glVertex2d(player.getPlayerX()-0.033, player.getPlayerY()+0.033);   // [ - ; + ]
       
        gl.glFlush();
        gl.glEnd();
    }
 
 
    public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged)
    {}
 
    // Reshape canvas
    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();
        gl.glPerspective(45.0f, h, 1.0, 20.0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();*/
    }
 
        public void dispose(GLAutoDrawable arg0)
        {}

        @Override
        public void keyTyped(KeyEvent paramKeyEvent)
        { }

        @Override
        public void keyPressed(KeyEvent e)
        {
               
    if(e.getKeyCode()==KeyEvent.VK_S)
    {
    double move=0.00833333333;
   
                                player.setPlayerY(Math.round( (player.getPlayerY()-move) * 10000.0 ) / 10000.0);
    }
               
        }

        @Override
        public void keyReleased(KeyEvent paramKeyEvent) {
                // TODO Auto-generated method stub
               
        }

}

// Class to that works with the position of the "player"
public class MoveControl
{
        // Initial player position
        private double playerX=-0.9;
        private double playerY=0.8;
       
        // Return the playerX
        public double getPlayerX() {
                return playerX;
        }

        // Set playerX
        public void setPlayerX(double playerX) {
                this.playerX = playerX;
        }

    // Return the playerY
        public double getPlayerY() {
                return playerY;
        }
       
        // Set playerY
        public void setPlayerY(double playerY) {
                this.playerY = playerY;
        }
       
}
Reply | Threaded
Open this post in threaded view
|

Re: addKeyListener cannot be added to GLAutoDrawable

gouessej
Administrator
Hi

GLAutoDrawable is an interface used by JOGL, its implementations are not forced by any means to override java.awt.Component or javax.swing.JComponent.

You can put the key listeners directly on your GLCanvas, what's wrong with that?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: addKeyListener cannot be added to GLAutoDrawable

Mohara
Weirdly enough, I couldn`t get the program to answer the keys if I added the key listeners to the GLCanvas. Even more surprising is the fact that I added it to the Jframe. I managed somehow. Thank you.

I am still curious about how the tutorial has GLAutoDrawable.addKeyListener() , while I couldn`t make it work. Since I am learning I wanted to know the right way. Adding to the GLCanvas or even JFrame destroys the hierarchy I planned, so I rather add it somewhere in the Renderer.class and the only Component (at least following the Javadocs) should be GLAutoDrawable.