通过边连接jung中的顶点会导致创建另一个额外的顶点

时间:2014-12-20 17:27:23

标签: jung jung2

当我想连接两个已存在的顶点JUNG连接到顶点并创建一个额外的顶点时,我正在实现一个接口,用于在JUNG中创建,连接和着色顶点的命令,为什么?< / p>

这是我的连接方法代码:

public class Connect extends Command {
private CommandMaster cm;
private BehGraphUndirected behGraph;
private static int edgenumber=0;
@Override
public Object run(BehGraphUndirected behGraph, VisualizationImageServer panel, InterpretMaster interpretMaster, String... args) {

    System.out.print("connect Runs\n");
    this.cm = new CommandMaster();
    this.behGraph = behGraph;

    if(cm.exists(args[0]))
    {
        //got to another command
    }else
    {
        switch (args[0]) {
            case "edge":
                this.createEdge(args[1] , args[2]);
                break;
        }
    }
    interpretMaster.refreshAndRepaint();
    return null;

}
public void createEdge(String nodeName1 , String nodeName2)
{
    this.behGraph.addEdge(edgenumber++,nodeName1, nodeName2);
    System.out.println(this.behGraph.getVertexCount());
    System.out.println("edge between: "+nodeName1+" and "+ nodeName2+" added");
}

这是创建方法,以防你想知道我实现代码的方式:

package interpreter.command;

import GraphHandling.BehGraphUndirected;
import edu.uci.ics.jung.visualization.VisualizationImageServer;
import interpreter.Command;
import interpreter.CommandMaster;
import interpreter.InterpretMaster;


/**
*
* @author Administrator
*/
public class Create extends Command{
private CommandMaster cm;
private BehGraphUndirected behGraph;

@Override
public Object run(BehGraphUndirected behGraph, VisualizationImageServer panel, InterpretMaster interpretMaster, String... args) {
    System.out.print("create Runs \n");

    this.cm = new CommandMaster();
    this.behGraph = behGraph;

    if(cm.exists(args[0]))
    {
        //got to another command
    }else
    {
        switch (args[0]) {
            case "node":
                this.createNode(args[1]);
                break;
            case "label":
                this.createLabel(args[1]);
                break;
        }
    }
    interpretMaster.refreshAndRepaint();
    return null;
}
public void createNode(String nodeName)
{
    this.behGraph.addVertex(nodeName);
    System.out.print("vertex: "+nodeName+" added");
}

private void createLabel(String string) {

}
class str
{
    int i;
    long j;
}

}

连接两个节点之前和之后的图形图像:

enter image description here

enter image description here

这是我的BehGraphUndirected课程:

package GraphHandling;

import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import java.util.LinkedList;

/**
*
* @author Administrator
*/
public class BehGraphUndirected extends UndirectedSparseGraph{
private final LinkedList<Node> nodeList;

public BehGraphUndirected()
{ 
    this.nodeList = new LinkedList<>();
}
public void addNode(Node newNode)
{
   this.nodeList.add(newNode);
}

}

2 个答案:

答案 0 :(得分:1)

我已编译并测试了您的代码,Jung库似乎正常工作,它通过给予它的不同nodes来消除不同的objec看起来你有一些其他问题,就像processing the input strings中用作创建节点的对象的问题一样。

答案 1 :(得分:0)

你应该看看BehGraphUndirected正在做什么;它不是JUNG类或接口。

正在创建的顶点的名称是什么,以及它与传递给create方法的内容有什么关系?