因此,我正在尝试为矩形实现拖动以调整大小的功能。
因此,我想要的是当用户将鼠标悬停在矩形的右下角角 上时,将光标更改为双向箭头。 (就像您调整 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 值,将返回 true
或 false
.这样您就可以简单地按照您想要的任何顺序混合支票。
你也可以使用 Path2D#contains
它简化了检查,但你需要考虑软糖因素,但这只是一个想法......
关于java - 检测用户鼠标悬停在 JPanel 上 Path2D 形状的边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26446436/