Java Reference
In-Depth Information
9
new City( "San Francisco" , 50 , 210 ),
10
new City( "Los Angeles" , 75 , 275 ), new City( "Denver" , 275 , 175 ),
11
new City( "Kansas City" , 400 , 245 ),
12
new City( "Chicago" , 450 , 100 ), new City( "Boston" , 700 , 80 ),
13
new City( "New York" , 675 , 120 ), new City( "Atlanta" , 575 , 295 ),
14
new City( "Miami" , 600 , 400 ), new City( "Dallas" , 408 , 325 ),
15
new City( "Houston" , 450 , 360 ) };
16
17 // Edge array for graph in FigureĀ 28.1
18 int [][] edges = {
19 { 0 , 1 }, { 0 , 3 }, { 0 , 5 }, { 1 , 0 }, { 1 , 2 }, { 1 , 3 },
20 { 2 , 1 }, { 2 , 3 }, { 2 , 4 }, { 2 , 10 },
21 { 3 , 0 }, { 3 , 1 }, { 3 , 2 }, { 3 , 4 }, { 3 , 5 },
22 { 4 , 2 }, { 4 , 3 }, { 4 , 5 }, { 4 , 7 }, { 4 , 8 }, { 4 , 10 },
23 { 5 , 0 }, { 5 , 3 }, { 5 , 4 }, { 5 , 6 }, { 5 , 7 },
24 { 6 , 5 }, { 6 , 7 }, { 7 , 4 }, { 7 , 5 }, { 7 , 6 }, { 7 , 8 },
25 { 8 , 4 }, { 8 , 7 }, { 8 , 9 }, { 8 , 10 }, { 8 , 11 },
26 { 9 , 8 }, { 9 , 11 }, { 10 , 2 }, { 10 , 4 }, { 10 , 8 }, { 10 , 11 },
27 { 11 , 8 }, { 11 , 9 }, { 11 , 10 }
28 };
29
30
Graph<City> graph = new UnweightedGraph<>(vertices, edges);
create a graph
31
32 // Create a scene and place it in the stage
33 Scene scene = new Scene( new GraphView(graph), 750 , 450 );
34 primaryStage.setTitle( "DisplayUSMap" ); // Set the stage title
35 primaryStage.setScene(scene); // Place the scene in the stage
36 primaryStage.show(); // Display the stage
37 }
38
39
create a GraphView
static class City implements Displayable {
City class
40
private int x, y;
41
private String name;
42
43
City(String name, int x, int y) {
44
this .name = name;
45
this .x = x;
46
this .y = y;
47 }
48
49 @Override
50
public int getX() {
51
return x;
52 }
53
54 @Override
55
public int getY() {
56
return y;
57 }
58
59 @Override
60
public String getName() {
61
return name;
62 }
63 }
64 }
The class City is defined to model the vertices with their coordinates and names (lines 39-63).
The program creates a graph with the vertices of the City type (line 30). Since City implements
Displayable , a GraphView object created for the graph displays the graph in the pane (line 33).
As an exercise to get acquainted with the graph classes and interfaces, add a city (e.g.,
Savannah) with appropriate edges into the graph.
 
Search WWH ::




Custom Search