Skip to content

Instantly share code, notes, and snippets.

Created August 29, 2014 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d8d346e954954c0c7cdb to your computer and use it in GitHub Desktop.
Save anonymous/d8d346e954954c0c7cdb to your computer and use it in GitHub Desktop.
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLJPanel;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
public class GLTest {
public static void main(String[] args) {
GLProfile glprofile = GLProfile.getDefault();
GLCapabilities glcapabilities = new GLCapabilities(glprofile);
// glcapabilities.setAlphaBits(1);
System.out.println(glcapabilities);
final GLJPanel gljpanel = new GLJPanel(glcapabilities);
gljpanel.addGLEventListener(new GLEventListener() {
@Override
public void reshape(GLAutoDrawable glautodrawable, int x, int y,
int width, int height) {
OneTriangle.setup(glautodrawable.getGL().getGL2(), width,
height);
System.out.println(gljpanel.getChosenGLCapabilities());
}
@Override
public void init(GLAutoDrawable glautodrawable) {
}
@Override
public void dispose(GLAutoDrawable glautodrawable) {
}
@Override
public void display(GLAutoDrawable glautodrawable) {
OneTriangle.render(glautodrawable.getGL().getGL2(),
glautodrawable.getWidth(), glautodrawable.getHeight());
}
});
final JFrame jframe = new JFrame("One Triangle Swing GLJPanel");
jframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowevent) {
jframe.dispose();
System.exit(0);
}
});
jframe.getContentPane().add(gljpanel, BorderLayout.CENTER);
jframe.setSize(640, 480);
jframe.setVisible(true);
}
private static class OneTriangle {
protected static void setup(GL2 gl2, int width, int height) {
gl2.glMatrixMode(GL2.GL_PROJECTION);
gl2.glLoadIdentity();
// coordinate system origin at lower left with width and height same
// as the window
GLU glu = new GLU();
glu.gluOrtho2D(0.0f, width, 0.0f, height);
gl2.glMatrixMode(GL2.GL_MODELVIEW);
gl2.glLoadIdentity();
gl2.glViewport(0, 0, width, height);
}
protected static void render(GL2 gl2, int width, int height) {
gl2.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// draw a triangle filling the window
gl2.glLoadIdentity();
gl2.glBegin(GL.GL_TRIANGLES);
gl2.glColor3f(1, 0, 0);
gl2.glVertex2f(0, 0);
gl2.glColor3f(0, 1, 0);
gl2.glVertex2f(width, 0);
gl2.glColor3f(0, 0, 1);
gl2.glVertex2f(width / 2, height);
gl2.glEnd();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment