简体   繁体   English

一次完全移动2D形状JAVA

[英]Moving 2d shapes altogether at once JAVA

I have a java project that I can't quite seem to figure out right now. 我有一个Java项目,现在似乎还不太清楚。

It wants us to create a composite object made up of 2D shapes from a package called Wheels. 它希望我们从名为Wheels的包中创建一个由2D形状组成的复合对象。

This composite object needs to take in mouseEvent s in order to move together when clicked and dragged on. 此复合对象需要加入mouseEvent ,以便在单击和拖动时一起移动。 I've already completed this part but the next part of the assignment requires us to have one part of that object to be independently drag-able such that when you drag it, the whole composite object does not move. 我已经完成了这一部分,但是作业的下一部分要求我们使该对象的一部分可独立拖动,这样,当您拖动它时,整个复合对象就不会移动。

Although when you drag the composite object, the independent object should move the same amount maintain position relative to the composite object. 尽管在拖动复合对象时,独立对象应相对于复合对象移动相同量的位置。

Here's the class that I think would need some working on. 我认为这是需要进行一些练习的课程。

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);
}
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

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM