2010-03-01 95 views
1

我正在做这些iTunes Stanford课程,并且我一直在学习Java的开始。事情进展顺利,但他们最近引入了事件 - 特别是MouseEvents。我一直在阅读本书中的章节,并通过示例代码,而有些东西只是没有为我点击右键......总是会有异步的东西给我带来麻烦:-D难以理解Java MouseEvent

此前,有些人提到我提到“addMouseListener”是Graphics导入中的类是很重要的。据我所知,这只是在画布上添加一个全屏鼠标监听器。

我对这件事还是很新的,所以我可能不会像我应该那样描述事物。

这是我为了更好地理解它而试图简化的一段代码。目前,它会建立一个红色的矩形,我可以点击它并沿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); 
    } 

回答

0

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

有一件事情可以帮助我处理w /动画和移动gui中的对象时,在纸上绘制了一些场景,并注意坐标如何变化。

+0

感谢您的回答。我有画布宽度。困难(对我而言)是动画只是将对象与其过去的坐标相关联 - 而不是与画布相关。 gobj.move(e.getX() - lastX,0);所以我一直在尝试像if(lastX <0){lastX = 0}这样的事情,但这只是在做与鼠标移动有关的时髦事情 - 而不是画布:( – Joel 2010-03-01 04:54:49

2

您的代码正在相对于鼠标的最后位置移动矩形。当你简单地移动东西时,这可以很好地工作,但是当你想要在边界停止时需要使用绝对定位。

// 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(); 
    } 
} 

,这可能不是很你想要的,因为一旦你熄灭的边缘,对象将停止移动,但随后当你移回到画布,你的桨将立即移动而不是等待鼠标达到与其开始的桨相同的相对位置。

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