Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package p5.test;
- import java.applet.Applet;
- import java.awt.Frame;
- import java.awt.GraphicsDevice;
- import java.awt.GraphicsEnvironment;
- import javax.media.opengl.GL;
- import javax.media.opengl.GLCapabilities;
- import javax.media.opengl.GLContext;
- import javax.media.opengl.GLProfile;
- import javax.media.opengl.*;
- import javax.media.opengl.awt.GLCanvas;
- public class MiniApplet extends Applet {
- private static final long serialVersionUID = 1L;
- private Frame frame;
- private GLProfile profile;
- private GLCapabilities capabilities;
- private GLCanvas canvas;
- private GLContext context0;
- private GLContext context;
- private double theta = 0;
- private double s = 0;
- private double c = 0;
- void setup() {
- // Need to create this canvas first to have rendering working
- canvas = new GLCanvas(null);
- GraphicsEnvironment environment =
- GraphicsEnvironment.getLocalGraphicsEnvironment();
- GraphicsDevice displayDevice = environment.getDefaultScreenDevice();
- frame = new Frame(displayDevice.getDefaultConfiguration());
- frame.setSize(640, 480);
- frame.setVisible(true);
- profile = GLProfile.getMaxFixedFunc();
- capabilities = new GLCapabilities(profile);
- capabilities.setSampleBuffers(false);
- canvas = new GLCanvas(capabilities);
- frame.add(this);
- frame.add(canvas);
- canvas.addGLEventListener(new Renderer());
- }
- void drawLoop() {
- Thread loop = new Thread() {
- public void run() {
- while (true) {
- canvas.display();
- }
- }
- };
- loop.start();
- }
- void draw(GL2 gl) {
- gl.glClearColor(0, 0, 0, 1);
- gl.glClear(GL.GL_COLOR_BUFFER_BIT);
- theta += 0.01;
- s = Math.sin(theta);
- c = Math.cos(theta);
- gl.glBegin(GL.GL_TRIANGLES);
- gl.glColor3f(1, 0, 0);
- gl.glVertex2d(-c, -c);
- gl.glColor3f(0, 1, 0);
- gl.glVertex2d(0, c);
- gl.glColor3f(0, 0, 1);
- gl.glVertex2d(s, -s);
- gl.glEnd();
- }
- class Renderer implements GLEventListener {
- @Override
- public void display(GLAutoDrawable drawable) {
- draw(drawable.getGL().getGL2());
- context = drawable.getContext();
- if (context0 != null && context != null && context.hashCode() != context0.hashCode()) {
- System.out.println("Context changed");
- }
- context0 = context;
- }
- @Override
- public void dispose(GLAutoDrawable drawable) {
- }
- @Override
- public void init(GLAutoDrawable drawable) {
- }
- @Override
- public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
- }
- }
- public static void main(String[] args) {
- MiniApplet mini;
- try {
- Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(MiniApplet.class.getName());
- mini = (MiniApplet) c.newInstance();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- if (mini != null) {
- mini.setup();
- mini.drawLoop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement