一次移动2d形状JAVA

时间:2012-09-28 23:20:53

标签: java

我有一个java项目,我现在似乎无法弄清楚。

它希望我们创建一个由名为Wheels的包中的2D形状组成的复合对象。

此复合对象需要接收mouseEvent以便在单击和拖动时一起移动。我已经完成了这部分,但是分配的下一部分要求我们让该对象的一部分可以独立拖动,这样当你拖动它时,整个复合对象不会移动。

虽然拖动复合对象时,独立对象应相对于复合对象移动相同数量的维护位置。

这是我认为需要一些工作的课程。

public Character(Color _color1, Color _color2)
{
    _indPart1 = new IndPart(); // head
    _charPart2 = new CharacterPart(this); // body
    _charPart3 = new CharacterPart(this); //left arm
    _charPart4 = new CharacterPart(this); //right arm
    this.setColor(_color1, _color2);
    _indPart1.setSize(50, 50);
    _charPart2.setSize(50, 100);
    _charPart3.setSize(75, 35);
    _charPart4.setSize(75, 35);
}

public void setLocation(int x, int y)
{
    _x = x;
    _y = y;
    //_indPart1.setLocation((_x - _otherPointx) + (x - 300), (_y - _otherPointy) + (y - 250)); // head
    _indPart1.setLocation(x, y - 50); // head
    _charPart2.setLocation(x, y); //body
    _charPart3.setLocation(x + 51, y); // right arm
    _charPart4.setLocation(x - 76, y); // left arm
}

@Override
public void mousePressed(MouseEvent e)
{
    _prevPoint = e.getPoint();
}

@Override
public void mouseDragged(MouseEvent e) 
{
    _currPoint = e.getPoint();
    //_otherPointx = _indPart1.getXLocation();
    //_otherPointy = _indPart1.getYLocation();
    _diffx = _currPoint.x - _prevPoint.x;
    _diffy = _currPoint.y - _prevPoint.y;
    this.setLocation(_x + _diffx, _y + _diffy);
    _prevPoint = _currPoint;
    //_otherPointx = _currPoint.x;
    //_otherPointy = _currPoint.y;
}

public void setColor(Color c1, Color c2)
{
    _indPart1.setColor(c1);
    _charPart2.setColor(c2);
    _charPart3.setColor(c1);
    _charPart4.setColor(c1);
}

1 个答案:

答案 0 :(得分:0)

public void mouseDragged(MouseEvent e)
{

    if(_indPart1.contains(e.getPoint()))
    {

        // do your stuff with the draggable part and leave the whole component still

    }else {

        // drag the whole component

    }

}