PDA

View Full Version : Problem with drag and drop



Syris3000
11-19-2009, 07:20 AM
I am having some issues getting drag and drop to work in JOGL. The x motion is fine but the y motion seems to be getting off from where the shape i am dragging is. The cursor moves faster than the shape. I am translating the shape to where the mouse is moved but they don't match for some reason. I read on this forum that gluUnProject maps the mouse to objects but there is no call for that in JOGL. Any ideas? Here is the code i have.


import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.swing.JFrame;

import com.sun.opengl.util.FPSAnimator;

/**
* This is a simple example to try to have click and drag ability using JOGL
*
* @author jcoffman
*/
public class Dragging
extends JFrame
implements GLEventListener, KeyListener, MouseListener, MouseMotionListener
{
private GLCapabilities caps;
private GLCanvas canvas;
private FPSAnimator animator;
private float spin = 0f, spinDelta = 0f;


private float rectX = 0f;
private float rectY = 0f;

private float oldX = 0f;
private float oldY = 0f;

private float rectWidth = 40f;
private float rectHeight = 20f;

private int windowWidth = 1024;
private int windowHeight = 1024;
private boolean isDragging;

public Dragging()
{
super("doublebuf");

caps = new GLCapabilities();
caps.setDoubleBuffered(true);// request double buffer display mode
canvas = new GLCanvas(caps);
canvas.addGLEventListener(this);
canvas.addMouseListener(this);// register mouse callback functions
canvas.addMouseMotionListener(this);
animator = new FPSAnimator(canvas, 60);

getContentPane().add(canvas);
}

public void run()
{
setSize(windowWidth, windowHeight);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
animator.start();
}

public static void main(String[] args)
{
new Dragging().run();
}

public void init(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_FLAT);
}

public void display(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
//set up attrs to draw from the top left corner (0,0)
gl.glPushAttrib(GL.GL_DEPTH_BUFFER_BIT | GL.GL_COLOR_BUFFER_BIT
| GL.GL_ENABLE_BIT | GL.GL_TEXTURE_BIT
| GL.GL_TRANSFORM_BIT | GL.GL_VIEWPORT_BIT
| GL.GL_CURRENT_BIT);

gl.glMatrixMode(javax.media.opengl.GL.GL_PROJECTIO N);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glOrtho(0d, windowWidth, windowHeight, 0d, -1, 1);

//C code to for mapping mouse to object
// GLdouble pos3D_x, pos3D_y, pos3D_z;
//
// // arrays to hold matrix information
//
// GLdouble model_view[16];
// glGetDoublev(GL_MODELVIEW_MATRIX, model_view);
//
// GLdouble projection[16];
// glGetDoublev(GL_PROJECTION_MATRIX, projection);
//
// GLint viewport[4];
// glGetIntegerv(GL_VIEWPORT, viewport);
//
// // get 3D coordinates based on window coordinates
//
// gluUnProject(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 0.01,
// model_view, projection, viewport,
// &pos3D_x, &pos3D_y, &pos3D_z);


gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();

gl.glPushMatrix();
gl.glLoadIdentity();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
// original spin motion
// gl.glRotatef(spin, 0.0f, 0.0f, 1.0f);
gl.glColor3f(1.0f, 1.0f, 1.0f);

//move the rectangle
gl.glTranslatef(rectX, rectY, 0f);

//draw the rectangle
gl.glBegin(GL.GL_QUADS);
gl.glVertex2f(0f, 0f);
gl.glVertex2f(0f, rectHeight);
gl.glVertex2f(rectWidth, rectHeight);
gl.glVertex2f(rectWidth, 0f);
gl.glEnd( );

gl.glPopMatrix();
gl.glFlush();

//original spinning motion
//spinDisplay();
}

public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
{
GL gl = drawable.getGL();
gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();

if (w <= h) gl.glOrtho(-50.0, 50.0,//
-50.0 * (float) h / (float) w, //
50.0 * (float) h / (float) w, //
-1.0, 1.0);
else gl.glOrtho(-50.0 * (float) w / (float) h,
50.0 * (float) w / (float) h, -50.0, 50.0, //
-1.0, 1.0);

gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}

public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
boolean deviceChanged)
{
}

private void spinDisplay()
{
spin = spin + spinDelta;
if (spin > 360f) spin = spin - 360;
}

public void keyTyped(KeyEvent key)
{
}

public void keyPressed(KeyEvent key)
{
switch (key.getKeyCode()) {
case KeyEvent.VK_ESCAPE:
new Thread()
{
public void run()
{
animator.stop();
}
}.start();
System.exit(0);
default:
break;
}
}

public void keyReleased(KeyEvent key)
{
}

public void mouseClicked(MouseEvent key)
{
}

public void mousePressed(MouseEvent mouse)
{
oldX = mouse.getX();
oldY = mouse.getY();
if( ( (mouse.getX()> rectX &amp;&amp; (mouse.getX()<(rectX + rectWidth))) ) &amp;&amp;
( (mouse.getY()> rectY &amp;&amp; (mouse.getY()<(rectY + rectHeight))) ) ){
isDragging = true;
}
}


public void mouseReleased(MouseEvent mouse)
{
isDragging = false;
}

public void mouseEntered(MouseEvent mouse)
{
}

public void mouseExited(MouseEvent mouse)
{
}

@Override
public void mouseDragged(MouseEvent mouse) {
//System.out.println("MOUSE X: " + mouse.getX());
System.out.println("MOUSE Y: " + mouse.getY());
if(isDragging){
float movedX = mouse.getX() - oldX;
float movedY = mouse.getY() - oldY;
rectX += movedX;
rectY += movedY;
//System.out.println("RECT X: " + rectX);
System.out.println("RECT Y: " + rectY);
System.out.println("");
}

oldX = mouse.getX();
oldY = mouse.getY();

}

@Override
public void mouseMoved(MouseEvent e) {
System.out.println("Moved:"+e.getY());
// TODO Auto-generated method stub

}

}