如何使用鼠标拖动来移动三角形

时间:2009-09-28 23:05:21

标签: java graphics drag-and-drop mouse-listeners

我使用鼠标拖动画一个三角形。 在绘制了许多不同的三角形之后,我想使用鼠标拖动将一些三角形移动到不同的位置,但是我不知道如何将三角形拖动到不同的位置。求助:=(

1 个答案:

答案 0 :(得分:2)

你最后一次问,彼得是对的。 (link

这里有一些假代码来详细说明他在说什么:

  on mouse down:
    for each triangle, in reverse order of their drawing
      if the pointer is within the triangle's area
        set the "dragging" state in a member variable
        add a reference to the triangle to a member variable
        record the mouse position in a member variable
        record the initial position of the triangle in a member variable
        break
      end if
    end for


  on mouse move:
    if currently in the "dragging" state
      move the triangle by the same amount the mouse has moved
    end if


  on mouse up:
    if currently in the "dragging" state
      move the triangle by the same amount as the mouse has moved
      exit the "dragging" state
    end if