Advertisement
codeanticode

Untitled

Feb 14th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. package p5.test;
  2.  
  3. import java.applet.Applet;
  4. import java.awt.Frame;
  5. import java.awt.GraphicsDevice;
  6. import java.awt.GraphicsEnvironment;
  7.  
  8. import javax.media.opengl.GL;
  9. import javax.media.opengl.GLCapabilities;
  10. import javax.media.opengl.GLContext;
  11. import javax.media.opengl.GLProfile;
  12.  
  13. import javax.media.opengl.*;
  14. import javax.media.opengl.awt.GLCanvas;
  15.  
  16. public class MiniApplet extends Applet {
  17.   private static final long serialVersionUID = 1L;
  18.  
  19.   private Frame frame;
  20.   private GLProfile profile;
  21.   private GLCapabilities capabilities;
  22.   private GLCanvas canvas;
  23.   private GLContext context0;
  24.   private GLContext context;
  25.  
  26.   private double theta = 0;
  27.   private double s = 0;
  28.   private double c = 0;  
  29.  
  30.   void setup() {
  31.     // Need to create this canvas first to have rendering working
  32.     canvas = new GLCanvas(null);
  33.    
  34.     GraphicsEnvironment environment =
  35.       GraphicsEnvironment.getLocalGraphicsEnvironment();
  36.     GraphicsDevice displayDevice = environment.getDefaultScreenDevice();
  37.          
  38.     frame = new Frame(displayDevice.getDefaultConfiguration());
  39.     frame.setSize(640, 480);
  40.     frame.setVisible(true);
  41.    
  42.     profile = GLProfile.getMaxFixedFunc();
  43.     capabilities = new GLCapabilities(profile);
  44.     capabilities.setSampleBuffers(false);
  45.    
  46.     canvas = new GLCanvas(capabilities);
  47.     frame.add(this);
  48.     frame.add(canvas);
  49.     canvas.addGLEventListener(new Renderer());
  50.   }  
  51.  
  52.   void drawLoop() {
  53.     Thread loop = new Thread() {
  54.       public void run() {        
  55.         while (true) {
  56.           canvas.display();
  57.         }
  58.       }
  59.     };
  60.     loop.start();        
  61.   }
  62.  
  63.   void draw(GL2 gl) {
  64.     gl.glClearColor(0, 0, 0, 1);
  65.     gl.glClear(GL.GL_COLOR_BUFFER_BIT);
  66.    
  67.     theta += 0.01;
  68.     s = Math.sin(theta);
  69.     c = Math.cos(theta);      
  70.    
  71.     gl.glBegin(GL.GL_TRIANGLES);
  72.     gl.glColor3f(1, 0, 0);
  73.     gl.glVertex2d(-c, -c);
  74.     gl.glColor3f(0, 1, 0);
  75.     gl.glVertex2d(0, c);
  76.     gl.glColor3f(0, 0, 1);
  77.     gl.glVertex2d(s, -s);
  78.     gl.glEnd();    
  79.   }
  80.  
  81.   class Renderer implements GLEventListener {      
  82.     @Override
  83.     public void display(GLAutoDrawable drawable) {
  84.       draw(drawable.getGL().getGL2());      
  85.      
  86.       context = drawable.getContext();      
  87.       if (context0 != null && context != null && context.hashCode() != context0.hashCode()) {
  88.         System.out.println("Context changed");
  89.       }
  90.       context0 = context;
  91.     }
  92.  
  93.     @Override
  94.     public void dispose(GLAutoDrawable drawable) {
  95.     }
  96.  
  97.     @Override
  98.     public void init(GLAutoDrawable drawable) {
  99.     }
  100.  
  101.     @Override
  102.     public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
  103.     }    
  104.   }
  105.  
  106.   public static void main(String[] args) {
  107.     MiniApplet mini;
  108.     try {
  109.       Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(MiniApplet.class.getName());
  110.       mini = (MiniApplet) c.newInstance();
  111.     } catch (Exception e) {
  112.       throw new RuntimeException(e);
  113.     }    
  114.     if (mini != null) {
  115.       mini.setup();
  116.       mini.drawLoop();
  117.     }
  118.   }      
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement