java - JAVA 一次性移动 2D 形状

标签 java

我有一个java项目,我现在似乎不太明白。

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

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

虽然当您拖动复合对象时,独立对象应该移动相同的量以保持相对于复合对象的位置。

这是我认为需要一些工作的类(class)。

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

    }

}

关于java - JAVA 一次性移动 2D 形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12648526/

相关文章:

java - MySQL jdbc 驱动程序 ClassNotFoundException : com. mysql.jdbc.Driver

java - 动态 JSON 传递给 REST Web 服务并使用它将数据插入 mysql 数据库

java - 如何在键为null和setNumReduceTasks(0)的同时在MapReduce程序中为分隔符分配空白空间

java - android list 中的多个应用程序

java - 在 Java 中计算反函数

java - 服务器端 API 中的权限检查

Java反射,使用GetDeclaredField时忽略大小写

java - 如何使用 XStream 将对象列表转换为 XML 文档

java - 如果在命令行中提供了新值,如何覆盖从属性文件加载的值

java - StringReader 从 String.Split 输出中给出错误