Using java.awt.event.MouseMotionListener with Display on a java.awt.Canvas

Started by daboross, April 01, 2013, 22:17:39

Previous topic - Next topic

daboross

I am trying to use a MouseMotionListener with lwjgl, mostly because I prefer to have code run through notification instead of through polling, but when I try to attach a MouseMotionListener to a Canvas that I have used Display.setParent on, it won't receive any MosueEvents.
This code Does receive mouse events:
package net.daboross.test;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author daboross
 */
public class Test {

    public static void main(String[] args) throws Exception {
        JFrame jFrame = new JFrame("test");
        jFrame.setResizable(false);
        jFrame.setPreferredSize(new Dimension(640, 480));
        jFrame.pack();
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        jFrame.setLocation((d.width - 640) / 2, (d.height - 480) / 2);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Canvas canvas = new Canvas();
        jFrame.add(canvas);
        canvas.addMouseMotionListener(new MouseMotionListener() {
            public void mouseMoved(MouseEvent e) {
                System.out.println(e);
            }

            public void mouseDragged(MouseEvent e) {
                System.out.println(e);
            }
        });
        jFrame.setVisible(true);
    }
}

while this code doesn't:
package net.daboross.test;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author daboross
 */
public class Test {

    public static void main(String[] args) throws Exception {
        JFrame jFrame = new JFrame("test");
        jFrame.setResizable(false);
        jFrame.setPreferredSize(new Dimension(640, 480));
        jFrame.pack();
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        jFrame.setLocation((d.width - 640) / 2, (d.height - 480) / 2);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Canvas canvas = new Canvas();
        jFrame.add(canvas);
        canvas.addMouseMotionListener(new MouseMotionListener() {
            public void mouseMoved(MouseEvent e) {
                System.out.println(e);
            }

            public void mouseDragged(MouseEvent e) {
                System.out.println(e);
            }
        });
        jFrame.setVisible(true);
        try {
            Display.setParent(canvas);
            Display.setDisplayMode(new DisplayMode(640, 480));
            Display.create();
            GL11.glMatrixMode(GL11.GL_PROJECTION);
            GL11.glLoadIdentity();
            GL11.glOrtho(0, 640, 0, 480, 10, -10);
            GL11.glMatrixMode(GL11.GL_MODELVIEW);
        } catch (LWJGLException lwjgle) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, "LWJGLException when initializing display", lwjgle);
            System.exit(1);
        }
    }
}


Is the Display being loaded onto the Canvas stop that Canvas from outputting MouseEvents? Is this built into lwjgl, or is it a bug.
I can switch to using org.lwjgl.input.Mouse, but I would rather not to if there is another way,

I think that having java notify my program for mouse movement is more efficient then polling org.lwjgl.input.Mouse, but is that the case?
~Dabo Ross daboross.net

piegames

I've got exactly the same problem. When Keyboard.create() or Mouse.create() are called, they cut all Key/MouseEvents. The real Problem is, that Display.create() calls those two methods. That means, that you can't use AWT Events and a 3D Canvas at the same time :(

Cornix

As far as I know there is no way to keep the Swing Mouse- and Keyevents when using an lwjgl Display because the input from lwjgl overshadows the input from Swing.
Instead you could simply generate those events yourself with some simple code within your OpenGL rendering thread.