root/trunk/AppletTest.java

Revision 3, 7.7 kB (checked in by dpaola2, 1 month ago)

old pathways project added

Line 
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                 //getContentPane().add(graphViewer, BorderLayout.CENTER);
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                 //getContentPane().add(slider, BorderLayout.SOUTH);
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                 // contentPane contains the graphViewer and buttonPane, which
104                 // holds the buttons, and stacks them vertically
105                 Container contentPane = getContentPane();
106                 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
107                 contentPane.add(graphViewer);
108
109                 // this pane holds the buttons, and stacks them horizontally
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                 // put this between each new button
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                 // if mouse clicked
150                 if(event.getID() == MouseEvent.MOUSE_CLICKED) {
151                 // if selected
152                         if(selected instanceof PhysicalNode &&
153                            (graphViewer.getPath() == null || event.isShiftDown()) &&
154                            clicked != null) {
155                                 // find path and clear selection if not shift
156                                 showBestPath((PhysicalNode)selected, clicked);
157                                 //if(!event.isShiftDown())
158                                 //      graphViewer.setSelectedObject(null);
159                         }
160                         // if not selected
161                         else {
162                                 // set selection
163                                 graphViewer.setPath(null);
164                                 graphViewer.clearCustomObjectPainters();
165                                 graphViewer.setSelectedObject(clicked);
166                         }
167                 } else
168
169                 // if mouse dragged
170                 if(event.getID() == MouseEvent.MOUSE_DRAGGED) {
171                         // if shift
172                         if(event.isShiftDown()) {
173                                 // find path (if possible)
174                                 if(selected instanceof PhysicalNode && clicked != null)
175                                         showBestPath((PhysicalNode)selected, clicked);
176                         }
177                         // otherwise
178                         else {
179                                 // do drag-scrolling
180                                 //Point2D.Double center = graphViewer.getVisibleRegionCenter();
181                                 //center.x -= point.x - lastPoint.x;
182                                 //center.y -= point.y - lastPoint.y;
183                                 //graphViewer.setVisibleRegionCenter(center);
184                         }
185                 }
186                 lastPoint = point;
187         }
188
189         Point2D.Double lastPoint;
190
191         private void showBestPath(PhysicalNode origin, PhysicalNode destination) {
192                 // do the search
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                 // show the path
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                 // for efficient .contains().
224                 //HashSet pathNodes = new HashSet(path.nodes);
225
226                 // display the searched nodes
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                                 //if(pathNodes.contains(node)) continue;
235                                 graphViewer.addCustomObjectPainter(node, 2, painter);
236                         }
237
238                         // display the opened nodes
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                                 //if(pathNodes.contains(node)) continue;
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                 //graphViewer.scaleRelative(1);
258         }
259 }
Note: See TracBrowser for help on using the browser.