为什么每次点击都会移动相同的圆圈,无论距离是多少?

时间:2018-06-10 22:06:00

标签: java awt mouselistener mousemotionlistener

我正在制作一个体育游戏设计模拟器,其中有一个"教练"添加了一个播放器列表,您可以使用mouseClickedmouseDraggedmouseReleased在屏幕上移动它们。它计算最接近的"播放器"通过距离公式确定哪个圆圈移动"但是,无论我点击的坐标如何,移动的第一个圆圈是唯一的圆圈移动,即使有另一个圆圈更近。我想知道你们是否都可以帮助我:

public void mousePressed(MouseEvent e)
{
    xCoord = e.getX();
    yCoord = e.getY();
    //circleCenter = new Point((int)starterList.get(position).getPoint().getX(), (int)starterList.get(position).getPoint().getY());
    //circleCoords.add(circleCenter);
    //points = new ArrayList<Point>();
    for(i = 0; i < starterList.size(); i++)
    {
        closest = 3000;
        double difOfXes = starterList.get(position).getX() - xCoord;
        double difOfYes = starterList.get(position).getY() - yCoord;
        actualClosest = Math.sqrt(difOfXes * difOfXes + difOfYes * difOfYes);

        if(actualClosest < closest)
        {
            closest = actualClosest;
            position = i;
        }
    }
}

public void mouseDragged(MouseEvent e)    
{ 
    //Point circleClosest = circleCoords.get(position);

    starterList.get(position).setLocation(new Point(e.getX(), e.getY()));
    Point p = new Point(e.getX(), e.getY());
    starterList.get(position).setPoint(p);
    //starterList.get(position).getPoint().move((int) e.getX(), (int)e.getY());
    //set the coords of circleList at position= e.getx
    //points.add(p);
    //g.drawOval(p.getX(), p.getY(), 20,20);
    repaint();
}

public void mouseReleased(MouseEvent e)
{
    Point release = new Point(e.getX(), e.getY());
    //int circleCenterLoc = circleCoords.indexOf(circleCenter);
    starterList.get(position).setPoint(release);
    //playersPoints.add(points);
    //playerIDs.add(position);
}

0 个答案:

没有答案