Advertisement
codeanticode

NEWT events

Nov 11th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3. import java.awt.Frame;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.WindowAdapter;
  6. import java.awt.event.WindowEvent;
  7. import java.lang.reflect.InvocationTargetException;
  8.  
  9. import javax.media.opengl.GL;
  10. import javax.media.opengl.GL2;
  11. import javax.media.opengl.GLAnimatorControl;
  12. import javax.media.opengl.GLAutoDrawable;
  13. import javax.media.opengl.GLCapabilities;
  14. import javax.media.opengl.GLEventListener;
  15. import javax.media.opengl.GLProfile;
  16.  
  17. import com.jogamp.newt.awt.NewtCanvasAWT;
  18. import com.jogamp.newt.event.MouseAdapter;
  19. import com.jogamp.newt.event.MouseListener;
  20. import com.jogamp.newt.opengl.GLWindow;
  21. import com.jogamp.opengl.util.Animator;
  22.  
  23. public class EventTest {
  24.   Frame frame;
  25.   GLWindow window;
  26.   NewtCanvasAWT canvas;  
  27.   float theta = 0;
  28.  
  29.   void draw(GL2 gl) {    
  30.     gl.glClearColor(0, 0, 0, 1);
  31.     gl.glClear(GL.GL_COLOR_BUFFER_BIT);
  32.    
  33.     theta += 0.01;
  34.     double s = Math.sin(theta);
  35.     double c = Math.cos(theta);      
  36.    
  37.     gl.glBegin(GL.GL_TRIANGLES);
  38.     gl.glColor3f(1, 0, 0);
  39.     gl.glVertex2d(-c, -c);
  40.     gl.glColor3f(0, 1, 0);
  41.     gl.glVertex2d(0, c);
  42.     gl.glColor3f(0, 0, 1);
  43.     gl.glVertex2d(s, -s);
  44.     gl.glEnd();    
  45.    
  46.     gl.glFlush();
  47.   }
  48.  
  49.   class TestGLListener implements GLEventListener {
  50.     public void display(GLAutoDrawable drawable) {
  51.       draw(drawable.getGL().getGL2());
  52.     }
  53.     public void dispose(GLAutoDrawable drawable) { }
  54.     public void init(GLAutoDrawable drawable) { }
  55.     public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) { }    
  56.   }
  57.  
  58.   class TestMouseAdapter extends MouseAdapter {    
  59.     public void mousePressed(MouseEvent e) {
  60.       System.out.println("mouse pressed event: " + e);        
  61.     }
  62.     public void mouseReleased(MouseEvent e) {
  63.       System.out.println("mouse released event: " + e);
  64.     }
  65.     public void mouseMoved(MouseEvent e) {          
  66.       System.out.println("mouse moved event: " + e);  
  67.     }    
  68.     public void mouseDragged(MouseEvent e) {
  69.       System.out.println("mouse dragged event: " + e);  
  70.     }    
  71.   }  
  72.  
  73.   public void run() throws InterruptedException, InvocationTargetException {
  74.     frame = new Frame("AWT Frame");
  75.    
  76.     GLProfile profile = GLProfile.getDefault();
  77.     GLCapabilities capabilities = new GLCapabilities(profile);
  78.    
  79.     window = GLWindow.create(capabilities);    
  80.     canvas = new NewtCanvasAWT(window);
  81.     canvas.setBounds(0, 0, 300, 300);
  82.     canvas.setFocusable(true);
  83.    
  84.     MouseListener mouseListener = new TestMouseAdapter();
  85.     window.addMouseListener(mouseListener);    
  86.    
  87.     frame.setLayout(new BorderLayout());
  88.     frame.add(canvas, BorderLayout.CENTER);
  89.    
  90.     TestGLListener glListener = new TestGLListener();
  91.     window.addGLEventListener(glListener);    
  92.     final GLAnimatorControl animator = new Animator(window);
  93.     animator.start();
  94.    
  95.     frame.setLocation(0, 0);
  96.     frame.setSize(300, 300);
  97.     frame.addWindowListener(new WindowAdapter() {
  98.       public void windowClosing(WindowEvent e) {
  99.         animator.stop();
  100.         System.exit(0);
  101.       }
  102.     });
  103.    
  104.     EventQueue.invokeAndWait(new Runnable() {
  105.       public void run() {
  106.         frame.validate();                
  107.         frame.setVisible(true);
  108.     }});
  109.   }
  110.  
  111.   public static void main(String[] args) {
  112.     EventTest test;
  113.     try {
  114.       Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(EventTest.class.getName());
  115.       test = (EventTest) c.newInstance();
  116.     } catch (Exception e) {
  117.       throw new RuntimeException(e);
  118.     }    
  119.     if (test != null) {
  120.       try {
  121.         test.run();
  122.       } catch (Exception e) {
  123.         e.printStackTrace();
  124.       }
  125.     }
  126.   }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement