将 mouselistener 添加到 canvas,并在其上显示 lwjgl

Adding mouselistener to canvas with lwjgl display on it

我正在使用 java 和 LWJGL。我为 canvas 创建了一个鼠标侦听器,但是当我将 Displays 父级设置为 canvas 时它不起作用。

mousehandler类:

private static class handlerClass implements MouseListener {
    public handlerClass() {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("Canvas clicked");
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

这里是我在 DisplayManager class 中将 canvas 设置为父项的地方:

public void createDisplayJFrame(Canvas canvas) {
    ContextAttribs attribs = new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true);
    try {
        Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
        Display.setParent(canvas);
        Display.create(new PixelFormat(), attribs);

    } catch (LWJGLException ex) {
        //System.out.println(ex);
    }
    GL11.glViewport(0, 0, WIDTH, HEIGHT);
    lastFrameTime = getCurrentTime();
}

这是我添加 MouseListener 的地方:

public class UIMain extends javax.swing.JFrame {

/**
 * Creates new form UIMain
 */
private static Canvas canvas;
public static DisplayThread dt;
HandlerClass handler = new HandlerClass();

public UIMain() {
    initComponents();
    canvas = new Canvas();
    canvas.addMouseListener(handler);
    canvas.setSize(500, 500);
    canvas.setBackground(Color.WHITE);
    canvas.isDisplayable();
    canvas.setVisible(true);
    jPanel2.add(canvas, BorderLayout.CENTER);
}

我的 DisplayThreadClass:

public class DisplayThread extends Thread {

private Canvas canvas;
ArrayList<Entity> entities = new ArrayList();

public DisplayThread(Canvas canvas) {
    this.canvas = canvas;
}

public void run() {
    new DisplayManager().createDisplayJFrame(canvas);
    //Created entities and added to entities
    ......
    MasterRenderer renderer = new MasterRenderer();
    while (!Display.isCloseRequested()) {
        DisplayManager.updateDisplay();
        //Here is the solution   if(org.lwjgl.input.Mouse.isButtonDown(org.lwjgl.input.Mouse.getEventButton())){
            System.out.println("Mouse was clicked");
        }
    }
    renderer.cleanUp();
    DisplayManager.closeDisplay();
}

当 canvas 未设置为父项时(canvas 上没有任何内容),则 mouseListener 起作用。但是当displays parent设置为canvas的时候。它什么都不做。当 canvas 设置为父级时,我如何确定何时在 canvas 上单击鼠标?

通过在我的 displayThread

中使用解决了它
if(org.lwjgl.input.Mouse.isButtonDown(org.lwjgl.input.Mouse.getEventButton())){
    System.out.println("Mouse was clicked");
}

LWJGL 可以检查鼠标是否被按下。

尽管实际问题可能已解决:这可能与在 LWJGL 中完成的某些 "magic" 有关,并且可能与 LWJGL Display mounted on Canvas fails to generate Mouse Events 有关 - 所以如果您使用 Frame 而不是 JFrame,它应该已经可以工作了。

但是,如果你想使用swing,并且在canvas中添加一个真正的MouseListener,你可以考虑使用一个LWJGL AWTGLCanvas:

import static org.lwjgl.opengl.GL11.*;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.AWTGLCanvas;
import org.lwjgl.util.glu.GLU;

public class LwjglCanvasMouseEvents
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        LwjglCanvas canvas = null;
        try
        {
            canvas = new LwjglCanvas();
        }
        catch (LWJGLException e)
        {
            e.printStackTrace();
        }
        canvas.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                System.out.println(e);
            }
        });
        f.getContentPane().add(canvas);
        f.setSize(400, 400);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

class LwjglCanvas extends AWTGLCanvas
{
    private int currentWidth;
    private int currentHeight;

    public LwjglCanvas() throws LWJGLException
    {
        super();
    }

    @Override
    public void paintGL()
    {
        if (getWidth() != currentWidth || getHeight() != currentHeight)
        {
            currentWidth = getWidth();
            currentHeight = getHeight();
            glViewport(0, 0, currentWidth, currentHeight);
        }
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        GLU.gluOrtho2D(0.0f, currentWidth, 0.0f, currentHeight);

        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();

        glBegin(GL_TRIANGLES);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(0, 0, 0);
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(200, 0, 0);
        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(100, 200, 0);
        glEnd();

        glPopMatrix();
        try
        {
            swapBuffers();
        }
        catch (LWJGLException e)
        {
            e.printStackTrace();
        }
        repaint();
    }
}