简体   繁体   English

JAVA中的图形可视化

[英]Graph visualisation in JAVA

I need a program which generates a graph based on information contained in .txt file. 我需要一个程序,它根据.txt文件中包含的信息生成图表。 For example, if it is written 1,7 , 7,6 in file, program must construct a graph connecting 1st and 7th, and 7th and 6th nodes. 例如,如果是写1,7,7,6项文件,程序必须建立连结第1和第7,和第7和第6节点的图。 I think for the beginning I need to have 2 arrays/lists, say, X and Y containing the first and second values of pairs given in .txt file, respectively. 我认为一开始我需要有2个数组/列表,比如说XY分别包含.txt文件中给出的第一和第二对值。 I wrote a code for this but it gives different Exceptions which I cannot handle. 我为此编写了一个代码,但它提供了我无法处理的不同异常。 Here it is: 这里是:

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();  
}
}

If I am on a right way, please help me to fix the code above and give me advice about how to continue and how to convert into graph those lists. 如果我正确的方式,请帮助我修复上面的代码,并给我关于如何继续以及如何将这些列表转换为图表的建议。 Thanks in advance! 提前致谢!
PS I wrote a code which generates a graph using JUNG api, but problem is that I am including all node and edge information in code, while I need it to read from a .txt file. PS我编写了一个使用JUNG api生成图形的代码,但问题是我在代码中包含所有节点和边缘信息,而我需要它从.txt文件中读取。 Here is the code: 这是代码:

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);    
}

I would rather create an adjacency list. 我宁愿创建一个邻接列表。 The first list contains the "from" node, and the second list contains the ones it's connected to. 第一个列表包含“from”节点,第二个列表包含它连接的节点。

List<List<Integer>> adjacencyList = new ArrayList<>();

You need to initialize each list inside the list, so do this for all of the nodes you need. 您需要初始化列表中的每个列表,因此请对所需的所有节点执行此操作。

for (int i = 0; i < numberOfNodes; i++) {
  adjacencyList.add(new ArrayList<>());
}

When you want to add to the adjacency list, you do the following: 如果要添加到邻接列表,请执行以下操作:

adjacencyList.get(x).add(y);
adjacencyList.get(y).add(x); // Add this if you need an undirected graph

Then you can simply iterate through the list and draw a graph from there, for instance: 然后,您可以简单地遍历列表并从那里绘制图形,例如:

for (int i = 0; i < numberOfNodes; i++) {
  boolean[] visited = new boolean[numberOfNodes];
  for (int node : adjacencyList.get(i)) {
    ...
  }
}

You included the tag "JUNG" on your question. 您在问题中加入了“JUNG”标签。 Have you tried using the JUNG library? 你试过使用JUNG库吗? It includes types for representing graphs, reading them from a couple of different text file formats, and for visualizing them. 它包括用于表示图形的类型,从几种不同的文本文件格式中读取它们以及用于可视化它们的类型。

The distribution includes samples and documentation (and there are many questions on StackOverflow related to JUNG). 分发包括样本和文档(与JUNG相关的StackOverflow有很多问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM