我想用Java和Jogl编写程序。我们有一个Jframe和一个位于该框架内的GLJPanel。该程序接收鼠标事件,当用户单击框架时,它将在该位置绘制一个点。我为此程序编写了以下代码,但存在问题。有时,当您单击框架时,不会发出该点击事件或延迟发出该事件。请告诉我如何解决此问题。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jogli;

import com.jogamp.opengl.util.FPSAnimator;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.awt.GLJPanel;
import javax.swing.JFrame;

/**
 *
 * @author HESSAM
 */
public class JOGLI extends JFrame {

    /**
     * @param args the command line arguments
     */
    private GLJPanel pan;

    public JOGLI()
    {
        pan = new Curve();
        this.setMinimumSize( new Dimension( 800 , 600 ) );
        this.add(pan);
        this.pack();
        this.setVisible(true);
        final FPSAnimator anime1 = new FPSAnimator( pan , 24 , true );
        anime1.start();
        this.addWindowListener( new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // Use a dedicate thread to run the stop() to ensure that the
                // animator stops before program exits.
                new Thread() {
                   @Override
                   public void run() {
                      if (anime1.isStarted()) {
                           anime1.stop();
                       }
                      System.exit(0);
                   }
                }.start();
            }
        });
    }
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JOGLI().setVisible(true);
            }
        });
        // TODO code application logic here
    }
}




/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jogli;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLJPanel;

/**
 *
 * @author HESSAM
 */
public class Curve extends GLJPanel implements GLEventListener {
    private ArrayList < int[] > pointSet;
    private GL2 mygl;
    public Curve()
    {
        pointSet = new ArrayList<>();
        this.addGLEventListener(this);
        this.addMouseListener( new MouseAdapter() {
            @Override
            public void mouseClicked( MouseEvent e )
            {
                final int[] newPoint = { 0 , 0 , 0 };
                newPoint[0] = e.getX();
                newPoint[1] = getSize().height - e.getY();
                pointSet.add(newPoint);
                display();

            }
        });
    }
    @Override
    public void init(GLAutoDrawable drawable) {
        mygl = drawable.getGL().getGL2();
        mygl.glClearColor(0.15f, 0.15f , 0.15f , 1);
        mygl.glClear(mygl.GL_COLOR_BUFFER_BIT);
        mygl.glLoadIdentity();
        mygl.glMatrixMode(mygl.GL_PROJECTION);
        mygl.glLoadIdentity();
        mygl.glOrtho(0, getSize().width, 0, getSize().height, -1, 1);
        mygl.glMatrixMode(mygl.GL_MODELVIEW);
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
    }

    @Override
    public void display(GLAutoDrawable drawable) {
        mygl = drawable.getGL().getGL2();
        mygl.glClear(mygl.GL_COLOR_BUFFER_BIT);
        mygl.glBegin(mygl.GL_POINTS);
        {
            mygl.glColor3d(1, 0.1, 0.5);
            for( int i = 0 ; i < pointSet.size() ; ++i )
            {
                for(int j = -2 ; j <= 2 ; ++j ) {
                    for(int k = -2 ; k <= 2 ; ++k ) {
                        mygl.glVertex2d(pointSet.get(i)[0] + j, pointSet.get(i)[1] + k);
                    }
                }
            }
        }
        mygl.glEnd();
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    }

}

最佳答案

AWT输入EventDispatchThread被渲染阻止,反之亦然,这是AWT的设计限制,因为它在单个线程上执行输入和渲染。

建议使用JogAmp JOGL NEWT输入和窗口系统。
NEWT旨在与渲染并行进行输入。您可以直接使用GLWindow(NEWT),也可以使用NewtCanvasAWT(AWT应用程序内部的GLWindow),然后使用NEWTEventFiFo将输入注入渲染循环。
http://jogamp.org/jogl/doc/NEWT-Overview.html

08-16 12:39