Advertisement
codeanticode

Simple NEWT code

Feb 19th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.08 KB | None | 0 0
  1. package p5.newt_eventtest;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5. import java.awt.Frame;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8. import java.lang.reflect.InvocationTargetException;
  9.  
  10. import javax.media.opengl.GL;
  11. import javax.media.opengl.GL2;
  12. import javax.media.opengl.GLAnimatorControl;
  13. import javax.media.opengl.GLAutoDrawable;
  14. import javax.media.opengl.GLCapabilities;
  15. import javax.media.opengl.GLEventListener;
  16. import javax.media.opengl.GLProfile;
  17.  
  18. import com.jogamp.newt.awt.NewtCanvasAWT;
  19. import com.jogamp.newt.event.WindowUpdateEvent;
  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 NEWTMouseListener extends com.jogamp.newt.event.MouseAdapter {
  59.     @Override
  60.     public void mousePressed(com.jogamp.newt.event.MouseEvent e) {
  61.     }
  62.     @Override
  63.     public void mouseReleased(com.jogamp.newt.event.MouseEvent e) {
  64.     }
  65.     @Override
  66.     public void mouseClicked(com.jogamp.newt.event.MouseEvent e) {
  67.     }
  68.     @Override
  69.     public void mouseDragged(com.jogamp.newt.event.MouseEvent e) {
  70.     }
  71.     @Override
  72.     public void mouseMoved(com.jogamp.newt.event.MouseEvent e) {
  73.       System.out.println("mouse moved");
  74.     }
  75.     @Override
  76.     public void mouseWheelMoved(com.jogamp.newt.event.MouseEvent e) {
  77.     }
  78.     @Override
  79.     public void mouseEntered(com.jogamp.newt.event.MouseEvent e) {
  80.       System.out.println("mouse entered");
  81.     }
  82.     @Override
  83.     public void mouseExited(com.jogamp.newt.event.MouseEvent e) {
  84.       System.out.println("mouse exited");
  85.     }
  86.   }
  87.  
  88.   class NEWTWindowListener implements com.jogamp.newt.event.WindowListener {
  89.     @Override
  90.     public void windowDestroyNotify(com.jogamp.newt.event.WindowEvent arg0) {
  91.     }
  92.  
  93.     @Override
  94.     public void windowDestroyed(com.jogamp.newt.event.WindowEvent arg0) {
  95.     }
  96.  
  97.     @Override
  98.     public void windowGainedFocus(com.jogamp.newt.event.WindowEvent arg0) {
  99.       System.out.println("gained focus");
  100.     }
  101.  
  102.     @Override
  103.     public void windowLostFocus(com.jogamp.newt.event.WindowEvent arg0) {
  104.       System.out.println("lost focus");      
  105.     }
  106.  
  107.     @Override
  108.     public void windowMoved(com.jogamp.newt.event.WindowEvent arg0) {
  109.     }
  110.  
  111.     @Override
  112.     public void windowRepaint(WindowUpdateEvent arg0) {
  113.     }
  114.  
  115.     @Override
  116.     public void windowResized(com.jogamp.newt.event.WindowEvent arg0) {      
  117.     }
  118.   }
  119.  
  120.   public void run() throws InterruptedException, InvocationTargetException {
  121.     frame = new Frame("AWT Frame");
  122.     frame.setLocation(800, 500);
  123.     frame.setSize(300, 300);    
  124.    
  125.     GLProfile profile = GLProfile.getDefault();
  126.     GLCapabilities capabilities = new GLCapabilities(profile);
  127.    
  128.     window = GLWindow.create(capabilities);    
  129.     canvas = new NewtCanvasAWT(window);
  130.     canvas.setBounds(0, 0, 300, 300);
  131.     canvas.setFocusable(true);
  132.    
  133.     NEWTMouseListener mouseListener = new NEWTMouseListener();
  134.     window.addMouseListener(mouseListener);    
  135.    
  136.     NEWTWindowListener windowListener = new NEWTWindowListener();
  137.     window.addWindowListener(windowListener);
  138.    
  139.     frame.setLayout(new BorderLayout());
  140.     frame.add(canvas, BorderLayout.CENTER);
  141.    
  142.     TestGLListener glListener = new TestGLListener();
  143.     window.addGLEventListener(glListener);    
  144.     final GLAnimatorControl animator = new Animator(window);
  145.     animator.start();
  146.    
  147.     frame.addWindowListener(new WindowAdapter() {
  148.       public void windowClosing(WindowEvent e) {
  149.         animator.stop();
  150.         System.exit(0);
  151.       }
  152.     });
  153.    
  154.     EventQueue.invokeAndWait(new Runnable() {
  155.       public void run() {
  156.         frame.validate();                
  157.         frame.setVisible(true);
  158.     }});
  159.   }
  160.  
  161.   public static void main(String[] args) {
  162.     EventTest test;
  163.     try {
  164.       Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(EventTest.class.getName());
  165.       test = (EventTest) c.newInstance();
  166.     } catch (Exception e) {
  167.       throw new RuntimeException(e);
  168.     }    
  169.     if (test != null) {
  170.       try {
  171.         test.run();
  172.       } catch (Exception e) {
  173.         e.printStackTrace();
  174.       }
  175.     }
  176.   }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement