import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Rectangle;

import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.newt.swt.NewtCanvasSWT;
import com.jogamp.opengl.util.Animator;

public class NewtTest {
    NewtCanvasSWT right;
	public NewtTest() {
		final Display d = new Display();
		Shell shell = new Shell(d);
		shell.setLayout(new FillLayout());
		shell.setSize(400, 200);

	    BaseClass demo = new BaseClass();
		GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2));
		
		GLWindow glWindow1 = GLWindow.create(caps);
		glWindow1.addGLEventListener(demo);
		right = NewtCanvasSWT.create(shell, SWT.NO_BACKGROUND,   glWindow1);
		shell.open();
		
		Animator anim = new Animator(glWindow1);
		anim.start();
		while (!shell.isDisposed()) {
			if (!d.readAndDispatch()) {
				d.sleep();
			}
		}	
		d.dispose();
		System.exit(0);
	}

	class BaseClass implements GLEventListener {
		
		int x, y;
		int w, h;

		protected void setup(GL2 gl2) {
			gl2.glMatrixMode(GL2.GL_PROJECTION);
			gl2.glLoadIdentity();

			GLU glu = new GLU();
			glu.gluOrtho2D(0.0f, w, 0.0f, h);

			gl2.glMatrixMode(GL2.GL_MODELVIEW);
			gl2.glLoadIdentity();

			gl2.glViewport(x, y, w, h);
            
		}

		protected  void render(GL2 gl2) {
			gl2.glClear(GL.GL_COLOR_BUFFER_BIT);

			gl2.glLoadIdentity();
			gl2.glBegin(GL.GL_TRIANGLES);
			gl2.glColor3f(1, 0, 0);
			gl2.glVertex2f(0, 0);
			gl2.glColor3f(0, 1, 0);
			gl2.glVertex2f(w, 0);
			gl2.glColor3f(0, 0, 1);
			gl2.glVertex2f(w / 2, h);
			gl2.glEnd();
		}

		@Override
		public void display(GLAutoDrawable arg0) {
			render((GL2) arg0.getGL());
		}

		@Override
		public void dispose(GLAutoDrawable arg0) {
		}

		@Override
		public void init(GLAutoDrawable arg0) {
			setup((GL2) arg0.getGL());
		}

		@Override
		public void reshape(GLAutoDrawable arg0, int x, int y, int w, int h) {
			this.x = x; this.y = y; this.w = w; this.h = h;
			setup((GL2) arg0.getGL());
		}
	}
	
	public static void main(String args[]){
		new NewtTest();
	}

}
