鼠标拖动逻辑

发布于 2024-10-25 04:57:46 字数 859 浏览 1 评论 0原文

下面是一些使 GCompound 通过拖动正确移动的代码。

public void mouseDragged(MouseEvent e) {
    currentLabel.setLocation(e.getX()+DX, e.getY()+DY);
}
public void mousePressed(MouseEvent e) { 
    currentLabel = (coolLabel) getElementAt(e.getX(),e.getY());
    DX = currentLabel.getX()-e.getX();
    DY = currentLabel.getY()-e.getY();
    currentLabel.sendToFront();
}

这是没有正确移动的情况:

public void mouseDragged(MouseEvent e)
{
    currentLabel.move(e.getX()+DX, e.getY()+DY);
}

public void mousePressed(MouseEvent e) { 
    currentLabel = (coolLabel) getElementAt(e.getX(),e.getY());
    DX = -e.getX();
    DY = -e.getY();
    currentLabel.sendToFront();
}

有人能解释为什么会这样吗?似乎我的代码的两个版本都使用相同的思维...基本上在第一种情况下,我采用原始的 GObject 并根据与原始位置的差异设置其位置,在第二种情况下,我根据与原始位置的差异移动原始对象。

Here is some code which makes a GCompound move correctly with dragging.

public void mouseDragged(MouseEvent e) {
    currentLabel.setLocation(e.getX()+DX, e.getY()+DY);
}
public void mousePressed(MouseEvent e) { 
    currentLabel = (coolLabel) getElementAt(e.getX(),e.getY());
    DX = currentLabel.getX()-e.getX();
    DY = currentLabel.getY()-e.getY();
    currentLabel.sendToFront();
}

Here it is without it moving correctly:

public void mouseDragged(MouseEvent e)
{
    currentLabel.move(e.getX()+DX, e.getY()+DY);
}

public void mousePressed(MouseEvent e) { 
    currentLabel = (coolLabel) getElementAt(e.getX(),e.getY());
    DX = -e.getX();
    DY = -e.getY();
    currentLabel.sendToFront();
}

Can someone explain why this is so? Seems both versions of my code are using the same thinking... Basically in the first case I take the original GObject and set its location based on the difference from my original position, and in the second case I move the original object based on the difference from my original position.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

快乐很简单 2024-11-01 04:57:47

move()setLocation() 的已弃用同义词。

一个不起作用而另一个起作用的原因是您向其中输入了不同的参数。 (特别是在第二种情况下,您总是调用 move(0,0)。)

java.awt.Component 源代码中的片段证实了这一点:

public void setLocation(int x, int y) {
    move(x, y);
}

@Deprecated
public void move(int x, int y) {
    synchronized(getTreeLock()) {
        setBoundsOp(ComponentPeer.SET_LOCATION);            
        setBounds(x, y, width, height);
    }
}

move() is a deprecated synonym of setLocation().

The reason one doesn't work and the other does is that you feed different parameters into it. (Specifically in the second case you always call move(0,0).)

A snippet from the java.awt.Component source code confirms this:

public void setLocation(int x, int y) {
    move(x, y);
}

@Deprecated
public void move(int x, int y) {
    synchronized(getTreeLock()) {
        setBoundsOp(ComponentPeer.SET_LOCATION);            
        setBounds(x, y, width, height);
    }
}
放我走吧 2024-11-01 04:57:47

第一个示例相对于当前标签位置设置 DXDY,而第二个示例则忽略标签当前位置的当前位置。

例如,如果您当前的标签位置是 (50,50),并且您想要将其移动 (30,30)。如果您在指向 (70,70) 时按下鼠标(假设它在标签边界内):

在第一个示例中,DX 将设置为 -20,DY 将设置为 -20。现在,拖动后,新位置将是(如 mouseDragged 实现的) ((70+30)-20, (70+30)-20) = (80, 80),这是正确的。

在第二个示例中,DX 将设置为 -70,DY 也将设置为 -70。现在,拖动后,新位置将为 (100-70, 100-70) = (30,30),这是错误的。

While the first example sets DX and DY relatively to your current label location, the second example ignores the current location of the current location of your label.

For example, if your current label location is (50,50) and you want to move it by (30,30). if you press the mouse when pointing to (70,70) (assuming it's within the label boundries):

In your first example DX will be set to -20 and DY will be set to -20. Now, after the drag the new location will be (as mouseDragged implements) ((70+30)-20, (70+30)-20) = (80, 80) which is correct.

In your second example DX will be set to -70, as well as DY. Now, after the drag, the new location will be (100-70, 100-70) = (30,30) which is wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文