2009-09-28 133 views
1

我使用鼠標拖動繪製三角形。 繪製了許多不同的三角形後,我想用鼠標拖動將一些三角形移動到不同的位置,但是我不知道如何將三角形拖到不同的位置。請幫助:=(如何使用鼠標拖動移動三角形

回答

2

彼得是對的,你問的最後時間(link

下面是一些psudocode闡述了他在說什麼。

 
    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 
+0

everytimes我拖着鼠標,新三角形繪製:-( – Jessy 2009-09-29 02:39:25

+0

我相信你忘記重畫你的繪畫方法中的背景,或者在它的頂部調用super.paint(g),或者用Graphics :: clearRect(x,y, w,h)繪製三角形之前。 – Gunslinger47 2009-09-29 05:06:33

+0

謝謝,現在問題解決了:-) – Jessy 2009-09-30 12:33:15