-
Notifications
You must be signed in to change notification settings - Fork 1
/
GraphPanel.java
205 lines (190 loc) · 6.15 KB
/
GraphPanel.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
A panel to draw a graph
*/
public class GraphPanel extends JComponent
{
/**
Constructs a graph panel.
@param aToolBar the tool bar with the node and edge tools
@param aGraph the graph to be displayed and edited
*/
public GraphPanel(ToolBar aToolBar, Graph aGraph)
{
toolBar = aToolBar;
graph = aGraph;
setBackground(Color.WHITE);
addMouseListener(new
MouseAdapter()
{
public void mousePressed(MouseEvent event)
{
Point2D mousePoint = event.getPoint();
Node n = graph.findNode(mousePoint);
Edge e = graph.findEdge(mousePoint);
Object tool = toolBar.getSelectedTool();
if (tool == null) // select
{
if (e != null)
{
selected = e;
}
else if (n != null)
{
selected = n;
dragStartPoint = mousePoint;
dragStartBounds = n.getBounds();
}
else
{
selected = null;
}
}
else if (tool instanceof Node)
{
Node prototype = (Node) tool;
Node newNode = (Node) prototype.clone();
boolean added = graph.add(newNode, mousePoint);
if (added)
{
selected = newNode;
dragStartPoint = mousePoint;
dragStartBounds = newNode.getBounds();
}
else if (n != null)
{
selected = n;
dragStartPoint = mousePoint;
dragStartBounds = n.getBounds();
}
}
else if (tool instanceof Edge)
{
if (n != null) rubberBandStart = mousePoint;
}
lastMousePoint = mousePoint;
repaint();
}
public void mouseReleased(MouseEvent event)
{
Object tool = toolBar.getSelectedTool();
if (rubberBandStart != null)
{
Point2D mousePoint = event.getPoint();
Edge prototype = (Edge) tool;
Edge newEdge = (Edge) prototype.clone();
if (graph.connect(newEdge,
rubberBandStart, mousePoint))
selected = newEdge;
}
validate();
repaint();
lastMousePoint = null;
dragStartBounds = null;
rubberBandStart = null;
}
});
addMouseMotionListener(new
MouseMotionAdapter()
{
public void mouseDragged(MouseEvent event)
{
Point2D mousePoint = event.getPoint();
if (dragStartBounds != null)
{
if (selected instanceof Node)
{
Node n = (Node) selected;
Rectangle2D bounds = n.getBounds();
n.translate(
dragStartBounds.getX() - bounds.getX()
+ mousePoint.getX() - dragStartPoint.getX(),
dragStartBounds.getY() - bounds.getY()
+ mousePoint.getY() - dragStartPoint.getY());
}
}
lastMousePoint = mousePoint;
repaint();
}
});
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Rectangle2D bounds = getBounds();
Rectangle2D graphBounds = graph.getBounds(g2);
graph.draw(g2);
if (selected instanceof Node)
{
Rectangle2D grabberBounds = ((Node) selected).getBounds();
drawGrabber(g2, grabberBounds.getMinX(), grabberBounds.getMinY());
drawGrabber(g2, grabberBounds.getMinX(), grabberBounds.getMaxY());
drawGrabber(g2, grabberBounds.getMaxX(), grabberBounds.getMinY());
drawGrabber(g2, grabberBounds.getMaxX(), grabberBounds.getMaxY());
}
if (selected instanceof Edge)
{
Line2D line = ((Edge) selected).getConnectionPoints();
drawGrabber(g2, line.getX1(), line.getY1());
drawGrabber(g2, line.getX2(), line.getY2());
}
if (rubberBandStart != null)
{
Color oldColor = g2.getColor();
g2.setColor(PURPLE);
g2.draw(new Line2D.Double(rubberBandStart, lastMousePoint));
g2.setColor(oldColor);
}
}
/**
Removes the selected node or edge.
*/
public void removeSelected()
{
if (selected instanceof Node)
{
graph.removeNode((Node) selected);
}
else if (selected instanceof Edge)
{
graph.removeEdge((Edge) selected);
}
selected = null;
repaint();
}
/**
Draws a single "grabber", a filled square
@param g2 the graphics context
@param x the x coordinate of the center of the grabber
@param y the y coordinate of the center of the grabber
*/
public static void drawGrabber(Graphics2D g2, double x, double y)
{
final int SIZE = 5;
Color oldColor = g2.getColor();
g2.setColor(PURPLE);
g2.fill(new Rectangle2D.Double(x - SIZE / 2,
y - SIZE / 2, SIZE, SIZE));
g2.setColor(oldColor);
}
public Dimension getPreferredSize()
{
Rectangle2D bounds
= graph.getBounds((Graphics2D) getGraphics());
return new Dimension(
(int) bounds.getMaxX(),
(int) bounds.getMaxY());
}
private Graph graph;
private ToolBar toolBar;
private Point2D lastMousePoint;
private Point2D rubberBandStart;
private Point2D dragStartPoint;
private Rectangle2D dragStartBounds;
private Object selected;
private static final Color PURPLE = new Color(0.7f, 0.4f, 0.7f);
}