我需要你的帮助:) 我有一个类主营:传递的主要方法创建另一个类的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 )有数据!
不幸的是我没有真正的线索你的问题和你的问题是什么。你看,如何将一个数组列表传递给另一个对象......这个问题非常微不足道(通过它)?但我真的不知道你在问什么。提示:将您的问题降至最低。不要从你的“完整例子”开始;带走**任何**,这不是你正试图解决的问题的一部分。所以不要谈论绘画,绘画,无论如何。 – GhostCat
它看起来像你在整个应用程序中使用可变静态字段。这是不好的设计......不要这样做:) –
使用构造函数参数来传递列表。 – Thomas