| 1 |
import java.util.*; |
|---|
| 2 |
import java.awt.Graphics; |
|---|
| 3 |
import javax.swing.*; |
|---|
| 4 |
import javax.swing.event.*; |
|---|
| 5 |
import java.awt.geom.*; |
|---|
| 6 |
import java.awt.*; |
|---|
| 7 |
import java.awt.event.*; |
|---|
| 8 |
import java.io.*; |
|---|
| 9 |
import java.net.*; |
|---|
| 10 |
|
|---|
| 11 |
public class AppletTest extends JApplet implements PointListener { |
|---|
| 12 |
JGraphViewer graphViewer; |
|---|
| 13 |
JSlider slider; |
|---|
| 14 |
Graph graph; |
|---|
| 15 |
JButton switchMapButton; |
|---|
| 16 |
|
|---|
| 17 |
JCheckBox showAllNodesBox; |
|---|
| 18 |
JCheckBox busEnableBox; |
|---|
| 19 |
|
|---|
| 20 |
JLabel busRouteName; |
|---|
| 21 |
|
|---|
| 22 |
boolean showAllNodes = false; |
|---|
| 23 |
boolean busEnable = false; |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
int mapIndex = -1; |
|---|
| 28 |
|
|---|
| 29 |
public void init() { |
|---|
| 30 |
try { |
|---|
| 31 |
javax.swing.SwingUtilities.invokeAndWait(new Runnable() { |
|---|
| 32 |
public void run() { |
|---|
| 33 |
createGui(); |
|---|
| 34 |
} |
|---|
| 35 |
}); |
|---|
| 36 |
} catch (Exception e) { |
|---|
| 37 |
e.printStackTrace(); |
|---|
| 38 |
System.err.println("createGUI didn't successfully complete"); |
|---|
| 39 |
} |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
public void createGui() { |
|---|
| 44 |
|
|---|
| 45 |
Graph g = new Graph(); |
|---|
| 46 |
|
|---|
| 47 |
graphViewer = new JGraphViewer(null, 1/1.31*1, 1/1.31*1, null); |
|---|
| 48 |
graphViewer.addPointListener(this); |
|---|
| 49 |
graphViewer.setBackground(Color.BLACK); |
|---|
| 50 |
graphViewer.setDefaultNodePainter(new PhysicalNodePainter(Color.RED, 5)); |
|---|
| 51 |
graphViewer.setDefaultEdgePainter(new PhysicalEdgePainter(Color.DARK_GRAY, 4)); |
|---|
| 52 |
graphViewer.setSelectedNodePainter(new PhysicalNodePainter(Color.GREEN, 7)); |
|---|
| 53 |
graphViewer.setSelectedEdgePainter(new PhysicalEdgePainter(Color.GREEN, 7)); |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
Dictionary sliderLabels = new Hashtable(); |
|---|
| 58 |
sliderLabels.put(new Integer(0), new JLabel("Fit")); |
|---|
| 59 |
sliderLabels.put(new Integer(100), new JLabel("1X")); |
|---|
| 60 |
sliderLabels.put(new Integer(200), new JLabel("2X")); |
|---|
| 61 |
sliderLabels.put(new Integer(300), new JLabel("3X")); |
|---|
| 62 |
sliderLabels.put(new Integer(400), new JLabel("4X")); |
|---|
| 63 |
|
|---|
| 64 |
slider = new JSlider(0, 400, 100); |
|---|
| 65 |
slider.setLabelTable(sliderLabels); |
|---|
| 66 |
slider.setPaintLabels(true); |
|---|
| 67 |
slider.addChangeListener(new ChangeListener(){ |
|---|
| 68 |
public void stateChanged(ChangeEvent e) { |
|---|
| 69 |
graphViewer.setTemporaryLowQuality(((JSlider)e.getSource()).getValueIsAdjusting()); |
|---|
| 70 |
graphViewer.scaleRelative(((JSlider)e.getSource()).getValue() / 100.0); |
|---|
| 71 |
} |
|---|
| 72 |
}); |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
switchMapButton = new JButton("Switch map"); |
|---|
| 78 |
switchMapButton.addActionListener(new ActionListener() { |
|---|
| 79 |
public void actionPerformed(ActionEvent e) { |
|---|
| 80 |
switchMap(); |
|---|
| 81 |
} |
|---|
| 82 |
}); |
|---|
| 83 |
|
|---|
| 84 |
showAllNodesBox = new JCheckBox("Show all nodes", true); |
|---|
| 85 |
showAllNodesBox.addActionListener(new ActionListener() { |
|---|
| 86 |
public void actionPerformed(ActionEvent e) { |
|---|
| 87 |
showAllNodes = ((JCheckBox)e.getSource()).isSelected(); |
|---|
| 88 |
graphViewer.getDefaultNodePainter().setVisible(showAllNodes); |
|---|
| 89 |
graphViewer.getDefaultEdgePainter().setVisible(showAllNodes); |
|---|
| 90 |
graphViewer.graphChanged(); |
|---|
| 91 |
} |
|---|
| 92 |
}); |
|---|
| 93 |
|
|---|
| 94 |
showAllNodesBox.doClick(); |
|---|
| 95 |
|
|---|
| 96 |
busEnableBox = new JCheckBox("Enable buses", false); |
|---|
| 97 |
busEnableBox.addActionListener(new ActionListener() { |
|---|
| 98 |
public void actionPerformed(ActionEvent e) { |
|---|
| 99 |
busEnable = ((JCheckBox)e.getSource()).isSelected(); |
|---|
| 100 |
} |
|---|
| 101 |
}); |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
Container contentPane = getContentPane(); |
|---|
| 106 |
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS)); |
|---|
| 107 |
contentPane.add(graphViewer); |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
JPanel buttonPane = new JPanel(); |
|---|
| 111 |
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); |
|---|
| 112 |
buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
|---|
| 113 |
|
|---|
| 114 |
buttonPane.add(switchMapButton); |
|---|
| 115 |
|
|---|
| 116 |
buttonPane.add(Box.createRigidArea(new Dimension(5, 5))); |
|---|
| 117 |
buttonPane.add(showAllNodesBox); |
|---|
| 118 |
buttonPane.add(Box.createRigidArea(new Dimension(5, 5))); |
|---|
| 119 |
buttonPane.add(busEnableBox); |
|---|
| 120 |
buttonPane.add(Box.createRigidArea(new Dimension(15, 15))); |
|---|
| 121 |
buttonPane.add(busRouteName = new JLabel()); |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
contentPane.add(slider); |
|---|
| 125 |
contentPane.add(buttonPane); |
|---|
| 126 |
|
|---|
| 127 |
loadMap(); |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
public void loadMap() { |
|---|
| 131 |
try { |
|---|
| 132 |
graph = Graph.loadFromStream((new URL(getCodeBase().toString() + "uiuc.pathwaysmap")).openStream()); |
|---|
| 133 |
Iterator i = graph.getBackgroundMaps().iterator(); |
|---|
| 134 |
while(i.hasNext()) { |
|---|
| 135 |
((TiledMap)i.next()).setRootUri(getDocumentBase().toString()); |
|---|
| 136 |
} |
|---|
| 137 |
graphViewer.setGraph(graph); |
|---|
| 138 |
switchMap(); |
|---|
| 139 |
|
|---|
| 140 |
} catch(Exception e) { |
|---|
| 141 |
e.printStackTrace(); |
|---|
| 142 |
} |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
public void pointReceived(MouseEvent event, Point2D.Double point) { |
|---|
| 146 |
PhysicalNode clicked = graph.getClosestNode(point); |
|---|
| 147 |
Object selected = graphViewer.getSelectedObject(); |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
if(event.getID() == MouseEvent.MOUSE_CLICKED) { |
|---|
| 151 |
|
|---|
| 152 |
if(selected instanceof PhysicalNode && |
|---|
| 153 |
(graphViewer.getPath() == null || event.isShiftDown()) && |
|---|
| 154 |
clicked != null) { |
|---|
| 155 |
|
|---|
| 156 |
showBestPath((PhysicalNode)selected, clicked); |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
else { |
|---|
| 162 |
|
|---|
| 163 |
graphViewer.setPath(null); |
|---|
| 164 |
graphViewer.clearCustomObjectPainters(); |
|---|
| 165 |
graphViewer.setSelectedObject(clicked); |
|---|
| 166 |
} |
|---|
| 167 |
} else |
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
if(event.getID() == MouseEvent.MOUSE_DRAGGED) { |
|---|
| 171 |
|
|---|
| 172 |
if(event.isShiftDown()) { |
|---|
| 173 |
|
|---|
| 174 |
if(selected instanceof PhysicalNode && clicked != null) |
|---|
| 175 |
showBestPath((PhysicalNode)selected, clicked); |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
else { |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
} |
|---|
| 185 |
} |
|---|
| 186 |
lastPoint = point; |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
Point2D.Double lastPoint; |
|---|
| 190 |
|
|---|
| 191 |
private void showBestPath(PhysicalNode origin, PhysicalNode destination) { |
|---|
| 192 |
|
|---|
| 193 |
if(origin == destination) return; |
|---|
| 194 |
AStarSearch userSearch = new AStarSearch(); |
|---|
| 195 |
userSearch.setOrigin(origin); |
|---|
| 196 |
userSearch.setDestination(destination); |
|---|
| 197 |
userSearch.setBusEnable(busEnable); |
|---|
| 198 |
Path path = userSearch.computeBestPath(); |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
graphViewer.setPath(path); |
|---|
| 202 |
|
|---|
| 203 |
String outputText = ""; |
|---|
| 204 |
Iterator j = path.nodes.iterator(); |
|---|
| 205 |
boolean first = true; |
|---|
| 206 |
while(j.hasNext()) { |
|---|
| 207 |
Object node = j.next(); |
|---|
| 208 |
if(node instanceof BusNode) { |
|---|
| 209 |
outputText += (first ? " Take the " : ", ")+ ((BusNode)node).getDescription(); |
|---|
| 210 |
first = false; |
|---|
| 211 |
} |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|
| 214 |
int time = (int)path.getTotalCost().getTime(); |
|---|
| 215 |
if(time == 0) time = 1; |
|---|
| 216 |
outputText += " Total time: " + (time / 60) + ":" + (time % 60 < 10 ? "0" : "") + (time % 60); |
|---|
| 217 |
|
|---|
| 218 |
busRouteName.setText(outputText); |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
System.out.println("Cost of path: " + path.getTotalCost()); |
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
graphViewer.clearCustomObjectPainters(); |
|---|
| 228 |
if(showAllNodes) { |
|---|
| 229 |
PhysicalNodePainter painter = new PhysicalNodePainter(Color.CYAN, 5); |
|---|
| 230 |
Iterator i = userSearch.getSearchedNodes().iterator(); |
|---|
| 231 |
while(i.hasNext()) { |
|---|
| 232 |
Node node = (Node)i.next(); |
|---|
| 233 |
if(!(node instanceof PhysicalNode)) continue; |
|---|
| 234 |
|
|---|
| 235 |
graphViewer.addCustomObjectPainter(node, 2, painter); |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
painter = new PhysicalNodePainter(Color.RED, 5); |
|---|
| 240 |
i = userSearch.getOpenNodes().iterator(); |
|---|
| 241 |
while(i.hasNext()) { |
|---|
| 242 |
Node node = (Node)i.next(); |
|---|
| 243 |
if(!(node instanceof PhysicalNode)) continue; |
|---|
| 244 |
|
|---|
| 245 |
graphViewer.addCustomObjectPainter(node, 2, painter); |
|---|
| 246 |
} |
|---|
| 247 |
graphViewer.addCustomObjectPainter(path.nodes.getLast(), 1, new PhysicalNodePainter(Color.MAGENTA, 5)); |
|---|
| 248 |
} |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
public void switchMap() { |
|---|
| 254 |
if(graph.getBackgroundMaps().size() == 0) return; |
|---|
| 255 |
mapIndex = (mapIndex + 1) % graph.getBackgroundMaps().size(); |
|---|
| 256 |
graphViewer.setBackgroundMap((BackgroundMap)graph.getBackgroundMaps().get(mapIndex)); |
|---|
| 257 |
|
|---|
| 258 |
} |
|---|
| 259 |
} |
|---|