0
我使用java.awt.Polygon
在Java中繪製了一個多邊形。我想用鼠標移動多邊形(我想拖動它)。我知道我必須在addMouseMotionListener
中使用mouseDragged
方法。這樣我就可以知道鼠標拖動多邊形的路徑的座標(x,y)。如何拖動多邊形?
但問題是,我不知道如何處理採集的(x,y)來移動多邊形。這是代碼的一部分:
public void mouseListeners(DrawEverything det) {
det.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
if(isMouseInMe(evt.getX(), evt.getY())){//this "if" checks if the cursor is in the shape when we drag it
int xTmep , yTemp ;
xTmep = (int) (evt.getX() - xMousePressed) ;//xMousePressed--> the x position of the mouse when pressed on the shape
yTemp = (int) (evt.getY() - yMousePressed) ;
for(int i = 0 ; i < nPoints ; ++i){
xPoints[i] += xTmep;//array of x-positions of the points of polygon
yPoints[i] += yTemp;
}
}
}
});
這部分是我遇到的麻煩主要部分:
for(int i = 0 ; i < nPoints ; ++i){
xPoints[i] += xTmep;
yPoints[i] += yTemp;
}
非常感謝你,它像一個魅力工作。 – gandalf