root/trunk/GuiTest.java

Revision 3, 8.3 kB (checked in by dpaola2, 6 days ago)

old pathways project added

Line 
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                 // make the graph
22                 graph = new Graph();
23
24                 //graph.createRegularGrid(33, 38, 100, 100, .1, .5);
25
26                 maps = new TiledMap[2];
27
28                 // the gif map
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),   // SouthEast corner of DCL (sidewalk int.)
38                         new Point2D.Double(2445.77, 3461.0200000000004),  // lone building NE of Windsor and Lincoln
39                         new Point2D.Double(463.74, 3537.0000000000005)    // Fork in the river NE of Windsor and Neil
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                 //AffineTransform arialTransform = AffineTransform.getScaleInstance(1.17,1.17);
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                 // here's an example of how to change how the graphViewer looks
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                 // make some buttons
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                 // here's the top-level frame thingy
86                 JFrame frame = new JFrame("Pathways GUI Test");
87
88                 // new code using boxlayouts
89
90                 // contentPane contains the graphViewer and buttonPane, which
91                 // holds the buttons, and stacks them vertically
92                 Container contentPane = frame.getContentPane();
93                 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
94                 contentPane.add(graphViewer);
95
96                 // this pane holds the buttons, and stacks them horizontally
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         // called from the event-dispatching thread when a button is pressed
115         public void actionPerformed(ActionEvent e) {
116                 // CASE: User presses "Switch Map"
117                 if(e.getSource().equals(switchMapButton)) {
118
119                         mapIndex = (mapIndex + 1) % maps.length;
120                         graphViewer.setBackgroundMap(maps[mapIndex]);
121                 }
122                 // CASE: User presses "Get a Random Path"
123                 else if(e.getSource().equals(randomPathButton)) {
124                         graphViewer.setSelectedObject(null);
125                         showBestPath((PhysicalNode)graph.getRandomNode(), (PhysicalNode)graph.getRandomNode());
126                 }
127                 // CASE: User presses "Clear All Selected Nodes"
128                 else if (  e.getSource().equals(clearNodesButton)  )    {
129                         graphViewer.clearAllSpecialNodes();
130                 }
131         }
132
133         /********************************************
134          *  pointReceived(MouseEvent event,
135          *                Point2D.Double point)
136          *
137          *  DESCRIPTION--
138          *      Whenever the mouse returns an event in the graph,
139          *  this function will handle that event.
140          *  Currently, the events supported are:
141          *
142          *  EVENTS--
143          *      Clicking the graph: highlights the point
144          *  nearest to where you clicked.
145          *
146          *      Shift-clicking the graph: highlights the
147          *  path to where you just clicked from the first
148          *  node, or just highlights a node if nothing's
149          *  highlighted yet.
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                         // Pressing shift means we want a second point!
159                         // ... and the path to it.
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                         // If shift wasn't pressed or the origin wasn't defined,
171                         // act independently of shift key.
172                         // Highlight the nearest point.
173                         else
174                         {
175
176                                 // For point-matching purposes
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                                 //Oh, and let's add a point just for fun
182                                 graph.addNode(new PhysicalNode(graph.getUnusedNodeID(), point.getX(), point.getY()));
183                                 graphViewer.graphChanged();
184
185                                 Object object = graphViewer.closestMatch(point, 0);
186                                 //if(!(object instanceof PhysicalNode)) return;
187                                 graphViewer.setSelectedObject(object);
188                         }
189                 }
190         }
191
192         private void showBestPath(PhysicalNode origin, PhysicalNode destination) {
193                 // do the search
194                 AStarSearch userSearch = new AStarSearch();
195                 userSearch.setOrigin(origin);
196                 userSearch.setDestination(destination);
197                 Path path = userSearch.computeBestPath();
198
199                 // show the path
200                 graphViewer.setPath(path);
201
202                 // for efficient .contains().
203                 //HashSet pathNodes = new HashSet(path.nodes);
204
205                 // display the searched nodes
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                         //if(pathNodes.contains(node)) continue;
213                         graphViewer.addCustomObjectPainter(node, 2, painter);
214                 }
215
216                 // display the opened nodes
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                         //if(pathNodes.contains(node)) continue;
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                 //Schedule a job for the event-dispatching thread:
231                 //creating and showing this application's GUI.
232                 //yeah, painful, i know
233                 final String[] finalArgs = args;
234                 javax.swing.SwingUtilities.invokeLater(new Runnable() {
235                         public void run() {
236                                 new GuiTest(finalArgs);
237
238                                 // for paint debugging purposes
239                                 //new javax.swing.Timer(1000, new ActionListener() {
240                                 //      public void actionPerformed(ActionEvent evt) {
241                                 //              System.out.println();
242                                 //      }
243                                 //}).start();
244                         }
245                 });
246         }
247 }
Note: See TracBrowser for help on using the browser.