我需要你的幫助:) 我有一個類主營:傳遞的主要方法創建另一個類的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