Java - 自定义形状的可拖动 JFrame

2022-01-24 00:00:00 java swing jframe

所以我使用 setShape(s); 制作了一个自定义形状的 JFrame当你将 JFrame 设置为未装饰时,我想看看我想要的问题,你不能用鼠标在屏幕上拖动 Frame,所以我尝试实现自己的可拖动框架,但它没有按预期工作,这是 Frame 类:

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 Frame with your mouse on the screen, so I tried to implement my own draggable frame, but it's not working as supposed to, here's the Frame class :

package rt;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.event.MouseInputAdapter;

public class MyFrame extends JFrame{

private static final int WIDTH = 1024;
private static final int HEIGHT = 1024;
private static int [] OCTAGON_COORDS_X = {300, 524, 680, 680, 524, 300, 144, 144};
private static int [] OCTAGON_COORDS_Y = {300, 300, 457, 680, 836, 836, 680, 457};

public MyFrame() throws IOException {
    // Determine what the default GraphicsDevice can support.
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();

    boolean isUniformTranslucencySupported = gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT);
    boolean isShapedWindowSupported = gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT);
    if (isUniformTranslucencySupported && isShapedWindowSupported) {
        setUndecorated(true);
        setSize(WIDTH, HEIGHT);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        updateFrame();
        //setOpacity(0.55f);
        setVisible(true);
        MyListener myListener = new MyListener();
        addMouseListener(myListener);
        addMouseMotionListener(myListener);
    }
    else {
        System.err.println("Custom shaped JFrames and trunslucent JFrames are not supported by this device!");
        System.exit(0);
    }
}

public void updateFrame() {
    Shape s = new Polygon (OCTAGON_COORDS_X, OCTAGON_COORDS_Y, OCTAGON_COORDS_X.length);
    setShape(s);
}

private class MyListener extends MouseInputAdapter {

    private int initialPressedX;
    private int initialPressedY;

    @Override
    public void mousePressed(MouseEvent e) {
        initialPressedX = e.getX();
        initialPressedY = e.getY();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        updateFrameCoords(e);
    }


    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();
    }
}

}

主类:

package rt;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {
        MyFrame frame = new MyFrame();
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

但是拖动操作没有像我预期的那样工作我猜问题出在这些行:有人介意检查并解释我做错了什么以及如何解决吗?提前致谢!

But the dragging action isn't working as I expected I guess the problem lies in those lines : Anyone mind checking and explaining what I did wrong and how to fix it ? Thanks in advance !

推荐答案

我认为你的问题是在执行 mouseDragged 时担心框架的形状.我认为您不必担心这一点.

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);
}

相关文章