root/trunk/BuildingApplet.java

Revision 3, 3.4 kB (checked in by dpaola2, 6 days 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 BuildingApplet extends JApplet {
12         JGraphViewer graphViewer;
13         Graph graph;
14         JButton switchMapButton;
15
16         int sourceFloor, destFloor, currentFloor;
17
18         public void createGui() {
19
20                 // loads the graph from the file
21                 loadGraph();
22
23                 String sourceRoomNum = getParameter("source");
24                 String destRoomNum = getParameter("destination");
25
26                 Node sourceN = graph.getNodeByName(sourceRoomNum);
27                 Node destN = graph.getNodeByName(destRoomNum);
28                
29                 if(sourceN == null || destN == null) {
30                         JOptionPane.showMessageDialog(this, "Could not find source " + sourceRoomNum + " or destination " + destRoomNum, "Find@Siebel", JOptionPane.ERROR_MESSAGE);     
31                         return;
32                 }
33                
34                 RoomNode source = (RoomNode)sourceN;
35                 RoomNode dest = (RoomNode)destN;
36
37                
38                
39                 sourceFloor = source.getFloor();
40                 destFloor = dest.getFloor();
41
42                 currentFloor = sourceFloor;             
43                
44                 AStarSearch userSearch = new AStarSearch();
45                 userSearch.setOrigin(source);
46                 userSearch.setDestination(dest);
47                 Path path = userSearch.computeBestPath();               
48
49                 graphViewer = new JGraphViewer(graph, 8, 8, null);
50                 graphViewer.setBackground(Color.BLACK);
51                 graphViewer.setDefaultNodePainter(null);
52                 graphViewer.setDefaultEdgePainter(null);
53                 graphViewer.setPathNodePainter(new PhysicalNodePainter(Color.BLUE, (float)2.5));
54                 graphViewer.setPathEdgePainter(new PhysicalEdgePainter(Color.BLUE, 5));
55
56                 graphViewer.setPath(path);
57
58                 graphViewer.addCustomObjectPainter(source, 0, new PhysicalNodePainter(Color.GREEN, 5));
59                 graphViewer.addCustomObjectPainter(dest, 0, new PhysicalNodePainter(Color.RED, 5));
60                
61                 graphViewer.setBackgroundMap((BackgroundMap)graph.getBackgroundMaps().get(currentFloor));
62                
63                 getContentPane().add(graphViewer, BorderLayout.CENTER);
64
65                 if(sourceFloor != destFloor) {
66                         switchMapButton = new JButton("Click here to show destination floor");
67                         switchMapButton.addActionListener(new ActionListener() {
68                                 public void actionPerformed(ActionEvent e) {
69                                         switchMap();
70                                 }
71                         });
72                         getContentPane().add(switchMapButton, BorderLayout.SOUTH);
73                 }
74                
75
76                 graphViewer.setBackground(new Color(0xcbcc99));
77         }
78
79         public void start() {
80                 validate();
81                 graphViewer.scaleRelative(0.0);
82         }
83        
84         public void loadGraph() {
85                 try {
86                         System.out.println("getCodeBase: " + getCodeBase().toString());
87                         System.out.println("getDocumentBase: " + getDocumentBase().toString());
88                
89                        
90                         graph = Graph.loadFromStream((new URL(getCodeBase().toString() + "siebelmap.dat")).openStream());
91                         Iterator i = graph.getBackgroundMaps().iterator();
92                         while(i.hasNext()) {
93                                 ((BuildingMap)i.next()).setRootUri(getCodeBase().toString());
94                         }
95                 } catch(Exception e) {
96                         e.printStackTrace();
97                 }
98         }
99
100         public void switchMap() {
101                 if(currentFloor == sourceFloor) {
102                         currentFloor = destFloor;
103                         switchMapButton.setText("Click here to show original floor.");
104                 } else {
105                         currentFloor = sourceFloor;
106                         switchMapButton.setText("Click here to show destination floor.");
107                 }               
108                
109                 graphViewer.setBackgroundMap((BackgroundMap)graph.getBackgroundMaps().get(currentFloor));
110         }
111
112         public void init() {
113                 try {
114                         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
115                         public void run() {
116                                 createGui();
117                                 }
118                         });
119                 } catch (Exception e) {
120                         e.printStackTrace();
121                         System.err.println("createGUI didn't successfully complete");
122                 }
123         }
124
125 }
Note: See TracBrowser for help on using the browser.