【问题标题】:Adding mouselistener to canvas with lwjgl display on it将鼠标监听器添加到画布上并显示 lwjgl
【发布时间】:2015-05-27 04:14:41
【问题描述】:

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

鼠标处理程序类:

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 类中将画布设置为父级的地方:

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();
}

当画布未设置为父级(画布上没有任何内容)时,mouseListener 起作用。但是当显示父级设置为画布时。它什么也不做。当画布设置为父级时,如何确定鼠标何时在画布上单击?

【问题讨论】:

标签: java lwjgl mouselistener


【解决方案1】:

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

但是,如果你想使用 swing,并在画布上添加一个真实的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();
    }
}

【讨论】:

    【解决方案2】:

    通过在我的 displayThread 中使用解决了它

    if(org.lwjgl.input.Mouse.isButtonDown(org.lwjgl.input.Mouse.getEventButton())){
        System.out.println("Mouse was clicked");
    }
    

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

    【讨论】:

      猜你喜欢
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 2019-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 2014-12-13
      相关资源
      最近更新 更多