-
Notifications
You must be signed in to change notification settings - Fork 1
/
Graph.java
180 lines (159 loc) · 3.93 KB
/
Graph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import java.util.*;
import java.util.List;
/**
A graph consisting of selectable nodes and edges.
*/
public abstract class Graph implements Serializable
{
/**
Constructs a graph with no nodes or edges.
*/
public Graph()
{
nodes = new ArrayList<Node>();
edges = new ArrayList<Edge>();
}
/**
Adds an edge to the graph that joins the nodes containing
the given points. If the points aren't both inside nodes,
then no edge is added.
@param e the edge to add
@param p1 a point in the starting node
@param p2 a point in the ending node
*/
public boolean connect(Edge e, Point2D p1, Point2D p2)
{
Node n1 = findNode(p1);
Node n2 = findNode(p2);
if (n1 != null && n2 != null)
{
e.connect(n1, n2);
edges.add(e);
return true;
}
return false;
}
/**
Adds a node to the graph so that the top left corner of
the bounding rectangle is at the given point.
@param n the node to add
@param p the desired location
*/
public boolean add(Node n, Point2D p)
{
Rectangle2D bounds = n.getBounds();
n.translate(p.getX() - bounds.getX(),
p.getY() - bounds.getY());
nodes.add(n);
return true;
}
/**
Finds a node containing the given point.
@param p a point
@return a node containing p or null if no nodes contain p
*/
public Node findNode(Point2D p)
{
for (int i = nodes.size() - 1; i >= 0; i--)
{
Node n = nodes.get(i);
if (n.contains(p)) return n;
}
return null;
}
/**
Finds an edge containing the given point.
@param p a point
@return an edge containing p or null if no edges contain p
*/
public Edge findEdge(Point2D p)
{
for (int i = edges.size() - 1; i >= 0; i--)
{
Edge e = edges.get(i);
if (e.contains(p)) return e;
}
return null;
}
/**
Draws the graph
@param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
for (Node n : nodes)
n.draw(g2);
for (Edge e : edges)
e.draw(g2);
}
/**
Removes a node and all edges that start or end with that node
@param n the node to remove
*/
public void removeNode(Node n)
{
for (int i = edges.size() - 1; i >= 0; i--)
{
Edge e = edges.get(i);
if (e.getStart() == n || e.getEnd() == n)
edges.remove(e);
}
nodes.remove(n);
}
/**
Removes an edge from the graph.
@param e the edge to remove
*/
public void removeEdge(Edge e)
{
edges.remove(e);
}
/**
Gets the smallest rectangle enclosing the graph
@param g2 the graphics context
@return the bounding rectangle
*/
public Rectangle2D getBounds(Graphics2D g2)
{
Rectangle2D r = null;
for (Node n : nodes)
{
Rectangle2D b = n.getBounds();
if (r == null) r = b;
else r.add(b);
}
for (Edge e : edges)
r.add(e.getBounds(g2));
return r == null ? new Rectangle2D.Double() : r;
}
/**
Gets the node types of a particular graph type.
@return an array of node prototypes
*/
public abstract Node[] getNodePrototypes();
/**
Gets the edge types of a particular graph type.
@return an array of edge prototypes
*/
public abstract Edge[] getEdgePrototypes();
/**
Gets the nodes of this graph.
@return an unmodifiable list of the nodes
*/
public List<Node> getNodes()
{
return Collections.unmodifiableList(nodes); }
/**
Gets the edges of this graph.
@return an unmodifiable list of the edges
*/
public List<Edge> getEdges()
{
return Collections.unmodifiableList(edges);
}
private ArrayList<Node> nodes;
private ArrayList<Edge> edges;
}