Link to home
Start Free TrialLog in
Avatar of thockit
thockit

asked on

dragging an awt window using a mouse (jdk 1.1.8) ? for HeyHey

Dear Heyhey,

I currently have a situation where i am using the mouse to drag a window(not a frame) but one if the problems that i am facing is how to obtain the shift in the mouse drag. By that i mean whether the mouse moved up/down a pixel, or sideways a pixel. Because i cannot find a good smooth way to move the window. right now in  mouseDragged(MouseEvent e) i use a line of code that goes like:
        setLocation(getLocation().x+e.getX(),getLocation().y+e.getY());

but this can hardly be called good. Do you know something here that will help me really move the window in a good mathematically appropriate way.

Also, I have one more issue. When i click on the panel and drag it i fing that the cursor has a tendency to move to the top left corner and not stay at the point where it was clicked first. Maybe you can help me set this right too.

Thanks in advance,
thockit.
Avatar of thockit
thockit

ASKER

Edited text of question.
Avatar of thockit

ASKER

Edited text of question.
>> Dear Heyhey,

thanks :)

can you post some small compilable example, so that we can work directly on it ?

what's life without beer ! :)
Avatar of thockit

ASKER

ok !! i am going to send you the beer to your address at heyhey_@iname.com

thockit.
I can check your code tomorrow - but if you post it here, some other expert may solve the problem ...


beer time :)
Avatar of thockit

ASKER

where are you located ! :)
I think u can check the direction of movement by comparing the last coordinates of the event and the current one.
here is some drag-theWindow example code that works for me (IBM JDK1.1.7 / WinNT4WS)

you have to change your own code yourself :)

(I couldn't check your code yesterday - I found an old friend in this overcast country and we had very long beer 'discussion' ... :)



import java.awt.*;
import java.awt.event.*;

public class test extends Window
{
  public test(Frame f)
  {
    super(f);
    enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
  }
 
  public void update(Graphics g)
  {
    paint(g);
  }
 
  public void paint(Graphics g)
  {
    Dimension d = getSize();
    g.setColor(Color.lightGray);
    g.fillRect(0, 0, d.width, d.height);
    g.setColor(Color.cyan);
    g.fillRect(10, 10, d.width - 20, d.height - 20);
    g.setColor(Color.black);
    g.drawRect(0, 0, d.width -1, d.height -1);
    g.drawRect(10, 10, d.width - 21, d.height - 21);
  }
 
  protected void processMouseEvent(MouseEvent e)
  {
    thisProcessMouseEvent(e);
//    log("processMouseEvent id = " + e.getID());
    super.processMouseEvent(e);
  }  
  protected void processMouseMotionEvent(MouseEvent e)
  {
    thisProcessMouseEvent(e);
//    log("processMouseMotionEvent id = " + e.getID());
    super.processMouseMotionEvent(e);

  }

  private int lastX = -1;
  private int lastY = -1;
  protected void thisProcessMouseEvent(MouseEvent e)
  {
    int id = e.getID();
    int x = e.getX();
    int y = e.getY();
   
    switch (id)
    {
      case MouseEvent.MOUSE_CLICKED:
        log("mouseClicked: " + x + ", " + y);
        break;
      case MouseEvent.MOUSE_PRESSED:
        log("mousePressed: " + x + ", " + y);
        lastX = x;
        lastY = y;
        break;
      case MouseEvent.MOUSE_RELEASED:
        log("mouseReleased: " + x + ", " + y);
        break;
      case MouseEvent.MOUSE_ENTERED:
        log("mouseEntered: " + x + ", " + y);
        break;
      case MouseEvent.MOUSE_EXITED:
        log("mouseExited: " + x + ", " + y);
        break;
      case MouseEvent.MOUSE_DRAGGED:
        log("mouseDragged: " + x + ", " + y);
        Point p = getLocation();
        log("p: "+ p);
        int newX = p.x + (x - lastX);
        int newY = p.y + (y - lastY);
        log("new: " + newX + ", " + newY);
        setLocation(newX, newY);
        break;
      case MouseEvent.MOUSE_MOVED:
        log("mouseMoved: " + x + ", " + y);
        break;
      default:
        log("ERROR");
    }
  }  

  private void log(String st)
  {
//    System.out.println("" + st);
  }
 
  public static void main(String args[])
  {
    test t = new test(new Frame());
    t.setBounds(10, 10, 110, 110);
    t.setVisible(true);
  }
}
so can I lock this question ? :)

(btw. I won't be angry if you increase the points :)
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial