2012-09-28 38 views
1

我有一個Java項目,我現在無法完全弄清楚。一次完全移動2D圖形JAVA

它希望我們可以從名爲Wheels的包中創建由2D形狀組成的複合對象。

此複合對象需要在mouseEvent s中才能在點擊並拖動時一起移動。我已經完成了這部分,但下一部分任務要求我們讓該對象的一部分獨立拖拽,以便在拖拽時整個複合對象不會移動。

儘管當您拖動複合對象時,獨立對象應相對於複合對象移動相同數量的維護位置。

這是我認爲需要一些工作的類。

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

你怎麼有人點擊了複合材料和部分區分? – MadProgrammer

回答

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 

    } 

}