These two programs I consider basic in handling mouse and keyboard events. I looked up some programming sites and had to collate info from variety of sources. If you are new to JOGL then I suggest you this site where you can download a PDF of e-book on computer graphics by David J Eck.
The first program i made was to handle keyboard events:
Here it is:
import java.awt.*;
import java.awt.event.*;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.gl2.*;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import javax.swing.JFrame;
class mykeyboardeventshandler implements KeyListener,GLEventListener
{
String msg="";
int x=10,y=20;
private GLU glu;
public void keyPressed(KeyEvent ke)
{
//showStatus("Key down");
}
public void keyReleased(KeyEvent ke)
{
//showStatus("Key released");
}
public void keyTyped(KeyEvent ke)
{
msg+=ke.getKeyChar();
//repaint();
}
/**
* Take care of initialization here.
*/
public void init(GLAutoDrawable gld) {
GL2 gl = gld.getGL().getGL2();
glu = new GLU();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glViewport(-250, -150, 250, 150);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
}
/**
* Take care of drawing here.
*/
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
/*
* put your code here
*/
// drawLine(gl, 0, 0, 100, 50);
gl.glPushMatrix();
GLUT glut = new GLUT();
gl.glRasterPos2i(0,10);
// gl.glTranslatef(0, 0, 0);
gl.glColor3f(1, 0, 0);
glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, msg);
gl.glPopMatrix();
gl.glFlush();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
}
public void dispose(GLAutoDrawable arg0)
{
}
}
public class keyboardeventsdemo
{
public static void main(String[] args)
{
//getting the capabilities object of GL2 profile
final GLProfile profile=GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities=new GLCapabilities(profile);
// The canvas
final GLCanvas glcanvas=new GLCanvas(capabilities);
final Animator animator = new Animator(glcanvas);
mykeyboardeventshandler b=new mykeyboardeventshandler();
glcanvas.addGLEventListener(b);
glcanvas.addKeyListener(b);
glcanvas.setSize(400, 400);
//creating frame
final JFrame frame=new JFrame("Basic frame");
//adding canvas to frame
frame.add(glcanvas);
frame.setSize(640,480);
frame.setResizable(false);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
animator.stop();
frame.dispose();
System.exit(0);
}
});
animator.start();
frame.setVisible(true);
glcanvas.requestFocus();
}
}
Snapshot:
The second program i made is on handling mouse events. I made a separate class for handling mouse events and GLEvents called MyMouseEventsHandler. I instantiate it in the MouseEventsDemo class and them add event listeners to the frame using this object.
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.gl2.GLUT;
import com.jogamp.opengl.util.Animator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
class MyMouseEventsHandler implements MouseMotionListener, MouseListener,GLEventListener
{
String msg="hello";
int mouseX=0,mouseY=0;
private GLU glu;
public void init()
{
}
public void mouseClicked(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse clicked";
//repaint();
}
public void mouseEntered(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse Entered";
//repaint();
}
public void mouseExited(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse Exited";
//repaint();
}
public void mousePressed(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse Pressed";
//repaint();
}
public void mouseReleased(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="Mouse Released";
//repaint();
}
public void mouseMoved(MouseEvent me)
{
//showStatus("Moving mouse at "+me.getX()+" , "+me.getY());
}
public void mouseDragged(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="mouse dragged";
//repaint();
}
public void init(GLAutoDrawable gld)
{
GL2 gl = gld.getGL().getGL2();
glu = new GLU();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glViewport(-250, -150, 250, 150);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
}
public void display(GLAutoDrawable gld)
{
GL2 gl = gld.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
/*
* put your code here
*/
// drawLine(gl, 0, 0, 100, 50);
gl.glPushMatrix();
GLUT glut = new GLUT();
gl.glRasterPos2i(0,10);
// gl.glTranslatef(0, 0, 0);
gl.glColor3f(1, 0, 0);
glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, msg);
gl.glPopMatrix();
gl.glFlush();
// gld.swapBuffers();
}
public void reshape(GLAutoDrawable gld,int x,int y,int width,int height)
{
}
public void dispose(GLAutoDrawable gld)
{
}
}
public class MouseEventsDemo extends JFrame
{
public static void main(String[] args)
{
final GLProfile profile=GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities=new GLCapabilities(profile);
// The canvas
final GLCanvas glcanvas=new GLCanvas(capabilities);
MyMouseEventsHandler mmeh=new MyMouseEventsHandler();
final Animator animator = new Animator(glcanvas);
// glcanvas.addGLEventListener(mmeh);
glcanvas.setSize(400, 400);
//creating frame
final JFrame frame=new JFrame("Basic frame");
//adding canvas to frame
frame.add(glcanvas);
frame.setSize(640,480);
frame.setResizable(false);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
animator.stop();
frame.dispose();
System.exit(0);
}
});
glcanvas.addGLEventListener(mmeh);
glcanvas.addMouseMotionListener(mmeh);
glcanvas.addMouseListener(mmeh);
animator.start();
frame.setVisible(true);
glcanvas.requestFocus();
}
}
Snapshot:
The first program i made was to handle keyboard events:
Here it is:
import java.awt.*;
import java.awt.event.*;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.gl2.*;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import javax.swing.JFrame;
class mykeyboardeventshandler implements KeyListener,GLEventListener
{
String msg="";
int x=10,y=20;
private GLU glu;
public void keyPressed(KeyEvent ke)
{
//showStatus("Key down");
}
public void keyReleased(KeyEvent ke)
{
//showStatus("Key released");
}
public void keyTyped(KeyEvent ke)
{
msg+=ke.getKeyChar();
//repaint();
}
/**
* Take care of initialization here.
*/
public void init(GLAutoDrawable gld) {
GL2 gl = gld.getGL().getGL2();
glu = new GLU();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glViewport(-250, -150, 250, 150);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
}
/**
* Take care of drawing here.
*/
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
/*
* put your code here
*/
// drawLine(gl, 0, 0, 100, 50);
gl.glPushMatrix();
GLUT glut = new GLUT();
gl.glRasterPos2i(0,10);
// gl.glTranslatef(0, 0, 0);
gl.glColor3f(1, 0, 0);
glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, msg);
gl.glPopMatrix();
gl.glFlush();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
}
public void dispose(GLAutoDrawable arg0)
{
}
}
public class keyboardeventsdemo
{
public static void main(String[] args)
{
//getting the capabilities object of GL2 profile
final GLProfile profile=GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities=new GLCapabilities(profile);
// The canvas
final GLCanvas glcanvas=new GLCanvas(capabilities);
final Animator animator = new Animator(glcanvas);
mykeyboardeventshandler b=new mykeyboardeventshandler();
glcanvas.addGLEventListener(b);
glcanvas.addKeyListener(b);
glcanvas.setSize(400, 400);
//creating frame
final JFrame frame=new JFrame("Basic frame");
//adding canvas to frame
frame.add(glcanvas);
frame.setSize(640,480);
frame.setResizable(false);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
animator.stop();
frame.dispose();
System.exit(0);
}
});
animator.start();
frame.setVisible(true);
glcanvas.requestFocus();
}
}
Snapshot:
The second program i made is on handling mouse events. I made a separate class for handling mouse events and GLEvents called MyMouseEventsHandler. I instantiate it in the MouseEventsDemo class and them add event listeners to the frame using this object.
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.gl2.GLUT;
import com.jogamp.opengl.util.Animator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
class MyMouseEventsHandler implements MouseMotionListener, MouseListener,GLEventListener
{
String msg="hello";
int mouseX=0,mouseY=0;
private GLU glu;
public void init()
{
}
public void mouseClicked(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse clicked";
//repaint();
}
public void mouseEntered(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse Entered";
//repaint();
}
public void mouseExited(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse Exited";
//repaint();
}
public void mousePressed(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="mouse Pressed";
//repaint();
}
public void mouseReleased(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="Mouse Released";
//repaint();
}
public void mouseMoved(MouseEvent me)
{
//showStatus("Moving mouse at "+me.getX()+" , "+me.getY());
}
public void mouseDragged(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="mouse dragged";
//repaint();
}
public void init(GLAutoDrawable gld)
{
GL2 gl = gld.getGL().getGL2();
glu = new GLU();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glViewport(-250, -150, 250, 150);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
}
public void display(GLAutoDrawable gld)
{
GL2 gl = gld.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
/*
* put your code here
*/
// drawLine(gl, 0, 0, 100, 50);
gl.glPushMatrix();
GLUT glut = new GLUT();
gl.glRasterPos2i(0,10);
// gl.glTranslatef(0, 0, 0);
gl.glColor3f(1, 0, 0);
glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, msg);
gl.glPopMatrix();
gl.glFlush();
// gld.swapBuffers();
}
public void reshape(GLAutoDrawable gld,int x,int y,int width,int height)
{
}
public void dispose(GLAutoDrawable gld)
{
}
}
public class MouseEventsDemo extends JFrame
{
public static void main(String[] args)
{
final GLProfile profile=GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities=new GLCapabilities(profile);
// The canvas
final GLCanvas glcanvas=new GLCanvas(capabilities);
MyMouseEventsHandler mmeh=new MyMouseEventsHandler();
final Animator animator = new Animator(glcanvas);
// glcanvas.addGLEventListener(mmeh);
glcanvas.setSize(400, 400);
//creating frame
final JFrame frame=new JFrame("Basic frame");
//adding canvas to frame
frame.add(glcanvas);
frame.setSize(640,480);
frame.setResizable(false);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
animator.stop();
frame.dispose();
System.exit(0);
}
});
glcanvas.addGLEventListener(mmeh);
glcanvas.addMouseMotionListener(mmeh);
glcanvas.addMouseListener(mmeh);
animator.start();
frame.setVisible(true);
glcanvas.requestFocus();
}
}
Snapshot:
No comments:
Post a Comment