Java 平移多边形位置
我想写一个程序,允许我根据我的鼠标拖动一个多边形,以及它的初始鼠标位置。代码是可以工作的,但问题是它一点也不敏感,而且每次鼠标移动都会产生更大的翻译。有什么办法可以改进吗?代码如下:Java 平移多边形位置,java,polygon,mousemove,polygons,Java,Polygon,Mousemove,Polygons,我想写一个程序,允许我根据我的鼠标拖动一个多边形,以及它的初始鼠标位置。代码是可以工作的,但问题是它一点也不敏感,而且每次鼠标移动都会产生更大的翻译。有什么办法可以改进吗?代码如下: public void mousePressed(MouseEvent e) { //Get mousePressed coordinates xMousePressed = e.getX(); yMousePressed = e.getY(); } @Override public vo
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();
}
}
public void鼠标按下(MouseEvent e){
//获取鼠标按下的坐标
xMousePressed=e.getX();
yMousePressed=e.getY();
}
@凌驾
公共无效鼠标标记(鼠标事件e){
//仅当鼠标在屏幕中心按下时才拖动多边形
如果(270
您可以使用2D shapes API,它可以直接转换形状,也可以将图形上下文转换为“更大的转换”你的意思是,如果你按下并向右移动10个像素,多边形移动超过10个像素?我的意思是,多边形的移动似乎与鼠标移动不相称。你可以使用2D shapes API,它可以直接转换形状,或者你可以将图形上下文转换为“更大的转换”你的意思是,如果你按下并向右移动10个像素,多边形移动超过10个像素?我的意思是,多边形的移动似乎与鼠标的移动不成比例。