java 加边_Java:JGraphT使用循环添加边

是否可以通过使用循环将边添加到图形?我正在解析一个字符串,以确定适当的边缘和标签.由于某种原因,它只会为循环遍历String的while循环的第一轮添加边缘.对于所有其他消息,将显示以下消息…

警告:由于基础JGraphT图拒绝创建边缘,因此删除了边缘.当违反基础图的约束时,例如尝试向图添加平行边或自环以禁止它们时,可能会发生这种情况.为避免出现此消息,请确保使用合适的基础JGraphT图.

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

import org.jgraph.*;

import org.jgraph.graph.*;

import org.jgrapht.*;

import org.jgrapht.ext.*;

import org.jgrapht.graph.*;

// resolve ambiguity

import org.jgrapht.graph.DefaultEdge;

public class JGraphAdapterDemo

extends JApplet

{

private static final long serialVersionUID = 3256444702936019250L;

private static final Color DEFAULT_BG_COLOR = Color.decode("#FAFBFF");

private static final Dimension DEFAULT_SIZE = new Dimension(530, 320);

static ListenableGraph g =

new ListenableDirectedMultigraph(String.class);

static int [] finalStates = new int[10];

static int startState = 0;

static char tran = ' ';

static int endState = 0;

private JGraphModelAdapter jgAdapter;

public static void main(String [] args)

{

JGraphAdapterDemo applet = new JGraphAdapterDemo();

applet.init();

JFrame frame = new JFrame();

frame.getContentPane().add(applet);

frame.setTitle("JGraphT Adapter to JGraph Demo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setVisible(true);

}

public void init()

{

// create a JGraphT graph

ListenableGraph g =

new ListenableDirectedMultigraph(String.class);

// create a visualization using JGraph, via an adapter

jgAdapter = new JGraphModelAdapter(g);

JGraph jgraph = new JGraph(jgAdapter);

adjustDisplaySettings(jgraph);

getContentPane().add(jgraph);

resize(DEFAULT_SIZE);

int numStates = 4;

int numSymbols;

int currentState;

int i = 0;

String input = "4 2 0 2 -1 0 a 1 1 b 3 2 c 2 3 c 3 -1"; //place input String here

int readInt = 0;

int j = 0;

String str = "";

int place = 0;

String fState;

//read in numStates, numSymbols, initialState

i=0;

//parse string

i = input.indexOf(" ",0);

str = input.substring(0,i); //number of states

numStates = Integer.parseInt(str); //convert to int

for(int k = 0; k< numStates; k++){

g.addVertex("q"+k);

}

i++;

j = i;

i=input.indexOf(" ",j);

str = input.substring(j,i); //number of symbols

numSymbols = Integer.parseInt(str);

i++;

j = i;

i=input.indexOf(" ",j);

str = input.substring(j,i); //initial state

currentState = Integer.parseInt(str);

i++;

j = i;

//read in finalStates

while(readInt!=-1){

i=input.indexOf(" ",j);

fState = input.substring(j,i); //a final state

readInt = Integer.parseInt(fState);

if(readInt!=-1){

finalStates[place] = readInt;

i++;

j = i;

place++;

}//end if

}//end while

i++;

j = i;

String sState;

String eState;

while(startState!=-1&& j

i=input.indexOf(" ",j);

sState = input.substring(j,i); //start state

startState = Integer.parseInt(sState); //convert to int

if(startState!=-1){

i++;

j = i;

String cStr = "";

tran = input.charAt(i); //transition

cStr = cStr + tran;

i = i+2;

j=i;

i=input.indexOf(" ",j);

eState = input.substring(j,i); //end state

endState = Integer.parseInt(eState);

i++;

j=i;

String one = "q"+startState;

String two = "q"+endState;

System.out.println(one+ two +" "+cStr);

g.addEdge(one, two, cStr);

//drawEdge(one, two, cStr);

}//end if

}//end while

}

public static void drawEdge(String v, String v1, String label){

System.out.println(v +" "+v1+ " "+label);

g.addEdge(v,v1,label);

}

private void adjustDisplaySettings(JGraph jg)

{

jg.setPreferredSize(DEFAULT_SIZE);

Color c = DEFAULT_BG_COLOR;

String colorStr = null;

try {

colorStr = getParameter("bgcolor");

} catch (Exception e) {

}

if (colorStr != null) {

c = Color.decode(colorStr);

}

jg.setBackground(c);

}

@SuppressWarnings("unchecked") // FIXME hb 28-nov-05: See FIXME below

private void positionVertexAt(Object vertex, int x, int y)

{

DefaultGraphCell cell = jgAdapter.getVertexCell(vertex);

AttributeMap attr = cell.getAttributes();

Rectangle2D bounds = GraphConstants.getBounds(attr);

Rectangle2D newBounds =

new Rectangle2D.Double(

x,

y,

bounds.getWidth(),

bounds.getHeight());

GraphConstants.setBounds(attr, newBounds);

// TODO: Clean up generics once JGraph goes generic

AttributeMap cellAttr = new AttributeMap();

cellAttr.put(cell, attr);

jgAdapter.edit(cellAttr, null, null, null);

}

//~ Inner Classes ----------------------------------------------------------

/**

* a listenable directed multigraph that allows loops and parallel edges.

// */

private static class ListenableDirectedMultigraph

extends DefaultListenableGraph

implements DirectedGraph

{

private static final long serialVersionUID = 1L;

ListenableDirectedMultigraph(Class edgeClass)

{

super(new DirectedMultigraph(edgeClass));

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值