Java - Custom shaped draggable JFrame

前端 未结 2 1016
不知归路
不知归路 2021-01-26 05:58

so I made a custom shaped JFrame using setShape(s); and I got it to look how I wanted the problem when you set the JFrame to be undecorated you can\'t drag the Fram

2条回答
  •  星月不相逢
    2021-01-26 06:28

    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.

提交回复
热议问题