简体   繁体   English

使用Java检测垂直/水平鼠标移动

[英]Detecting vertical/horizontal mouse movement with java

I'm writing a program in java that has graphical objects that the user can select and then rotate or stretch based on mouse movement. 我正在用Java编写一个程序,该程序具有图形对象,用户可以选择这些对象,然后根据鼠标移动对其进行旋转或拉伸。 I'm close to getting to work, but the problem I'm having is figuring out a way for the adjustments to the shape to work naturally with the mouse movements. 我快要开始工作了,但是我遇到的问题是想办法通过鼠标移动自然地调整形状。

I have a MouseListener set up something like this: 我有一个MouseListener设置如下:

private class MouseHandler extends MouseAdapter
{
    public void mousePressed(MouseEvent e)
    {
         currentClickPoint = e.getPoint();
    }
}

and a MouseMotionListener set up something like this: 和MouseMotionListener设置如下:

private class MouseMotionHandler extends MouseMotionAdapter
{
    public void mouseDragged(MouseEvent e)
    {
         objectRotateAngle = currentClickPoint.getY() - e.getY();
         objectWidth += currentClickPoint.getX() - e.getX();
    }
}

This is a simplified version, obviously, but the problem is that I want the width to increase when the mouse is moved right, and decrease when the mouse is moved left, as well as the rotate angle to increase/decrease based on vertical mouse movement. 显然,这是简化版本,但是问题是我希望鼠标右移时宽度增加,而鼠标左移时宽度减少,并且希望垂直鼠标移动时旋转角度增加/减小。 Right now, the width won't begin to decrease until you've passed the currentClickPoint's X position going left and vise versa. 现在,直到您通过了currentClickPoint的X位置(向左移动),宽度才开始减小。 The issue with the rotating based on mouse Y movement is that every time you begin to move the mouse up to rotate, the angle is set back to 0. 基于鼠标Y移动的旋转问题是,每次您开始向上移动鼠标进行旋转时,角度都将重新设置为0。

The program needs to respond to a change in direction seamlessly, without setting the width or angle to 0 at the start of the adjustment. 程序需要无缝地响应方向变化,而无需在调整开始时将宽度或角度设置为0。 Does anyone have a better way of detecting the direction of mouse movement for this purpose? 有人为此目的有更好的检测鼠标移动方向的方法吗?

我建议存储原始鼠标位置(即,用户首次能够调整形状大小时的位置),然后通过直接添加当前位置和原始位置之间的差异或缩放比例来修改形状的宽度。差异,然后将其添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM