Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import java.awt.Frame;
- import java.awt.event.MouseEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.lang.reflect.InvocationTargetException;
- import javax.media.opengl.GL;
- import javax.media.opengl.GL2;
- import javax.media.opengl.GLAnimatorControl;
- import javax.media.opengl.GLAutoDrawable;
- import javax.media.opengl.GLCapabilities;
- import javax.media.opengl.GLEventListener;
- import javax.media.opengl.GLProfile;
- import com.jogamp.newt.awt.NewtCanvasAWT;
- import com.jogamp.newt.event.MouseAdapter;
- import com.jogamp.newt.event.MouseListener;
- import com.jogamp.newt.opengl.GLWindow;
- import com.jogamp.opengl.util.Animator;
- public class EventTest {
- Frame frame;
- GLWindow window;
- NewtCanvasAWT canvas;
- float theta = 0;
- void draw(GL2 gl) {
- gl.glClearColor(0, 0, 0, 1);
- gl.glClear(GL.GL_COLOR_BUFFER_BIT);
- theta += 0.01;
- double s = Math.sin(theta);
- double 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();
- gl.glFlush();
- }
- class TestGLListener implements GLEventListener {
- public void display(GLAutoDrawable drawable) {
- draw(drawable.getGL().getGL2());
- }
- public void dispose(GLAutoDrawable drawable) { }
- public void init(GLAutoDrawable drawable) { }
- public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) { }
- }
- class TestMouseAdapter extends MouseAdapter {
- public void mousePressed(MouseEvent e) {
- System.out.println("mouse pressed event: " + e);
- }
- public void mouseReleased(MouseEvent e) {
- System.out.println("mouse released event: " + e);
- }
- public void mouseMoved(MouseEvent e) {
- System.out.println("mouse moved event: " + e);
- }
- public void mouseDragged(MouseEvent e) {
- System.out.println("mouse dragged event: " + e);
- }
- }
- public void run() throws InterruptedException, InvocationTargetException {
- frame = new Frame("AWT Frame");
- GLProfile profile = GLProfile.getDefault();
- GLCapabilities capabilities = new GLCapabilities(profile);
- window = GLWindow.create(capabilities);
- canvas = new NewtCanvasAWT(window);
- canvas.setBounds(0, 0, 300, 300);
- canvas.setFocusable(true);
- MouseListener mouseListener = new TestMouseAdapter();
- window.addMouseListener(mouseListener);
- frame.setLayout(new BorderLayout());
- frame.add(canvas, BorderLayout.CENTER);
- TestGLListener glListener = new TestGLListener();
- window.addGLEventListener(glListener);
- final GLAnimatorControl animator = new Animator(window);
- animator.start();
- frame.setLocation(0, 0);
- frame.setSize(300, 300);
- frame.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- animator.stop();
- System.exit(0);
- }
- });
- EventQueue.invokeAndWait(new Runnable() {
- public void run() {
- frame.validate();
- frame.setVisible(true);
- }});
- }
- public static void main(String[] args) {
- EventTest test;
- try {
- Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(EventTest.class.getName());
- test = (EventTest) c.newInstance();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- if (test != null) {
- try {
- test.run();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement