为什么我的听众不.听



我是Java和OpenGL新手。

我有一个监听器,我想弄清楚为什么它不工作?当我按下一个键时,绝对没有任何反应。我所指的监听器是:

@Override
public void keyTyped(KeyEvent e) 
{
    // TODO Auto-generated method stub
    char key= e.getKeyChar();
    System.out.printf("Key typed: %cn", key); 
}

这是我的文件。

HelloOpenGL.java

package helloOpenGL;
/*************************************************************************************
*   IMPORTS     
**************************************************************************************/
import java.awt.BorderLayout;
import java.awt.Frame;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.Animator;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/*************************************************************************************
*   Class HelloOpenGL   
**************************************************************************************/
public class HelloOpenGL extends Frame 
{
    // Animator? Unneeded??
    static Animator anim = null;

    /*************************************************************************************
    *   setupJOGL
    *
    *   Method that sets up Java openGL??
    **************************************************************************************/
    private void setupJOGL()
    {
        GLCapabilities caps = new GLCapabilities(null);
        caps.setDoubleBuffered(true);
        caps.setHardwareAccelerated(true);
        GLCanvas canvas = new GLCanvas(caps); 
        add(canvas);
        JoglEventListener jgl = new JoglEventListener();
        canvas.addGLEventListener(jgl); 
        canvas.addKeyListener(jgl); 


        //anim = new Animator(canvas);
        //anim.start();
    }
    /*************************************************************************************
    *   HelloOpenGL Constructor ?   
    **************************************************************************************/
    public HelloOpenGL() 
    {
        super("test");
        setLayout(new BorderLayout());

        // To close window
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        setSize(600, 600);
        setLocation(40, 40);
        setVisible(true);
        // Call setupJOGL
        setupJOGL();
    }
    public static void main(String[] args) 
    {
        // Instantiate object
        HelloOpenGL demo = new HelloOpenGL();
        // Show it?
        demo.setVisible(true);
    }
}

class MyWin extends WindowAdapter 
{
   public void windowClosing(WindowEvent e)
   {
       System.exit(0);
   }
}

JoglEventListener.java

