Java - Custom shaped draggable JFrame

随声附和 提交于 2019-12-02 08:44:51

I think your problem is worrying about the shape of the frame when executing the mouseDragged. I don't think you need to worry about that.

If you just change your method to the below, it works. Test it out.

void updateFrameCoords(MouseEvent e) {
    setLocation(getLocation().x + e.getX() - initialPressedX,
                getLocation().y + e.getY() - initialPressedY);
}
  1. You have to update initial coordinates at each drag.
  2. evt.getX() gives coordinates relative to frame. You should use evt.getXOnScreen() in stead.

Replace

void updateFrameCoords(MouseEvent e) {
    int dx = e.getX() - initialPressedX;
    int dy = e.getY() - initialPressedY;
    for (int i = 0; i < OCTAGON_COORDS_X.length; i++) {
        OCTAGON_COORDS_X[i] += dx;
        OCTAGON_COORDS_Y[i] += dy;
    }
    updateFrame();
}

with

void updateFrameCoords(MouseEvent e) {
    int dx = e.getXOnScreen() - initialPressedX;
    int dy = e.getYOnScreen() - initialPressedY;
    for (int i = 0; i < OCTAGON_COORDS_X.length; i++) {
        OCTAGON_COORDS_X[i] += dx;
        OCTAGON_COORDS_Y[i] += dy;
    }
    updateFrame();
    initialPressedX = e.getXOnScreen();
    initialPressedY = e.getYOnScreen();
}

Same in mousePressed event.

Good luck.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!