2016-03-02 35 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

相关问题