我需要一個基於.txt文件中包含的信息生成圖形的程序。例如,如果在文件中寫入1,7,7,6,則程序必須構建連接第1和第7以及第7和第6節點的圖形。 我想一開始我需要有2個數組/列表,比如說,分別是X和Y,它們分別包含在.txt文件中給出的第一對和第二對值。我爲此編寫了一個代碼,但它給出了我無法處理的不同異常。那就是:JAVA中的圖形可視化
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReaddFile {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader((new FileReader("my/path/here")));
String line="";
List<Integer> list_x = new ArrayList<Integer>();
List<Integer> list_y = new ArrayList<Integer>();
while((line=br.readLine())!=null){
String[] t = line.split(",");
int x = Integer.parseInt(t[0]);
int y = Integer.parseInt(t[1]);
list_x.add(x);
list_y.add(y);
}
br.close();
}
}
如果我是一個正確的方式,請幫我解決上面的代碼,並給我關於如何繼續,如何轉換成圖形這些名單的建議。提前致謝!
P.S.我編寫了一個使用JUNG API生成圖形的代碼,但問題是我在代碼中包含所有節點和邊緣信息,而我需要它從.txt文件讀取。這裏是代碼:
import edu.uci.ics.jung.algorithms.layout.*;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import java.awt.*;
import javax.swing.JFrame;
public class GraphVis {
Graph<Integer, String> g;
public GraphVis() {
g = new SparseMultigraph<Integer, String>();
g.addVertex((Integer) 1);
g.addVertex((Integer) 2);
g.addVertex((Integer) 3);
g.addVertex((Integer) 4);
g.addVertex((Integer) 5);
g.addVertex((Integer) 6);
g.addVertex((Integer) 7);
g.addVertex((Integer) 8);
g.addVertex((Integer) 9);
g.addVertex((Integer) 10);
g.addVertex((Integer) 11);
g.addVertex((Integer) 12);
g.addVertex((Integer) 13);
g.addVertex((Integer) 14);
g.addEdge("1", 1,2);
g.addEdge("2", 2, 6);
g.addEdge("3", 2, 6);
g.addEdge("4", 2, 7);
g.addEdge("5", 2, 7);
g.addEdge("6", 2,12);
g.addEdge("7", 2,3);
g.addEdge("8", 2,3);
g.addEdge("9", 3,4);
g.addEdge("10", 3,4);
g.addEdge("11", 3,6);
g.addEdge("12", 3,6);
g.addEdge("13", 3,7);
g.addEdge("14", 3,7);
g.addEdge("15", 3,12);
g.addEdge("16", 4,5);
g.addEdge("17", 4,13);
g.addEdge("18", 4,13);
g.addEdge("19", 4,8);
g.addEdge("20", 4,12);
g.addEdge("21", 4,12);
g.addEdge("22", 4,7);
g.addEdge("23", 4,7);
g.addEdge("24", 4,6);
g.addEdge("25", 4,6);
g.addEdge("26", 5,12);
g.addEdge("27", 5,11);
g.addEdge("28", 5,11);
g.addEdge("29", 5,13);
g.addEdge("30", 5,8);
g.addEdge("31", 5,8);
g.addEdge("32", 5,9);
g.addEdge("33", 5,9);
g.addEdge("34", 5,9);
g.addEdge("35", 5,14);
g.addEdge("36", 5,14);
g.addEdge("37", 5,10);
g.addEdge("38", 5,10);
g.addEdge("39", 6,12);
g.addEdge("40", 6,12);
g.addEdge("41", 6,12);
g.addEdge("42", 6,7);
g.addEdge("43", 6,7);
}
public static void main(String[] args) {
GraphVis sgv = new GraphVis();
Layout<Integer, String> layout = new CircleLayout(sgv.g);
layout.setSize(new Dimension(600,600));
VisualizationViewer<Integer,String> vv = new
VisualizationViewer<Integer,String>(layout);
vv.setPreferredSize(new Dimension(750,750));
JFrame frame = new JFrame("Graph View ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
}
檢查此答案:http://stackoverflow.com/questions/43507283/directed-graph-initialization/43508282#43508282 – sirandy
謝謝。有沒有辦法可視化輸出並繪製圖形? –
輸出與App類中的調用graph.printGraph()一起顯示。要獲得圖形的圖形輸出,您可以使用像jGraphT(http://jgrapht.org/)或jgraphx(https://github.com/jgraph/jgraphx)這樣的庫,並且總是很難:使它有揮杆的裸手。 – sirandy