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.
thockitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

thockitAuthor Commented:
Edited text of question.
0
thockitAuthor Commented:
Edited text of question.
0
heyhey_Commented:
>> Dear Heyhey,

thanks :)

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

what's life without beer ! :)
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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

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


beer time :)
0
thockitAuthor Commented:
where are you located ! :)
0
samersCommented:
I think u can check the direction of movement by comparing the last coordinates of the event and the current one.
0
heyhey_Commented:
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);
  }
}
0
heyhey_Commented:
so can I lock this question ? :)

(btw. I won't be angry if you increase the points :)
0
heyhey_Commented:
answer ?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.