2013-10-21 72 views
0

我是Java新手。我正在嘗試使用GUI來輸入和顯示加權二叉樹。要創建根節點,用戶點擊屏幕。隨後,他從一個點拖到另一個點創建一個邊和一個新節點。一旦他釋放了鼠標,他應該被提示輸入重量,然後顯示邊緣(顯示重量)和新節點。使用MouseListener繪製加權樹

在輸入中,雖然我能夠創建根,但當我嘗試拖動以創建下一個節點時,程序進入循環。

代碼如下。

class myPanel extends JPanel implements MouseListener, MouseMotionListener 
{ 
int node_is_present[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //1 if node exists, 0  otherwise 
int no_of_nodes=0; 
int node_coords[][]=new int[15][2]; //stores co-ordinates of each node 
int weight[]=new int[15]; //stores weight of line in the order entered 
int x1,x2,y1,y2; 
int tracker=-1; 
int SIZE=20; 

public void clear() 
{ 
    x1=0; 
    y1=0; 
    x2=0; 
    y2=0; 
} 

@Override 
public void mouseClicked(MouseEvent me) {} 

@Override 
public void mousePressed(MouseEvent me) //will store intial x,y 
{ 
     tracker=1; 
     x1=me.getX(); 
     y1=me.getY(); 
     repaint(); 
} 

@Override 
public void mouseReleased(MouseEvent me) //will store final x,y and update arrays 
{ 

    x2=me.getX(); 
    y2=me.getY(); 
    if(x1!=x2) 
    tracker=2; 

    node_is_present[no_of_nodes]=1; 
    node_coords[no_of_nodes][0]=me.getX(); 
    node_coords[no_of_nodes][1]=me.getY(); 
    no_of_nodes++; 
    repaint(); 
} 

@Override 
public void mouseEntered(MouseEvent me){} 
@Override 
public void mouseExited(MouseEvent me) {} 
@Override 
public void mouseDragged(MouseEvent me){} 
@Override 
public void mouseMoved(MouseEvent me) {}   


public void paintComponent(Graphics g) 
{ 
    if(tracker==1 && no_of_nodes==0) //for root node 
    { 
     g.drawOval(x1-(SIZE/2),y1-(SIZE/2),SIZE,SIZE); 
     g.drawString(String.valueOf(no_of_nodes+1),x1-(SIZE/2),y1-(SIZE/2)); 
    } 

    else if(tracker==2 && no_of_nodes>0) 
    { 
     g.drawLine(x1 -(SIZE/2) , y1 -(SIZE/2) , x2 -(SIZE/2) , y2 -(SIZE/2)); 
     g.drawOval(x2-(SIZE/2),y2-(SIZE/2),SIZE,SIZE); 
     g.drawString(String.valueOf(no_of_nodes+1),x2-(SIZE/2),y2-(SIZE/2)); 

     String str=JOptionPane.showInputDialog("Enter the weight of the edge: "); 
     int w=Integer.parseInt(str); 
     weight[no_of_nodes-1]=w; 

     g.drawString(String.valueOf(w),Math.round((x1+x2)/2),Math.round((y1+y2)/2)); 
     clear(); 

    } 
} 

myPanel() 
{ 
    this.setSize(1500, 500); 
    addMouseListener(this); 
    addMouseMotionListener(this); 

    clear(); 
} 
} 


public class tree1 
{ 

public static void main(String[] args) { 
    JFrame frame=new JFrame(); 
    myPanel panel=new myPanel(); 
    frame.setSize(1500, 800); 
    frame.setLocation(0, 0); 
    frame.setTitle("Tree"); 
    frame.setBounds(100,100,1500,800); 
    frame.add(panel); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 
} 
+0

什麼是$的?另外你的'paintComponent(...)'方法似乎在其中有程序邏輯 - 'clear()'。這不應該。考慮創建併發布證明你的問題的[sscce](http://sscce.org)。此外,請記住將這個大問題分解爲小問題,在嘗試將所有內容合併之前解決每個小問題與其他問題分離。 –

+0

$ - 格式問題,修復它。 clear()不是paintcomponent的一部分 - 它是上面定義的一個單獨的函數,它將所有co-ords的值重置爲0. – user2226041

+0

我知道,但是您從* paintComponent中的*調用它。同樣,不要這樣做,因爲您沒有完全控制是否調用paintComponent。 –

回答

2

Swing中的繪畫可能隨時出於任何原因。正如@HoverCraftFullOfEels已經指出的那樣,你應該在任何paint方法中都沒有應用程序,這些應該只包含繪製你的組件所需的功能。

開始採取看看Performing Custom PaintingPainting in AWT and Swing爲更多細節油漆子系統

基本上,你paintComponent內,撥打super.paintComponent第一和刪除,特別是else塊。

創建一些模型,它包含所需的數據,然後使用paintComponent方法繪製該模型。