LWJGL Display mounted on Canvas fails to generate Mouse Events

社会主义新天地 提交于 2020-01-25 23:56:13

问题


My ultimate goal is to be able to make use of additional mouse buttons in Java. Currently, LWJGL's JInput doesn't seem capable of detecting more than three buttons. To make use of Java's System.setProperty("sun.awt.enableExtraMouseButtons", "true"), I've tried mounting the Display onto an AWT Canvas, within a JFrame. Unfortunately, this does not appear to work, and I am unsure why. [I should note that I've been away from Java for a some time]

import java.awt.Canvas;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class MainCanvas extends Canvas implements MouseListener
{
    private static final long serialVersionUID = 1L;

    public void mouseClicked(MouseEvent e)
    {
        System.out.println(e.getButton());
    }
    public void mouseEntered(MouseEvent e)
    {
        System.out.println(e.getButton());
    }
    public void mouseExited(MouseEvent e)
    {
        System.out.println(e.getButton());
    }
    public void mousePressed(MouseEvent e)
    {
        System.out.println(e.getButton());
    }
    public void mouseReleased(MouseEvent e)
    {
        System.out.println(e.getButton());
    }

    public void init()
    {
    }


    public static void main(String[] args)
    {
        MainCanvas mainCanvas = new MainCanvas();
        JFrame mainFrame = new JFrame("Simplify");

        mainFrame.setSize(640, 480);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.getContentPane().add(mainCanvas);
        mainFrame.setVisible(true);

        mainCanvas.addMouseListener(mainCanvas);

        try
        {
            DisplayMode mainDisplay = new DisplayMode(640, 480);
            Display.setDisplayMode(mainDisplay);
            Display.setParent(mainCanvas);
            Display.create();
        }
        catch (LWJGLException le)
        {
            System.out.println("Oh dear.");
        }

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 640, 480, 0, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        while (!Display.isCloseRequested())
        {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);  
            GL11.glColor3f(0.5f,0.5f,1.0f);
            GL11.glBegin(GL11.GL_QUADS);
                GL11.glVertex2f(100,100);
                GL11.glVertex2f(100+200,100);
                GL11.glVertex2f(100+200,100+200);
                GL11.glVertex2f(100,100+200);
            GL11.glEnd();
            Display.update();
        }
        Display.destroy();
    }
}

回答1:


I spoke with the fellows in the FreeNode IRC some months back. The gist is because the canvas is a heavyweight component, events would not rise to the JFrame level. A Frame has to be used in its stead.




回答2:


Basically, the answer is you can't: i.e with lwjgl 2.8.2 on Windows only.

The reason is that the Windows implementation of lwjgl clobbers a key data structure that AWT requires for event handling.

http://www.java-gaming.org/topics/cannot-add-mouselistener-to-java-awt-canvas-with-lwjgl-on-windows/24650/msg/208505/view.html#msg208505




回答3:


Try adding the mouse listener to the JFRame instead. I ran into this before, and I think that's how I solved it.



来源:https://stackoverflow.com/questions/8460514/lwjgl-display-mounted-on-canvas-fails-to-generate-mouse-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!