import java.awt.Dimension;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;

import com.sun.opengl.util.FPSAnimator;
import com.sun.opengl.util.GLUT;

public class ScreenSaverOGL implements GLEventListener {

	/**
	 * ScreenSaverOGL - this is a simple screen saver that uses JOGL 
	 * Eric McCreath 2009,2011
	 * 
	 * You need to include the jogl jar files (gluegen-rt.jar and jogl.jar). In
	 * eclipse use "add external jars" in Project->Properties->Libaries
	 * otherwise make certain they are in the class path.  In the current linux 
         * computers there files are in the /usr/share/java directory.
	 * 
         * If you are executing from the command line then something like:
         *   javac -cp .:/usr/share/java/jogl.jar:/usr/share/java/gluegen-rt.jar ScreenSaverOGL.java
         *   java -cp .:/usr/share/java/jogl.jar:/usr/share/java/gluegen-rt.jar ScreenSaverOGL
         * should work.
         *
	 * When running the LD_LIBRARY_PATH environment variable should point to a
	 * directory that contains: libgluegen-rt.so, libjogl_cg.so, libjogl_awt.so,
	 * and libjogl.so. In eclipse this can be done in the "Run Configurations.."
	 * by adding an environment variable.  In the current linux set up th LD_LIBRARY_PATH
         * does not need to change.
	 * 
	 */

	JFrame jf;
	GLCanvas canvas;
	GLCapabilities caps;
	Dimension dim = new Dimension(800, 600);
	FPSAnimator animator;

	float xpos;
	float xvel;

	public ScreenSaverOGL() {
		jf = new JFrame();
		caps = new GLCapabilities();
		canvas = new GLCanvas(caps);
		canvas.addGLEventListener(this);
		canvas.requestFocusInWindow();
		jf.getContentPane().add(canvas);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
		jf.setPreferredSize(dim);
		jf.pack();
		animator = new FPSAnimator(canvas, 20);
		xpos = 100.0f;
		xvel = 1.0f;
		animator.start();
	}

	public static void main(String[] args) {
		new ScreenSaverOGL();
	}

	public void display(GLAutoDrawable dr) {
		GL gl = dr.getGL();
		GLU glu = new GLU();
		GLUT glut = new GLUT();

		gl.glClear(GL.GL_COLOR_BUFFER_BIT);
		gl.glColor3f(1.0f, 0.0f, 0.0f);
		gl.glRasterPos2f(xpos, 300.0f);
		glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, "Save the Screens");
		gl.glFlush();
		
		xpos += xvel;
		if (xpos > dim.getWidth())
			xpos = 0.0f;

	}

	public void displayChanged(GLAutoDrawable dr, boolean arg1, boolean arg2) {
	}

	public void init(GLAutoDrawable dr) {
		GL gl = dr.getGL();
		GLU glu = new GLU();
		GLUT glut = new GLUT();
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glMatrixMode(GL.GL_PROJECTION);
		glu.gluOrtho2D(0.0, dim.getWidth(), 0.0, dim.getHeight());
                gl.glMatrixMode(GL.GL_MODELVIEW);
	}

	public void reshape(GLAutoDrawable dr, int arg1, int arg2, int arg3,
			int arg4) {
	}
}

