我想写一个程序,它允许我根据我的鼠标拖动多边形,它是初始mousePressed位置。代码可行,但问题在于它根本不敏感,每一个小鼠标移动都会提供更大的翻译。有没有办法改善它?这是代码:
public void mousePressed(MouseEvent e) {
//Get mousePressed coordinates
xMousePressed = e.getX();
yMousePressed = e.getY();
}
@Override
public void mouseDragged(MouseEvent e) {
//Drag polygon only if mouse was pressed in the center of the screen
if (270 < xMousePressed && xMousePressed < 520
&& 205 < yMousePressed && yMousePressed < 390){
//Create new mouse location, relative to the last one.
int xTemp = (int) ((e.getX() - xMousePressed));
int yTemp = (int) ((e.getY() - yMousePressed));
//Get list of all the polygons on screen.
java.util.List<Polygon> tempList = scene.getPolygons();
//For each point of each polygon
for(Polygon p: tempList){
for(int i = 0; i < p.npoints; i++){
//Move
p.xpoints[i] += xTemp;
p.ypoints[i] += yTemp;
}
}
//Update the mouse position for next movement
yMousePressed = e.getX();
yMousePressed = e.getY();
//Draw the changes on screen.
this.repaint();
}
}