java - 检测用户鼠标悬停在 JPanel 上 Path2D 形状的边缘

标签 java swing hover jpanel shapes

因此,我正在尝试为矩形实现拖动以调整大小的功能。

因此,我想要的是当用户将鼠标悬停在矩形的右下角 上时,将光标更改为双向箭头。 (就像您调整 Chrome 浏览器大小时一样)我不确定该怎么做。

这是程序的简化版本,只是在屏幕中央添加了一个正方形。有人对我如何实现这个有任何想法吗?谢谢

更新

我几乎明白了,但当我位于矩形的右上角时,光标也在变化。只有当它位于右下角时,我才需要它。我究竟做错了什么?谢谢!

public void mouseMoved(MouseEvent e) {
        int mouseX = e.getX();
        int mouseY = e.getY();
        Path2D hitShape = null;
        for (Path2D shape : shapes) {

            Rectangle bounds = shape.getBounds();
            if (mouseY >= bounds.y && mouseY <= bounds.y + getHeight()) {
                int lowerX = bounds.x + bounds.width - 2;
                int upperX = lowerX + 4;
                if (mouseX >= lowerX && mouseX <= upperX) {
                    hitShape = shape;
                    System.out.println("Hit");
                    Cursor cursor = Cursor
                            .getPredefinedCursor(Cursor.SE_RESIZE_CURSOR);
                    setCursor(cursor);
                    break;
                } else {
                    Cursor cursor = Cursor
                            .getPredefinedCursor(Cursor.DEFAULT_CURSOR);
                    setCursor(cursor);
                }
            }

        }
    }

完整代码

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

public class UMLEditor {

    public static void main(String[] args) {

        JFrame frame = new UMLWindow();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(30, 30, 1000, 700);
        frame.getContentPane().setBackground(Color.white);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class UMLWindow extends JFrame {
    Shapes shapeList = new Shapes();
    Panel panel;

    private static final long serialVersionUID = 1L;

    public UMLWindow() {
        addMenus();
        panel = new Panel();
    }

    public void addMenus() {

        getContentPane().add(shapeList);

        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        shapeList.addSquare(100, 100);
    }
}

// Shapes class, used to draw the shapes on the panel
// as well as implements the MouseListener for dragging
class Shapes extends JPanel {
    private static final long serialVersionUID = 1L;

    private List<Path2D> shapes = new ArrayList<Path2D>();
    int currentIndex;

    public Shapes() {
        MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
        addMouseListener(myMouseAdapter);
        addMouseMotionListener(myMouseAdapter);
    }

    public void addSquare(int width, int height) {
        Path2D rect2 = new Path2D.Double();
        rect2.append(new Rectangle(442, 269, width, height), true);

        shapes.add(rect2);
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(2));
        for (Path2D shape : shapes) {
            g2.draw(shape);
        }
    }

    class MyMouseAdapter extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseDragged(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            int mouseX = e.getX();
            int mouseY = e.getY();
            Path2D hitShape = null;
            for (Path2D shape : shapes) {

                Rectangle bounds = shape.getBounds();
                if (mouseY >= bounds.y && mouseY <= bounds.y + bounds.getHeight()) {
                    int lowerX = bounds.x + bounds.width - 2;
                    int upperX = lowerX + 4;
                    if (mouseX >= lowerX && mouseX <= upperX) {
                        hitShape = shape;
                        System.out.println("Hit");
                        Cursor cursor = Cursor
                                .getPredefinedCursor(Cursor.SE_RESIZE_CURSOR);
                        setCursor(cursor);
                        break;
                    } else {
                        Cursor cursor = Cursor
                                .getPredefinedCursor(Cursor.DEFAULT_CURSOR);
                        setCursor(cursor);
                    }
                }

            }
        }
    }
}

最佳答案

有点像...

int mouseX = e.getX();
int mouseY = e.getY();
Path2D hitShape = null;
for (Path2D shape : shapes) {

    Rectangle bounds = shape.getBounds();
    if (mouseY >= bounds.y && mouseY <= bounds.y + height) {
        int lowerX = bounds.x + bounds.width - 2;
        int upperX = lowerX + 4;
        if (mouseX >= lowerX && mouseX <= upperX) {
            hitShape = shape;
            break;
        }
    }

}

// Deal with the shape you just hit...

您可能也有兴趣查看 MouseMotionListener并根据光标的位置更改光标/形状...

更新了右角检测

右角需要与鼠标的 x 和 y 位置相匹配,因为你应该在该点周围留出一些“边距”,你可以简单地使用 mouseY >= bounds.y - margin && mouseY <= bounds.y + margin , 然后你会确定鼠标在哪一侧......

int mouseX = e.getX();
int mouseY = e.getY();
Path2D hitShape = null;
for (Path2D shape : shapes) {

    Rectangle bounds = shape.getBounds();
    // Upper edge...with buffer...
    if (mouseY >= bounds.y - 4 && mouseY <= bounds.y + 4) {
        int lowerX = bounds.x + bounds.width - 2;
        int upperX = lowerX + 4;
        // Right corner....
        if (mouseX >= lowerX && mouseX <= upperX) {
            hitShape = shape;
            break;
        }
    }

}

大约现在,我至少会创建 4 个方法,isWithinTopEdge , isWithinBottomEdge , isWithInLeftEdge , isWithInRightEdge , 将占据路径的边界,鼠标 Point和 margin 值,将返回 truefalse .这样您就可以简单地按照您想要的任何顺序混合支票。

你也可以使用 Path2D#contains它简化了检查,但你需要考虑软糖因素,但这只是一个想法......

关于java - 检测用户鼠标悬停在 JPanel 上 Path2D 形状的边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26446436/

相关文章:

java - Jboss 7 中的 GroovyScriptEngine 类加载

html - 在页面加载时播放的 CSS 悬停动画

java - 在不使用任何 API 的情况下,根据 LastName 对二维 String 数组进行排序,然后对 FirstName 进行排序

java - 仅映射来自 DTO 的特定字段 -> 使用 Map-Struct 的模型

java - Cassandra 1.1.1 无法删除 columnfamily

java - Graphics 和 Graphics2D 之间的区别?

java - 从不同的类调用 JPanel 时遇到问题

java - JMenu Action 监听器

css - Cufon h2 在 div 上悬停

css - Firefox 和 IE 中的悬停效果