The problem was that I was calling getPixel()
in mouseClicked()
(i.e. from another thread). An OpenGL context can only be active in a single thread at a time. Solutions are discussed here.
For example, it is more correct to use OpenGL context in this method:
/**
* Called back by the animator to perform per-frame rendering.
*/
@Override
public void display(GLAutoDrawable glAutoDrawable) {
GL2 gl = glAutoDrawable.getGL().getGL2(); // get the OpenGL 2 graphics context
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // clear background
gl.glLoadIdentity(); // reset the model-view matrix
// Rendering code
/* There you can fill the whole window with a color,
or draw something more beautiful... */
gl.glFlush();
/* Here goes testing cycle from mouseClicked(); and it works! */
}