我正在使用java和LWJGL。我为画布创建了一个鼠标监听器,但是当我将Displays父级设置为画布时它不起作用。
mousehandlerClass:
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可以正常工作。但是当显示父级设置为画布时。它什么都不做。当画布设置为父级时,如何确定在画布上单击鼠标的时间?
答案 0 :(得分: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();
}
}
答案 1 :(得分:0)
使用我的displayThread
解决了这个问题if(org.lwjgl.input.Mouse.isButtonDown(org.lwjgl.input.Mouse.getEventButton())){
System.out.println("Mouse was clicked");
}
LWJGL可以检查鼠标是否被按下。