• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

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.
0
thockit
Asked:
thockit
  • 5
  • 4
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now