flickering

Can anyone suggest me why the drawing is flickering using the GLEventListener implementation below ?

Many thanks

D.

import java.awt.;
import java.awt.event.
;
import javax.swing.;
import net.java.games.jogl.
;

public class GLEventListenerImpl implements GLEventListener, MouseListener, MouseMotionListener {
private Animator animator;
int viewportWidth;
int viewportHeight;

boolean exitRequested = false;
boolean valid = true;
int x = 300, y = 300;

public GLEventListenerImpl() {
}

public void displayChanged(GLDrawable glDrawable, boolean modeChanged, boolean deviceChanged) {
}

public void reshape(GLDrawable glDrawable, int x, int y, int width, int height) {
    viewportWidth = width;
  viewportHeight = height;
}
  
public void init(GLDrawable glDrawable) {
  glDrawable.addMouseListener(this);
  glDrawable.addMouseMotionListener(this);
        
  final GL gl = glDrawable.getGL();

    gl.glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); //white 
    gl.glColor3f( 1.0f, 1.0f, 1.0f ); 
        
    animator = new Animator(glDrawable);
    animator.start();
  valid = false;
}
  
public void display(GLDrawable glDrawable) {
  final GL gl = glDrawable.getGL();
    final GLU glu = glDrawable.getGLU();

    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity(); 
    glu.gluOrtho2D (0,
                    viewportWidth,
                    0,
                    viewportHeight);

  gl.glViewport( 0, 0, viewportWidth, viewportHeight ); 
  if (!valid) {
      valid = true;
      
        gl.glClear (GL.GL_COLOR_BUFFER_BIT);

        int i = 0;
        exitRequested = false;
        for (; i < 100000 && !exitRequested; i++) {
            gl.glBegin (GL.GL_LINES);
            gl.glVertex2i (100, 100);
            gl.glVertex2i (x, viewportHeight - y);
            gl.glEnd();
        }

        System.err.println(i);
  }
  
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public void mouseMoved(MouseEvent e) {
}

public void mouseDragged(MouseEvent e) {
    valid = false;
  x = e.getX();
  y = e.getY();
  exitRequested = true;
}

}

Solved thanks 8)

D.

Always nice to post the solution :wink:

Kev

as to have any feedback … ::slight_smile:


glCanvas.addGLEventListener(new GLEventListenerImpl());
glCanvas.setAutoSwapBufferMode(false);

import java.awt.;
import java.awt.event.
;
import javax.swing.;
import net.java.games.jogl.
;

public class GLEventListenerImpl implements GLEventListener, MouseListener, MouseMotionListener {
private Animator animator;
int viewportWidth;
int viewportHeight;

boolean exitRequested = false;
boolean valid = true;
int x = 300, y = 300;

public GLEventListenerImpl() {
}

public void displayChanged(GLDrawable glDrawable, boolean modeChanged, boolean deviceChanged) {
}

public void reshape(GLDrawable glDrawable, int x, int y, int width, int height) {
    viewportWidth = width;
  viewportHeight = height;
}
  
public void init(GLDrawable glDrawable) {
  glDrawable.addMouseListener(this);
  glDrawable.addMouseMotionListener(this);
        
  final GL gl = glDrawable.getGL();

    gl.glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); //white 
    gl.glColor3f( 1.0f, 1.0f, 1.0f ); 
        
    animator = new Animator(glDrawable);
    animator.start();
  valid = false;
}
  
public void display(GLDrawable glDrawable) {
  final GL gl = glDrawable.getGL();
    final GLU glu = glDrawable.getGLU();

    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity(); 
    glu.gluOrtho2D (0,
                    viewportWidth,
                    0,
                    viewportHeight);

  gl.glViewport( 0, 0, viewportWidth, viewportHeight ); 
  if (!valid) {
      valid = true;
      
      glDrawable.swapBuffers();
      
        gl.glClear (GL.GL_COLOR_BUFFER_BIT);

      stop = false;
      
        int i = 0;
        exitRequested = false;
        for (; i < 10000 && !exitRequested; i++) {
            gl.glBegin (GL.GL_LINES);
            gl.glVertex2i (100, 100);
            gl.glVertex2i (x, viewportHeight - y);
            gl.glEnd();
        }

      stop = true;
      
        System.err.println(i);
  }
  
}

boolean stop = false;

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public void mouseMoved(MouseEvent e) {
}

public void mouseDragged(MouseEvent e) {
  exitRequested = true;
  while (!stop) {
        try {
          Thread.sleep(10);
        } catch (Exception x) {}
  }

  x = e.getX();
  y = e.getY();
    valid = false;
}

}

You did only post yesterday, quite late on (at least for my day).

Thanks for the solution tho.

Kev

i was jocking :wink: Anyway here is the most recent version using Java 5 Locks 8)

import java.awt.;
import java.util.concurrent.locks.ReentrantLock;
import java.awt.event.
;
import javax.swing.;
import net.java.games.jogl.
;

public class GLEventListenerImpl3 implements GLEventListener, MouseListener, MouseMotionListener {
private Animator animator;

private final ReentrantLock displayLock;
private boolean shouldDisplay;
private boolean shouldStop;

private int viewportWidth;
private int viewportHeight;

// TEST VARS ///////////////////////////////
int x, y;
////////////////////////////////////////////

public GLEventListenerImpl3() {
  displayLock = new ReentrantLock();
}

public void displayChanged(GLDrawable glDrawable, boolean modeChanged, boolean deviceChanged) {
}

public void reshape(GLDrawable glDrawable, int x, int y, int width, int height) {
    viewportWidth = width;
  viewportHeight = height;
}
  
public void init(GLDrawable glDrawable) {
  glDrawable.addMouseListener(this);
  glDrawable.addMouseMotionListener(this);
        
  final GL gl = glDrawable.getGL();

    gl.glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); //white 
    gl.glColor3f( 1.0f, 1.0f, 1.0f ); 
        
    animator = new Animator(glDrawable);
    animator.start();
  
  shouldDisplay = true;
  shouldStop = false;
}
  
public void display(GLDrawable glDrawable) {
  final GL gl = glDrawable.getGL();
    final GLU glu = glDrawable.getGLU();

    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
  
    glu.gluOrtho2D (0,
                    viewportWidth,
                    0,
                    viewportHeight);

  gl.glViewport( 0, 0, viewportWidth, viewportHeight );
  
  if (shouldDisplay) {
      displayLock.lock();

      shouldDisplay = false;
      
      glDrawable.swapBuffers();
      
        gl.glClear (GL.GL_COLOR_BUFFER_BIT);

      shouldStop = false;
      
      // HERE IS THE DRAWING ////////////////////
        int i = 0;
        for (; i < 100000 && !shouldStop; i++) {
            gl.glBegin (GL.GL_LINES);
            gl.glVertex2i (100, 100);
            gl.glVertex2i (x, viewportHeight - y);
            gl.glEnd();
        }
        System.err.println(i);
      ///////////////////////////////////////////

      displayLock.unlock();
  }
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public void mouseMoved(MouseEvent e) {
}

public void mouseDragged(MouseEvent e) {
  shouldStop = true;
  
  displayLock.lock();
  
  // HERE IS THE EVENT EFFECT ///////////////////
  x = e.getX();
  y = e.getY();
  ///////////////////////////////////////////////
  
  displayLock.unlock();
    
  shouldDisplay = true;
}

}

Do u know how to clean the mouse event queue ?

D.