netbeans - how to apply mousemotion listener on a single cube using JOGL -
i had created 3d cube in jogl using netbeans , want perform mouse drag event on single object using mousemotion listener interface mouse drag event occured when adding other object in canvas , rotation of cube , other object performs simultaneously want rotate cube using mouse drag event so, please suggest me solution problem thank you
box2.java
package box2; import com.sun.opengl.util.animator; import java.awt.dimension; import java.awt.frame; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.event.mousemotionlistener; import java.awt.event.windowadapter; import java.awt.event.windowevent; import javax.media.opengl.gl; import javax.media.opengl.glautodrawable; import javax.media.opengl.glcanvas; import javax.media.opengl.gleventlistener; import javax.media.opengl.glu.glu; public class box2 implements gleventlistener, mouselistener, mousemotionlistener { public static void main(string[] args) { frame frame = new frame("3d box"); glcanvas canvas = new glcanvas(); canvas.addgleventlistener(new box2()); frame.add(canvas); frame.setsize(900, 780); final animator animator = new animator(canvas); frame.addwindowlistener(new windowadapter() { @override public void windowclosing(windowevent e) { new thread(new runnable() { public void run() { animator.stop(); system.exit(0); } }).start(); } }); // center frame frame.setlocationrelativeto(null); frame.setvisible(true); animator.start(); } // rotating scene private float view_rotx = 80.0f; private float view_roty = 80.0f; // remember last mouse position private int oldmousex; private int oldmousey; public void init(glautodrawable drawable) { gl gl = drawable.getgl(); // set backgroundcolor , shading mode gl.glclearcolor(0.8f, 1.0f, 0.3f, 0.0f); gl.glshademodel(gl.gl_flat); // give me light float ambient[] = {0.0f,0.0f,0.0f,0.0f }; float diffuse[] = {0.0f,0.0f,0.0f,0.0f }; float specular[]= {0.2f,1.0f,0.2f,1.0f}; float position[] = {20.0f,30.0f,20.0f,0.0f }; gl.gllightfv(gl.gl_light0, gl.gl_ambient,ambient,0); gl.gllightfv(gl.gl_light0, gl.gl_diffuse, diffuse,0); gl.gllightfv(gl.gl_light0, gl.gl_position, position,0); gl.glmaterialfv(gl.gl_front,gl.gl_specular, specular, 0); // , red material float[] mambient ={0.1745f, 0.01175f, 0.01175f, 0.55f }; float[] mdiffuse ={0.61424f, 0.04136f, 0.04136f, 0.55f }; float[] mspecular ={0.727811f, 0.626959f, 0.626959f, 0.55f }; float mshine =76.8f; gl.glmaterialfv(gl.gl_front,gl.gl_ambient,mambient,0); gl.glmaterialfv(gl.gl_front,gl.gl_diffuse,mdiffuse,0); gl.glmaterialfv(gl.gl_front,gl.gl_specular,mspecular, 0); gl.glmaterialf (gl.gl_front,gl.gl_shininess,mshine); // gl.glenable(gl.gl_lighting); // gl.glenable(gl.gl_light0); // gl.glenable(gl.gl_depth_test); // gl.glenable(gl.gl_normalize); system.out.println("gl.gl_lighting : "+gl.gl_lighting); system.out.println("gl.gl_light0 : "+gl.gl_light0); system.out.println("gl.gl_depth_test : "+gl.gl_depth_test); drawable.addmouselistener(this); drawable.addmousemotionlistener(this); } public void reshape(glautodrawable drawable, int x, int y, int width, int height) { gl gl = drawable.getgl(); glu glu = new glu(); if (height <= 0) // no divide 0 height = 1; // keep ratio final float h = (float) width / (float) height; gl.glviewport(0, 0, width, height); gl.glmatrixmode(gl.gl_projection); gl.glloadidentity(); glu.gluperspective(45.0f, h, 1.0, 20.0); gl.glmatrixmode(gl.gl_modelview); gl.glloadidentity(); } public void display(glautodrawable drawable) { gl gl = drawable.getgl(); glu glu = new glu(); // needed lookat gl.glclear(gl.gl_color_buffer_bit | gl.gl_depth_buffer_bit); gl.glloadidentity(); glu.glulookat(4,4,4, // eye pos 0,0,0, // @ 0,0,1); // gl.gltranslatef(0.5f, 0.5f, 0.5f); gl.glrotatef(view_rotx, 1.0f, 0.0f, 0.0f); gl.glrotatef(view_roty, 0.0f, 1.0f, 0.0f); gl.gltranslatef(-0.5f, -0.5f, -0.5f); onebox.drawme(gl); onebox.drawcircle(gl); gl.glflush(); } public void displaychanged(glautodrawable drawable, boolean modechanged, boolean devicechanged) { } public void mouseclicked(mouseevent e) {system.out.println("mouseclicked called");} public void mouseentered(mouseevent e) {system.out.println("mouseentered called");} public void mouseexited(mouseevent e) {system.out.println("mouseexited called");} public void mousereleased(mouseevent e) {system.out.println("mousereleased called"); } public void mousepressed(mouseevent e) { system.out.println("mousepressed called"); oldmousex = e.getx(); oldmousey = e.gety(); } public void mousedragged(mouseevent e) { system.out.println("mousedragged called"); int x = e.getx(); int y = e.gety(); dimension size = e.getcomponent().getsize(); float thetay = 360.0f * ( (float)(x-oldmousex)/(float)size.width); float thetax = 360.0f * ( (float)(oldmousey-y)/(float)size.height); oldmousex = x; oldmousey = y; view_rotx += thetax; view_roty += thetay; } public void mousemoved(mouseevent e) {} } **onebox.java** package box2; import static java.lang.math.cos; import static java.lang.math.sin; import javax.media.opengl.gl; /** *@author borres */ public class onebox { static void drawme(gl gl) { /* draws sides of unit cube (0,0,0)-(1,1,1) */ gl.glpushmatrix(); gl.glbegin(gl.gl_polygon);/* f1: front */ gl.glcolor3f(0.3f, 0.5f, 0.0f); gl.glnormal3f(-1.0f,0.0f,0.0f); gl.glvertex3f(0.0f,0.0f,0.0f); gl.glvertex3f(0.0f,0.0f,1.0f); gl.glvertex3f(1.0f,0.0f,1.0f); gl.glvertex3f(1.0f,0.0f,0.0f); gl.glend(); gl.glbegin(gl.gl_polygon);/* f2: bottom */ gl.glcolor3f(0.9f, 0.0f, 0.5f); gl.glnormal3f(0.0f,0.0f,-1.0f); gl.glvertex3f(0.0f,0.0f,0.0f); gl.glvertex3f(1.0f,0.0f,0.0f); gl.glvertex3f(1.0f,1.0f,0.0f); gl.glvertex3f(0.0f,1.0f,0.0f); gl.glend(); gl.glbegin(gl.gl_polygon);/* f3:back */ gl.glcolor3f(0.0f, 0.1f, 1.0f); gl.glnormal3f(1.0f,0.0f,0.0f); gl.glvertex3f(1.0f,1.0f,0.0f); gl.glvertex3f(1.0f,1.0f,1.0f); gl.glvertex3f(0.0f,1.0f,1.0f); gl.glvertex3f(0.0f,1.0f,0.0f); gl.glend(); gl.glbegin(gl.gl_polygon);/* f4: top */ gl.glcolor3f(0.0f, 0.6f, 0.7f); gl.glnormal3f(0.0f,0.0f,1.0f); gl.glvertex3f(1.0f,1.0f,1.0f); gl.glvertex3f(1.0f,0.0f,1.0f); gl.glvertex3f(0.0f,0.0f,1.0f); gl.glvertex3f(0.0f,1.0f,1.0f); gl.glend(); gl.glbegin(gl.gl_polygon);/* f5: left */ gl.glcolor3f(0.8f, 0.0f, 0.2f); gl.glnormal3f(0.0f,1.0f,0.0f); gl.glvertex3f(0.0f,0.0f,0.0f); gl.glvertex3f(0.0f,1.0f,0.0f); gl.glvertex3f(0.0f,1.0f,1.0f); gl.glvertex3f(0.0f,0.0f,1.0f); gl.glend(); gl.glbegin(gl.gl_polygon);/* f6: right */ gl.glcolor3f(0.1f, 0.0f, 0.6f); gl.glnormal3f(0.0f,-1.0f,0.0f); gl.glvertex3f(1.0f,0.0f,0.0f); gl.glvertex3f(1.0f,0.0f,1.0f); gl.glvertex3f(1.0f,1.0f,1.0f); gl.glvertex3f(1.0f,1.0f,0.0f); gl.glend(); gl.glpopmatrix(); } static void drawcircle(gl gl) { gl.gltranslatef(-0.5f, 0.5f, -3.5f); gl.glpushmatrix(); final float degree_to_rad = (float)3.14 / 180; gl.glbegin(gl.gl_line_loop); gl.glcolor3f(1.0f, 0.0f, 1.0f); int m_in_degree = 460; int n_in_degree = 100; for( int nr =n_in_degree; nr < m_in_degree; nr++ ) { float fx = (float) sin((float)nr * degree_to_rad ); float fy = (float) cos((float)nr * degree_to_rad ); gl.glvertex2f( fx-5, fy-5 ); } gl.glend(); gl.glpopmatrix(); } }
Comments
Post a Comment