1 回答

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊
有人可以告訴我從哪里開始嗎?
父面板需要知道子面板之間的關(guān)系。
一種方法是保持跟蹤ArrayList組件對(duì)之間的關(guān)系。然后,您需要重寫paintChildren(...)父面板的方法以在兩個(gè)子面板之間繪制一條線。
您在父面板類中定義 ArrayList:
private ArrayList<Component> relationships = new ArrayList<Component>();
然后根據(jù)需要將組件對(duì)添加到 ArrayList:
relationships.add( component1a );
relationships.add( component1b );
基本的繪畫代碼是:
@Override
protected void paintChildren(Graphics g)
{
for (int i = 0; i < relationships.size(); i += 2)
{
Component one = relationships.get(i);
Component two = relationships.get(i + 1);
Point p1 = //calculate the center of component one
Point p2 = //calculate the center of component two
g.drawline(p1.x, p1.y, p2.x, p2.y);
}
super.paintChildren(g);
}
因此,上面的代碼應(yīng)該在添加到 ArrayList 的每對(duì)組件的中心點(diǎn)之間繪制線條。然后,子面板將繪制在線條的頂部,以便線條看起來像是從每個(gè)組件的邊緣出來的。
查看trashgod 的GraphPanel示例。此示例支持拖動(dòng)形狀,并且線條將跟隨形狀。
添加回答
舉報(bào)