当我想在两个节点之间添加一条边以通过或失败时,我可以很容易地做到这一点,但是验证存在问题,如果我们再次在相同的两个节点之间创建一条边,我不想要。我只需要私有类 ConnectAction 在相同的两个节点之间创建一条边。我在那里尝试了一些东西,但它不起作用。如果我们删除迭代器并只创建它被创建的边缘。
这是节点类
private static class Node {
private Point p;
private int r;
private String NAME;
private String nodeid;
private boolean selected = false;
private Rectangle b = new Rectangle();
/**
* Construct a new node.
*/
public Node(String nodeid,Point p, int r,String NAME) {
this.nodeid=nodeid;
this.p = p;
this.r = r;
this.NAME = NAME;
setBoundary(b);
}
/**
* Calculate this node's rectangular boundary.
*/
private void setBoundary(Rectangle b) {
b.setBounds(p.x - r, p.y - r, 2 * r, 2 * r);
}
/**
* Draw this node.
*/
public void draw(Graphics g) {
g.setColor(Color.white);
g.fillRect(b.x, b.y, b.width, b.height);
g.setColor(Color.black);
g.drawString(NAME,b.x+25,b.y+40);
if (selected) {
g.setColor(Color.darkGray);
g.drawRect(b.x+3, b.y+3, b.width, b.height);
}
}
/**
* Return this node's location.
*/
public Point getLocation() {
return p;
}
/**
* Return true if this node contains p.
*/
public boolean contains(Point p) {
return b.contains(p);
}
/**
* Return true if this node is selected.
*/
public boolean isSelected() {
return (selected);
}
/**
* Mark this node as selected.
*/
public void setSelected(boolean selected) {
this.selected = selected;
}
/**
* Collected all the selected nodes in list.
*/
public static void getSelected(List<Node> list, List<Node> selected) {
selected.clear();
for (Node n : list) {
if (n.isSelected()) {
selected.add(n);
}
}
}
/**
* Select no nodes.
*/
public static void selectNone(List<Node> list) {
for (Node n : list) {
n.setSelected(false);
}
}
/**
* Select a single node; return true if not already selected.
*/
public static boolean selectOne(List<Node> list, Point p) {
for (Node n : list) {
if (n.contains(p)) {
if (!n.isSelected()) {
Node.selectNone(list);
n.setSelected(true);
}
return true;
}
}
return false;
}
/**
* Select each node in r.
*/
public static void selectRect(List<Node> list, Rectangle r) {
for (Node n : list) {
n.setSelected(r.contains(n.p));
}
}
/**
* Toggle selected state of each node containing p.
*/
public static void selectToggle(List<Node> list, Point p) {
for (Node n : list) {
if (n.contains(p)) {
n.setSelected(!n.isSelected());
}
}
}
/**
* Update each node's position by d (delta).
*/
public static void updatePosition(List<Node> list, Point d) {
for (Node n : list) {
if (n.isSelected()) {
n.p.x += d.x;
n.p.y += d.y;
n.setBoundary(n.b);
}
}
}
}
这是边缘类
private static class Edge{
private Node n1;
private Node n2;
private String Ctype;
private String eid;
private static String edgeid;
public Edge(String eid,Node n1, Node n2, String Ctype) {
this.eid = eid;
this.n1 = n1;
this.n2 = n2;
this.Ctype=Ctype;
}
public void draw(Graphics g) {
Point p1 = n1.getLocation();
Point p2 = n2.getLocation();
g.setColor(Color.darkGray);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
g.drawString(Ctype,(p1.x+p2.x)/2,(p1.y+p2.y)/2);
}
}
并且在这里面临的问题是在同一对节点之间仅创建一条边。
private class ConnectAction extends AbstractAction {
String ConnectionType;
public ConnectAction(String name) {
super(name);
this.ConnectionType=name;
}
public void actionPerformed(ActionEvent e) {
Node.getSelected(nodes, selected);
if (selected.size() > 1) {
for (int i = 0; i < 1; ++i) {
Node n3 = selected.get(i);
Node n4 = selected.get(i + 1);
String a= n3.nodeid;
String b= n4.nodeid;
// System.out.println(a);
//System.out.println(b);
//System.out.println(Edgeid);
ListIterator<Edge> iter = edges.listIterator();
while (iter.hasNext()) {
Edge e1 = iter.next();
if (!(n3.nodeid == e1.n1.nodeid && n4.nodeid == e1.n2.nodeid) || (n4.nodeid == e1.n1.nodeid && n3.nodeid == e1.n2.nodeid)) {
String Edgeid=ID.giveid();
Edge ed=new Edge(Edgeid,n3, n4,ConnectionType);
edges.add(ed);
} iter.remove();
}
}
}
repaint();
}
}