package helloOpenGL;
/*************************************************************************************
*   IMPORTS     
**************************************************************************************/
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import com.jogamp.opengl.*;
import com.jogamp.opengl.glu.GLU;
/*************************************************************************************
*   JoglEventListener   
**************************************************************************************/
public class JoglEventListener implements GLEventListener, KeyListener 
{
    float[] vertices={5.97994f, -0.085086f, -0.010798f, 
            5.97994f, 10.0043f, -0.010798f, 
            7.99077f, 10.0043f, -0.010798f, 
            7.99077f, 11.3449f, -0.010798f, 
            -0.405339f, 11.3449f, -0.010798f, 
            -0.405339f, 9.98083f, -0.010798f, 
            1.65252f, 9.98083f, -0.010798f, 
            1.65252f, 0.549879f, -0.010798f, 
            -0.722839f, 0.549879f, -0.010798f, 
            -0.722839f, -1.69612f, -0.010798f, 
            2.6168f, -1.69612f, -0.010798f, 
            -7.24925f, 0.42055f, -0.010798f, 
            -9.35415f, 0.42055f, -0.010798f, 
            -9.35415f, 10.0043f, -0.010798f, 
            -7.37859f, 10.0043f, -0.010798f, 
            -7.37859f, 11.3802f, -0.010798f, 
            -15.8217f, 11.3802f, -0.010798f, 
            -15.8217f, 9.99258f, -0.010798f, 
            -13.8109f, 9.99258f, -0.010798f, 
            -13.8109f, -0.061591f, -0.010798f, 
            -10.2361f, -1.73139f, -0.010798f, 
            -7.26099f, -1.73139f, -0.010798f, 
            -6.1909f, 0.855631f, -0.010798f, 
            -8.11942f, 0.855631f, -0.010798f, 
            -8.11942f, 2.31379f, -0.010798f, 
            0.217914f, 2.31379f, -0.010798f, 
            0.217914f, 0.926204f, -0.010798f, 
            -1.73415f, 0.926204f, -0.010798f, 
            -1.73415f, -4.10675f, -0.010798f, 
            9.23724f, 0.937952f, -0.010798f, 
            7.26169f, 0.937952f, -0.010798f, 
            7.26169f, 2.38434f, -0.010798f, 
            15.6696f, 2.38434f, -0.010798f, 
            15.6696f, 1.00851f, -0.010798f, 
            14.964f, 1.00851f, -0.010798f, 
            7.75558f, -2.44873f, -0.010798f, 
            14.4231f, -9.36318f, -0.010798f, 
            16.0576f, -9.36318f, -0.010798f, 
            16.0576f, -10.6685f, -0.010798f, 
            7.62625f, -10.6685f, -0.010798f, 
            7.62625f, -9.33965f, -0.010798f, 
            9.67236f, -9.33965f, -0.010798f, 
            4.49827f, -3.90687f, -0.010798f, 
            -1.35784f, -6.59973f, -0.010798f, 
            -1.35784f, -9.3279f, -0.010798f, 
            0.217914f, -9.3279f, -0.010798f, 
            0.217914f, -10.6919f, -0.010798f, 
            -8.22526f, -10.6919f, -0.010798f, 
            -8.22526f, -9.32786f, -0.010798f, 
            -6.20266f, -9.32786f, -0.010798f};
    int[] indices={3, 2, 3, 1, 
            3, 1, 3, 6, 
            3, 1, 6, 10, 
            3, 10, 6, 7, 
            3, 10, 7, 8, 
            3, 4, 5, 6, 
            3, 4, 6, 3, 
            3, 10, 8, 9, 
            3, 1, 10, 0, 
            3, 13, 14, 15, 
            3, 13, 15, 18, 
            3, 13, 18, 20, 
            3, 13, 20, 12, 
            3, 16, 17, 18, 
            3, 16, 18, 15, 
            3, 12, 20, 21, 
            3, 12, 21, 11, 
            3, 20, 18, 19, 
            3, 49, 22, 44, 
            3, 44, 22, 28, 
            3, 44, 28, 43, 
            3, 43, 28, 29, 
            3, 43, 29, 42, 
            3, 42, 29, 35, 
            3, 42, 35, 41, 
            3, 41, 35, 36, 
            3, 41, 36, 38, 
            3, 38, 36, 37, 
            3, 39, 40, 41, 
            3, 39, 41, 38, 
            3, 29, 30, 32, 
            3, 29, 32, 34, 
            3, 29, 34, 35, 
            3, 46, 47, 49, 
            3, 46, 49, 44, 
            3, 46, 44, 45, 
            3, 22, 23, 25, 
            3, 22, 25, 27, 
            3, 22, 27, 28, 
            3, 25, 23, 24, 
            3, 27, 25, 26, 
            3, 49, 47, 48, 
            3, 32, 30, 31, 
            3, 34, 32, 33};
    float backrgb[] = new float[4]; 
    float rot; 
    // Instantiate GLU thing?
    private GLU glu = new GLU();
    /*************************************************************************************
    *   displayChanged 
    *   What does this do?  
    **************************************************************************************/
    public void displayChanged(GLAutoDrawable gLDrawable, 
                boolean modeChanged, boolean deviceChanged) 
    {
        // Function that does nothing?
    }
    /** Called by the drawable immediately after the OpenGL context is
    * initialized for the first time. Can be used to perform one-time OpenGL
    * initialization such as setup of lights and display lists.
    * @param gLDrawable The GLAutoDrawable object.
    */
    /*************************************************************************************
    *   init    
    **************************************************************************************/
    public void init(GLAutoDrawable gLDrawable) 
    {
        GL2 gl = gLDrawable.getGL().getGL2();
        //gl.glShadeModel(GL.GL_LINE_SMOOTH);              // Enable Smooth Shading
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    // Black Background
        gl.glClearDepth(1.0f);                      // Depth Buffer Setup
        gl.glEnable(GL.GL_DEPTH_TEST);              // Enables Depth Testing
        gl.glDepthFunc(GL.GL_LEQUAL);               // The Type Of Depth Testing To Do
        // Really Nice Perspective Calculations
        //gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);  
    }

    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, 
                int 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();
        glu.gluPerspective(45.0f, h, 1.0, 200.0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f, 0.0f, -40.0f);
    }
    @Override
    public void display(GLAutoDrawable gLDrawable) 
    {
        // TODO Auto-generated method stub
        final GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClearColor(backrgb[0], 0, 1, 1);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        backrgb[0]+=0.0005;
        if (backrgb[0]> 1) backrgb[0] = 0; 
        //  DRAW STUFF IN THIS FUNCTION
    }
    @Override
    public void dispose(GLAutoDrawable arg0) 
    {
        // TODO Auto-generated method stub
    }   
    @Override
    public void keyTyped(KeyEvent e) 
    {
        // TODO Auto-generated method stub
        char key= e.getKeyChar();
        System.out.printf("Key typed: %cn", key); 
    }
    @Override
    public void keyPressed(KeyEvent e) 
    {
        // TODO Auto-generated method stub
    }
    @Override
    public void keyReleased(KeyEvent e) 
    {
        // TODO Auto-generated method stub
    }
      /*  
    public void init(GLDrawable gLDrawable) {
        final GL gl = glDrawable.getGL();
        final GLU glu = glDrawable.getGLU();
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluOrtho2D(-1.0f, 1.0f, -1.0f, 1.0f); // drawing square
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }*/
}

基本上,由于某种原因,当我按键盘上的键时,"键入的键"永远不会输出,就好像它没有在听一样。我确信我的错误在某个微不足道的地方,但我不确定它在哪里?

您需要确保canvas具有焦点,以便在那里接收输入事件。我无法从代码量中分辨出哪个GUI元素接收输入事件。也可以将侦听器添加到显示的框架和其他元素中。或者将焦点显式地设置为canvas(并确保它保持在那里)。

使用的方法称为JComponent.requestFocus (API),当然需要在元素显示后调用。

key listener必须添加到框架(画布)。我将使HelloOpenGL类,扩展Frame,像这样:

public class HelloOpenGL extends Frame implements KeyListener{
    public HelloOpenGL(){
        addKeyListener(this); //this statement is missing from your code
        setFocusable(true);
        ...
    }
    ...
    //Override KeyListener interface methods in this class
}

所以我会在HelloOpenGL类本身做关键事件处理,因为它是一个代表帧,也记住@Andy Turner所说的。我以这种方式尝试了一个程序,在我的情况下工作得很好。

相关内容

  • 没有找到相关文章

最新更新