2016-03-02 69 views
0

我需要你的幫助:) 我有一個類主營:傳遞的主要方法創建另一個類的ArrayList

public class Main { 

    private static ArrayList<Integer> xCoords = new ArrayList<Integer>(); 
    private static ArrayList<Integer> yCoords = new ArrayList<Integer>(); 


public static void main(String[] args) { 

    Adress adress2 = new Adress(6, 8); 
    TourManager.addAdress(adress2); 
    Adress adress3 = new Adress(3, 4); 
    TourManager.addAdress(adress3); 
    Adress adress4 = new Adress(9, 12); 
    TourManager.addAdress(adress4); 
    Adress adress5 = new Adress(12, 16); 
    TourManager.addAdress(adress5); 

     // Initialize intial solution 

    Tour currentSolution = new Tour(); 

    currentSolution.generateIndividual(); 
    Tour best = new Tour(currentSolution.getTour()); 
    System.out.println("Distance From adress (0,0): " + best.getDistance()); 
    System.out.println("Solution distance : " + best.toString()+ "\n"); 

     // i try to sent x and y Coordinates in my too ArraList xCoords, yCoords list 
     // in the oder to be used in the second class to draw line. 

     for (int i = 0; i < best.tourSize(); i++) { 
       xCoords.add(i, best.getAdress(i).x); 
       yCoords.add(i, best.getAdress(i).y); 
     } 

     for (int i = 0; i < best.tourSize(); i++) { 
       System.out.print(yCoords.get(i)+ " - "); 
     } 

     new Draw(); // new instance 

} 

海爾我的第二類畫線:

public class Draw extends JPanel { 


    //private static ArrayList<Integer> xCoords = new ArrayList<Integer>(); 
    //private static ArrayList<Integer> yCoords = new ArrayList<Integer>(); 
    // how can i recover the tow ArrayList xCoords, yCoords in the order to make a loop for to exract x,y 


      public void paintComponent(Graphics g) 
      { 

      final int offset = 5; 

      super.paintComponent(g); 
      g.setColor(new Color(54,34,56)); 

      for(int i = 1; i <xCoords.size() ; i++) 
      { 
       g.fillOval(xCoords.get(i), yCoords.get(i), 8, 8); 
       g.setColor(Color.RED); 
       g.drawLine(xCoords.get(i-1)+offset, yCoords.get(i-1)+ offset, xCoords.get(i)+ offset, yCoords.get(i)+ offset); 
      } 

      } 

    public Draw() { 

     final int width = 400; // Breite des Fensters 
     final int height = 450; // Hoehe des Fensters 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setSize(width, height); 
     f.add(this); 
     f.setVisible(true); 
    } 

} 

我試圖在Draw類中實例化Tour,創建一個Loop來在ArrayList中添加x,y座標,然後創建另一個Loop來擴展x,y來繪製一條線,但在我的遊覽中我沒有數據,我應該重新創建Adress(x,y )有數據!

+0

不幸的是我沒有真正的線索你的問題和你的問題是什麼。你看,如何將一個數組列表傳遞給另一個對象......這個問題非常微不足道(通過它)?但我真的不知道你在問什麼。提示:將您的問題降至最低。不要從你的「完整例子」開始;帶走**任何**,這不是你正試圖解決的問題的一部分。所以不要談論繪畫,繪畫,無論如何。 – GhostCat

+0

它看起來像你在整個應用程序中使用可變靜態字段。這是不好的設計......不要這樣做:) –

+0

使用構造函數參數來傳遞列表。 – Thomas

回答

1

如果您想將某些東西傳遞給另一個類,只需編寫該類的構造函數併爲要傳遞的內容添加參數。

在你的情況下,將水木清華這樣的:

public class Draw extends JPanel { 
... 
    public Draw(ArrayList<Integer> xCoords, ArrayList<Integer> yCoords) { 
     // your code here. You can access the arraylists by the parameter name yCoords and xCoords 
    } 
... 
} 

之後,你只需要創建一個類的對象繪製並通過您的陣列。

希望這會有所幫助!

+0

謝謝Krossi,這真的是我的預期,但我有一個問題** paintComponent(Graphics g)**'xCoord.size()'是空的,當我嘗試使System.out.println(xCoord.size ))在我的構造函數之外的這個方法,例如它的工作原理! – Mah54

+0

我使用了一個構造函數參數來傳遞列表,但是我的繪製方法有問題。我真的很感激,如果有人能夠解釋(簡單地說)爲什麼我的座標似乎爲空。我嘗試給出一個單獨的例子,以便我可以更好地理解)
Mah54

+0

如果您使用上面的代碼,則錯誤在構造函數調用中。 '新Draw();'它可能是'新Draw(xCoords,yCoords);'之後你的Draw類知道xCoords-Arraylist。如果您想稍後使用相同的繪圖對象,請將該實例保存在變量中。 '繪製myDrawVariableName ='。 – Krossi

相關問題