Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.rajagopal.dhruv.graphRepresentation;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.stage.Stage;
- public class TestGraph extends Application {
- @Override // Override the start method in the Application class
- public void start(Stage primaryStage) throws IOException {
- Scanner read = new Scanner(System.in);
- System.out.println("Enter file name: ");
- String filename = read.nextLine();
- Scanner readF = new Scanner(new File(filename));
- int totalVert = readF.nextInt();
- ArrayList<Integer[]> verticesL = new ArrayList<Integer[]>();
- Vertex[] vertices = new Vertex[totalVert];
- readF.nextLine();
- for (int i = 0; i < totalVert; i++) {
- String line = readF.nextLine();
- //System.out.println(line);
- Scanner liner = new Scanner(line);
- liner.nextInt();
- int x = liner.nextInt()*2;
- int y = liner.nextInt()*2;
- vertices[i] = new Vertex(Integer.toString(i), x, y);
- while (liner.hasNextInt()) {
- verticesL.add(new Integer[] {i, liner.nextInt()});
- }
- }
- Integer[][] temp = verticesL.toArray(new Integer[verticesL.size()][2]);
- int[][] edges = new int[verticesL.size()][2];
- for(int i = 0; i < temp.length; i++) {
- for (int j = 0; j < temp[i].length; j++)
- edges[i][j] = temp[i][j].intValue(); // returns int value
- }
- //System.out.println("vertices:" + Arrays.toString(vertices));
- //System.out.println("edges:" + Arrays.deepToString(edges));
- Graph<Vertex> graph = new UnweightedGraph<Vertex>(vertices, edges);
- System.out.println("Is connected: " + ((AbstractGraph<Vertex>) graph).isConnected());
- System.out.println("Is cyclic: " + ((AbstractGraph<Vertex>) graph).isCyclic());
- // Create a scene and place it in the stage
- Scene scene = new Scene(new GraphView(graph), 500, 500);
- primaryStage.setTitle("ParsedGraph"); // Set the stage title
- primaryStage.setScene(scene); // Place the scene in the stage
- primaryStage.show(); // Display the stage
- readF.close();
- read.close();
- }
- static class Vertex implements Displayable {
- private int x, y;
- private String name;
- Vertex(String name, int x, int y) {
- this.x = x;
- this.y = y;
- this.name = name;
- }
- @Override
- public int getX() {
- return x;
- }
- @Override
- public int getY() {
- return y;
- }
- @Override
- public String getName() {
- return name;
- }
- //for debugging mostly
- public String toString() {
- return "x: " + x + " y: " + y + " name: " + name;
- }
- }
- /**
- * The main method is only needed for the IDE with limited JavaFX support. Not
- * needed for running from the command line.
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement