Search code examples
javawindows-7jogljava-6

JOGL – Not working colors


I have just started studying JOGL and there is one problem.

Below is simple implementation of two methods of GLEventListener.

@Override
public void init(GLDrawable glDrawable) {

    GL gl = glDrawable.getGL();
    GLU glu = glDrawable.getGLU();

    gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    glu.gluOrtho2D(0.0, 600.0, 0.0, 500.0);

}

@Override
public void display(GLDrawable glDrawable) {

    GL gl = glDrawable.getGL();
    gl.glPointSize(100.0f);

    gl.glColor3f(0.0f, 1.0f, 0.0f);
    gl.glBegin(GL.GL_POINTS);
    gl.glVertex2i(300, 250);
    gl.glEnd();

    gl.glEnd();

}

That is how I insert component in JFrame window.

package scribble;

import net.java.games.jogl.GLCanvas;
import net.java.games.jogl.GLCapabilities;
import net.java.games.jogl.GLDrawableFactory;
import javax.swing.*;
import java.awt.*;

public class ScribbleComponent extends JFrame {

    public static void main(String[] args) {
        ScribbleComponent component = new ScribbleComponent();
        ScribbleRunnable runnable = new ScribbleRunnable(component);
        SwingUtilities.invokeLater(runnable);
    }

    public ScribbleComponent() {
        super("Scribble");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GLCapabilities glCapabilities = new GLCapabilities();
        GLCanvas glCanvas = GLDrawableFactory.getFactory().createGLCanvas(glCapabilities);
        glCanvas.addGLEventListener(new ScribbleListener());
        getContentPane().add(glCanvas, BorderLayout.CENTER);
        getContentPane().add(new JButton("Clear Screen"), BorderLayout.SOUTH);
        setSize(600, 500);
        ScribbleHelper.centerWindow(this);
    }

}

ScribbleRunnable(component) makes component visible.

ScribbleHelper.centerWindow(this) just centering of the JFrame window.

I expect to see green square on white background.

It works correctly on the first computer, but on the second I am getting dark red square on black background. That happens always, I can not set any other colors.

Both computers are running Windows 7, using JDK 1.6 and the same libs.

Where could be the problem?


Solution

  • You should implement an com.jogamp.opengl.util.FPSAnimator to constantly update you display() method. Then have a look at it.

    Also use gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); to keep you display clean.