java.awt.Polygon
을 사용하여 자바로 폴리곤을 그렸습니다. 마우스로 다각형을 움직이기를 원합니다 (드래그하고 싶습니다). 나는 에 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