Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. package edu.rajagopal.dhruv.graphRepresentation;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.Scanner;
  8.  
  9. import javafx.application.Application;
  10. import javafx.scene.Scene;
  11. import javafx.stage.Stage;
  12.  
  13. public class TestGraph extends Application {
  14. @Override // Override the start method in the Application class
  15. public void start(Stage primaryStage) throws IOException {
  16. Scanner read = new Scanner(System.in);
  17. System.out.println("Enter file name: ");
  18. String filename = read.nextLine();
  19. Scanner readF = new Scanner(new File(filename));
  20. int totalVert = readF.nextInt();
  21. ArrayList<Integer[]> verticesL = new ArrayList<Integer[]>();
  22. Vertex[] vertices = new Vertex[totalVert];
  23.  
  24. readF.nextLine();
  25. for (int i = 0; i < totalVert; i++) {
  26. String line = readF.nextLine();
  27. //System.out.println(line);
  28. Scanner liner = new Scanner(line);
  29. liner.nextInt();
  30. int x = liner.nextInt()*2;
  31. int y = liner.nextInt()*2;
  32. vertices[i] = new Vertex(Integer.toString(i), x, y);
  33. while (liner.hasNextInt()) {
  34. verticesL.add(new Integer[] {i, liner.nextInt()});
  35. }
  36. }
  37.  
  38. Integer[][] temp = verticesL.toArray(new Integer[verticesL.size()][2]);
  39. int[][] edges = new int[verticesL.size()][2];
  40. for(int i = 0; i < temp.length; i++) {
  41. for (int j = 0; j < temp[i].length; j++)
  42. edges[i][j] = temp[i][j].intValue(); // returns int value
  43. }
  44. //System.out.println("vertices:" + Arrays.toString(vertices));
  45. //System.out.println("edges:" + Arrays.deepToString(edges));
  46. Graph<Vertex> graph = new UnweightedGraph<Vertex>(vertices, edges);
  47.  
  48. System.out.println("Is connected: " + ((AbstractGraph<Vertex>) graph).isConnected());
  49. System.out.println("Is cyclic: " + ((AbstractGraph<Vertex>) graph).isCyclic());
  50.  
  51.  
  52. // Create a scene and place it in the stage
  53. Scene scene = new Scene(new GraphView(graph), 500, 500);
  54. primaryStage.setTitle("ParsedGraph"); // Set the stage title
  55. primaryStage.setScene(scene); // Place the scene in the stage
  56. primaryStage.show(); // Display the stage
  57. readF.close();
  58. read.close();
  59. }
  60.  
  61. static class Vertex implements Displayable {
  62. private int x, y;
  63. private String name;
  64.  
  65. Vertex(String name, int x, int y) {
  66. this.x = x;
  67. this.y = y;
  68. this.name = name;
  69. }
  70.  
  71. @Override
  72. public int getX() {
  73. return x;
  74. }
  75.  
  76. @Override
  77. public int getY() {
  78. return y;
  79. }
  80.  
  81. @Override
  82. public String getName() {
  83. return name;
  84. }
  85.  
  86. //for debugging mostly
  87. public String toString() {
  88. return "x: " + x + " y: " + y + " name: " + name;
  89. }
  90. }
  91.  
  92. /**
  93. * The main method is only needed for the IDE with limited JavaFX support. Not
  94. * needed for running from the command line.
  95. */
  96. public static void main(String[] args) {
  97. launch(args);
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement