| 1 |
|
|---|
| 2 |
import java.awt.*; |
|---|
| 3 |
import java.awt.event.*; |
|---|
| 4 |
import java.awt.geom.*; |
|---|
| 5 |
import java.util.*; |
|---|
| 6 |
import javax.swing.*; |
|---|
| 7 |
import java.io.IOException; |
|---|
| 8 |
|
|---|
| 9 |
class GuiTest implements ActionListener, PointListener { |
|---|
| 10 |
|
|---|
| 11 |
JButton switchMapButton; |
|---|
| 12 |
JButton randomPathButton; |
|---|
| 13 |
JButton clearNodesButton; |
|---|
| 14 |
JGraphViewer graphViewer; |
|---|
| 15 |
Graph graph; |
|---|
| 16 |
|
|---|
| 17 |
TiledMap maps[]; |
|---|
| 18 |
int mapIndex = 0; |
|---|
| 19 |
|
|---|
| 20 |
GuiTest(String[] args) { |
|---|
| 21 |
|
|---|
| 22 |
graph = new Graph(); |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
maps = new TiledMap[2]; |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
maps[0] = new TiledMap(15, 13, 200, 200, AffineTransform.getScaleInstance(1.31,1.31)); |
|---|
| 30 |
maps[0].setTreeUri("images/uiucmap/"); |
|---|
| 31 |
maps[0].setExtension("png"); |
|---|
| 32 |
if(args.length > 0) { |
|---|
| 33 |
maps[0].setRootUri(args[0]); |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
Point2D.Double realPoints[] = { |
|---|
| 37 |
new Point2D.Double(1844.4800000000002, 465.05), |
|---|
| 38 |
new Point2D.Double(2445.77, 3461.0200000000004), |
|---|
| 39 |
new Point2D.Double(463.74, 3537.0000000000005) |
|---|
| 40 |
}; |
|---|
| 41 |
Point2D.Double imagePoints[] = { |
|---|
| 42 |
new Point2D.Double(1925.8119658119663, 553.1111111111112), |
|---|
| 43 |
new Point2D.Double(2533.643811573166, 3690.2649325233588), |
|---|
| 44 |
new Point2D.Double(460.7444267834348, 3762.0368672661193) |
|---|
| 45 |
}; |
|---|
| 46 |
|
|---|
| 47 |
AffineTransform arialTransform = AffineTransformFactory.createTransform(imagePoints, realPoints); |
|---|
| 48 |
|
|---|
| 49 |
System.out.println(arialTransform.getScaleX()); |
|---|
| 50 |
maps[1] = new TiledMap(20, 18, 200, 200, arialTransform); |
|---|
| 51 |
maps[1].setTreeUri("images/aerialmap/"); |
|---|
| 52 |
maps[1].setExtension("jpg"); |
|---|
| 53 |
if(args.length > 0) { |
|---|
| 54 |
maps[1].setRootUri(args[0]); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
for(int i = 0; i < realPoints.length; i++) { |
|---|
| 58 |
graph.addNode(new PhysicalNode(graph.getUnusedNodeID(), realPoints[i].getX(), realPoints[i].getY())); |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
graphViewer = new JGraphViewer(graph, 1/1.31*1, 1/1.31*1, maps[0]); |
|---|
| 62 |
graphViewer.setPreferredSize(new Dimension(400,400)); |
|---|
| 63 |
|
|---|
| 64 |
graphViewer.addPointListener(this); |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
graphViewer.setBackground(Color.BLACK); |
|---|
| 68 |
graphViewer.setDefaultNodePainter(new PhysicalNodePainter(Color.RED, 5)); |
|---|
| 69 |
graphViewer.setDefaultEdgePainter(new PhysicalEdgePainter(Color.DARK_GRAY, 4)); |
|---|
| 70 |
graphViewer.setSelectedNodePainter(new PhysicalNodePainter(Color.GREEN, 7)); |
|---|
| 71 |
graphViewer.setSelectedEdgePainter(new PhysicalEdgePainter(Color.GREEN, 7)); |
|---|
| 72 |
|
|---|
| 73 |
graphViewer.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5)); |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
switchMapButton = new JButton("Switch map"); |
|---|
| 77 |
switchMapButton.addActionListener(this); |
|---|
| 78 |
|
|---|
| 79 |
randomPathButton = new JButton("Find a random path"); |
|---|
| 80 |
randomPathButton.addActionListener(this); |
|---|
| 81 |
|
|---|
| 82 |
clearNodesButton = new JButton("Clear All Selected Nodes"); |
|---|
| 83 |
clearNodesButton.addActionListener(this); |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
JFrame frame = new JFrame("Pathways GUI Test"); |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
Container contentPane = frame.getContentPane(); |
|---|
| 93 |
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS)); |
|---|
| 94 |
contentPane.add(graphViewer); |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
JPanel buttonPane = new JPanel(); |
|---|
| 98 |
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); |
|---|
| 99 |
buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
|---|
| 100 |
buttonPane.add(switchMapButton); |
|---|
| 101 |
buttonPane.add(Box.createRigidArea(new Dimension(5, 5))); |
|---|
| 102 |
buttonPane.add(randomPathButton); |
|---|
| 103 |
buttonPane.add(Box.createRigidArea(new Dimension(5, 5))); |
|---|
| 104 |
buttonPane.add(clearNodesButton); |
|---|
| 105 |
buttonPane.add(Box.createHorizontalGlue()); |
|---|
| 106 |
|
|---|
| 107 |
contentPane.add(buttonPane); |
|---|
| 108 |
|
|---|
| 109 |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|---|
| 110 |
frame.pack(); |
|---|
| 111 |
frame.setVisible(true); |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
public void actionPerformed(ActionEvent e) { |
|---|
| 116 |
|
|---|
| 117 |
if(e.getSource().equals(switchMapButton)) { |
|---|
| 118 |
|
|---|
| 119 |
mapIndex = (mapIndex + 1) % maps.length; |
|---|
| 120 |
graphViewer.setBackgroundMap(maps[mapIndex]); |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
else if(e.getSource().equals(randomPathButton)) { |
|---|
| 124 |
graphViewer.setSelectedObject(null); |
|---|
| 125 |
showBestPath((PhysicalNode)graph.getRandomNode(), (PhysicalNode)graph.getRandomNode()); |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
else if ( e.getSource().equals(clearNodesButton) ) { |
|---|
| 129 |
graphViewer.clearAllSpecialNodes(); |
|---|
| 130 |
} |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
public PhysicalNode oldDestination; |
|---|
| 153 |
|
|---|
| 154 |
public void pointReceived(MouseEvent event, Point2D.Double point) { |
|---|
| 155 |
|
|---|
| 156 |
if(event.getID() == MouseEvent.MOUSE_PRESSED || event.getID() == MouseEvent.MOUSE_DRAGGED) { |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
if ( event.isShiftDown() && |
|---|
| 161 |
(graphViewer.getSelectedObject() instanceof PhysicalNode) ) |
|---|
| 162 |
{ |
|---|
| 163 |
PhysicalNode origin = (PhysicalNode)graphViewer.getSelectedObject(); |
|---|
| 164 |
PhysicalNode destination = graph.getClosestNode(point); |
|---|
| 165 |
if(destination.equals(oldDestination)) return; |
|---|
| 166 |
showBestPath(origin, destination); |
|---|
| 167 |
oldDestination = destination; |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
else |
|---|
| 174 |
{ |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
System.out.println("Real coordinates: (" + point.getX() + ", " + point.getY() + ")"); |
|---|
| 178 |
Point2D imagePoint = ((TiledMap)graphViewer.getBackgroundMap()).getRealToImage().transform(point, null); |
|---|
| 179 |
System.out.println("Image coordinates: (" + imagePoint.getX() + ", " + imagePoint.getY() + ")"); |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
graph.addNode(new PhysicalNode(graph.getUnusedNodeID(), point.getX(), point.getY())); |
|---|
| 183 |
graphViewer.graphChanged(); |
|---|
| 184 |
|
|---|
| 185 |
Object object = graphViewer.closestMatch(point, 0); |
|---|
| 186 |
|
|---|
| 187 |
graphViewer.setSelectedObject(object); |
|---|
| 188 |
} |
|---|
| 189 |
} |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
private void showBestPath(PhysicalNode origin, PhysicalNode destination) { |
|---|
| 193 |
|
|---|
| 194 |
AStarSearch userSearch = new AStarSearch(); |
|---|
| 195 |
userSearch.setOrigin(origin); |
|---|
| 196 |
userSearch.setDestination(destination); |
|---|
| 197 |
Path path = userSearch.computeBestPath(); |
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
graphViewer.setPath(path); |
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
graphViewer.clearCustomObjectPainters(); |
|---|
| 207 |
PhysicalNodePainter painter = new PhysicalNodePainter(Color.CYAN, 5); |
|---|
| 208 |
Iterator i = userSearch.getSearchedNodes().iterator(); |
|---|
| 209 |
while(i.hasNext()) { |
|---|
| 210 |
Node node = (Node)i.next(); |
|---|
| 211 |
if(!(node instanceof PhysicalNode)) continue; |
|---|
| 212 |
|
|---|
| 213 |
graphViewer.addCustomObjectPainter(node, 2, painter); |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
painter = new PhysicalNodePainter(Color.RED, 5); |
|---|
| 218 |
i = userSearch.getOpenNodes().iterator(); |
|---|
| 219 |
while(i.hasNext()) { |
|---|
| 220 |
Node node = (Node)i.next(); |
|---|
| 221 |
if(!(node instanceof PhysicalNode)) continue; |
|---|
| 222 |
|
|---|
| 223 |
graphViewer.addCustomObjectPainter(node, 2, painter); |
|---|
| 224 |
} |
|---|
| 225 |
|
|---|
| 226 |
graphViewer.addCustomObjectPainter(path.nodes.getLast(), 1, new PhysicalNodePainter(Color.MAGENTA, 5)); |
|---|
| 227 |
} |
|---|
| 228 |
|
|---|
| 229 |
public static void main(String[] args) { |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
final String[] finalArgs = args; |
|---|
| 234 |
javax.swing.SwingUtilities.invokeLater(new Runnable() { |
|---|
| 235 |
public void run() { |
|---|
| 236 |
new GuiTest(finalArgs); |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
} |
|---|
| 245 |
}); |
|---|
| 246 |
} |
|---|
| 247 |
} |
|---|