0

我想用 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) {        
    }

}
4

1 回答 1

3

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

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

于 2013-05-03T16:05:54.493 回答