gpt4 book ai didi

java - 鼠标拖动缩放实现 Java - 不是点击

转载 作者:行者123 更新时间:2023-12-01 10:16:38 25 4
gpt4 key购买 nike

我编写了一些代码来在 JPanel 上实现拖动缩放(即用鼠标绘制一个矩形,然后放大到该区域,使其填充容器)。但是,在我将其写入监听器之前,我有一个“mouseClicked”方法,该方法仍然存在。此方法执行不同的功能,只是返回单击的坐标。

不幸的是,我错误地使用 MousePressed 实现了缩放功能,因此它与 mouseClicked 事件同时运行。这意味着当我点击鼠标获取坐标时,它也会放大一个非常小的矩形。有办法改变这个吗?

预先感谢您的任何建议,如果您需要任何澄清,请告诉我!

这是监听器类:

类 panelClickListener 扩展 MouseAdapter 实现 MouseMotionListener {

    JLabel toShowCoords, rangeLabel;
juliaPanel panelJulia;
mandPanel panelMand;
int startX, startY, currentX, currentY;
boolean dragging;
ComplexNumber startRange, endRange;

public panelClickListener(JLabel toShowCoordsIn, juliaPanel panelJuliaIn, mandPanel panelMandIn, JLabel rangeLabelIn) {
toShowCoords = toShowCoordsIn;
panelJulia = panelJuliaIn;
panelMand = panelMandIn;
rangeLabel = rangeLabelIn;
}

public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
userSelectedPoint = pointsToDrawOn[x][y];
toShowCoords.setText("Number Selected: " + userSelectedPoint.getReal() + " , " + userSelectedPoint.getImaginary());
panelJulia.setFixedNumber(pointsToDrawOn[x][y]);
panelJulia.repaint();
}

public void mousePressed(MouseEvent event) {
Point point = event.getPoint();
startX = point.x;
startY = point.y;
dragging = true;
}

public void mouseReleased(MouseEvent event) {
dragging = false;
startRange = pointsToDrawOn[startX][startY];
endRange = pointsToDrawOn[currentX][currentY];
maxVal = endRange;
minVal = startRange;
rangeLabel.setText("Real axis: " + minVal.getReal() + "," + maxVal.getReal() + " - Imaginary axis: " + minVal.getImaginary() + "," + maxVal.getImaginary());
populatePointArray();
panelMand.repaint();
}

public void mouseDragged(MouseEvent event) {
Point p = event.getPoint();
currentX = p.x;
currentY = p.y;
}

}

最佳答案

dragging 仅应在调用 mouseDragged 时设置为 true,然后在调用 mouseReleased 时,您可以在处理缩放之前需要检查拖动是否为true

有一些理论可以证明,只有当 mousePressedmouseReleased 的鼠标点相同时才会调用 mouseClicked

因此,在点击操作中,您可以期望调用 mousePressedmouseReleasedmouseClicked (我相信这个顺序,但您'最好测试一下)。

在拖动操作中,您可以预期调用 mousePressedmouseDraggedmouseReleased

关于java - 鼠标拖动缩放实现 Java - 不是点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35852740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com