【问题标题】:Difficulty understanding Java MouseEvent难以理解 Java MouseEvent
【发布时间】:2010-03-02 13:51:23
【问题描述】:

我正在上这些 iTunes 斯坦福课程,并且我一直在学习 Java 入门。事情进展顺利,但他们最近引入了事件,特别是 MouseEvents。我一直在阅读书中的章节,并仔细阅读示例代码,但有些东西对我来说并不正确……总是那些异步的东西给我带来了麻烦:-D

之前,有些人提到我提到“addMouseListener”是图形导入中的一个类很重要。据我所知,这只是在画布上添加了一个毯子鼠标侦听器。

我对此仍然很陌生,所以我可能没有像我应该的那样描述事情。

这是我一直在尝试简化的一段代码,以便更好地理解它。目前,它将构建一个红色矩形,我可以单击它并沿 x 轴拖动它。太好了!!!

import java.awt.*;
import java.awt.event.*;
import acm.graphics.*;
import acm.program.*;

/** This class displays a mouse-draggable rectangle and oval */

public class DragObject extends GraphicsProgram {

    /* Build a rectangle  */
    public void run() {

        GRect rect = new GRect(100, 100, 150, 100);
        rect.setFilled(true);
        rect.setColor(Color.RED);
        add(rect);
        addMouseListeners();
    }

/** Called on mouse press to record the coordinates of the click */
    public void mousePressed(MouseEvent e) {
        lastX = e.getX();
        lastY = e.getY();
        gobj = getElementAt(lastX, lastY);
    }

/** Called on mouse drag to reposition the object */
    public void mouseDragged(MouseEvent e) { 

            if((lastX) > 100){
            gobj.move(e.getX() - lastX, 0);

            lastX = e.getX();
            lastY = e.getY();
        }
    }

/** Called on mouse click to move this object to the front */
    public void mouseClicked(MouseEvent e) {
        if (gobj != null) gobj.sendToFront();
    }

/* Instance variables */
private GObject gobj;   /* The object being dragged */
private double lastX;   /* The last mouse X position */
private double lastY;   /* The last mouse Y position */
}

如果我将鼠标拖离画布,我希望矩形留在画布内,而不是移开它(如果您在鼠标按钮仍然移动到滚动区域之外,水平滚动条的行为相同点击)。我怎样才能做到这一点?

我一直在尝试这些方法,但它无法正常工作:

    if  ( ( lastX > (getWidth() - PADDLE_WIDTH) ) || ( lastX < PADDLE_WIDTH ) ) {
        gobj.move(0, 0);
    } else {
        gobj.move(e.getX() - lastX, 0);
    }

【问题讨论】:

    标签: java


    【解决方案1】:

    您的代码正在相对于鼠标的最后一个位置移动矩形。当您只是移动物体时,这很好用,但是当您希望它在边界处停止时,您需要使用绝对定位。

    // When the mouse is pressed, calculate the offset between the mouse and the rectangle
    public void mousePressed(MouseEvent e) {
        lastX = e.getX();
        lastY = e.getY();
        gobj = getElementAt(lastX, lastY);
    }
    
    public void mouseDragged(MouseEvent e) { 
            double newX;
    
            // Assuming you can get the absolute X position of the object.
            newX = gobj.getX() + e.getX() - lastX;
            // Limit the range to fall within your canvas. Adjust for your paddle width as necessary.
            newX = Math.max( 0, Math.min( newX, getWidth() ) );
            // Set the new position of the paddle, assuming you can set the absolute position.
            gobj.setX( newX );
    
            lastX = e.getX();
            lastY = e.getY();
        }
    }
    

    这可能不是你想要的,因为一旦你离开边缘,对象就会停止移动,但是一旦你移回画布,你的桨会立即移动,而不是等待鼠标到达与它开始时的桨的相对位置相同。

    你可以尝试让它做你想做的事。

    【讨论】:

      【解决方案2】:

      为了做到这一点,你需要知道 Canvas 对象的宽度,我相信会有一个方法可以提供这个值。然后,您可以根据画布的宽度检查 MouseEvent 的当前 x 位置,一旦超过画布的宽度,就不会增加形状对象的 x 坐标。根据您希望在画布中保留多少形状,您可能还需要考虑形状对象的宽度。

      在 gui 中处理动画和移动对象时,对我有帮助的一件事是在纸上画出一些场景,并注意坐标如何变化。

      【讨论】:

      • 感谢您的回答。我确实有画布宽度。困难(对我来说)是动画只是相对于它过去的坐标移动对象,而不是相对于画布。 gobj.move(e.getX() - lastX, 0);所以我一直在尝试 if(lastX
      猜你喜欢
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 2014-11-15
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多