views:

209

answers:

1

I draw a triangle using mouse drag. After drawing many different triangles, I wanted to move some of the triangles into different location using mouse drag, however I don't know how to drag the triangle to different location. Help please :=(

+1  A: 

Peter was right the last time you asked. (link)

Here's some psudocode to elaborate on what he was talking about:

  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
Gunslinger47
everytimes I dragged the mouse, new triangle drawn :-(
Jessy
I believe you're forgetting to repaint the background in your paint method. Either call super.paint(g) at the top of it, or draw a big rectangle with Graphics::clearRect(x,y,w,h) before drawing your triangles.
Gunslinger47
Thanks, the problem solved now :-)
Jessy