Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project: Display a Graph Problem Description: A graph consists of vertices and e

ID: 3543891 • Letter: P

Question

Project: Display a Graph

Problem Description:

A graph consists of vertices and edges that connect vertices. Write a program that reads a graph from a file and displays it on a panel. The first line in the file contains a number that indicates the number of vertices (n). The vertices are labeled as 0, 1, ..., n-1. Each subsequent line, with the format u x y v1, v2, ..., describes that the vertex u is located at position (x, y) with edges (u, v1), (u, v2), etc. Figure (a) gives an example of the file for a graph. Your program prompts the user to enter the name of the file, reads data from the file, and displays the graph on a panel, as shown in Figure (b).

         

           (a)                    (b)           

The program reads the information about the graph and displays it visually.

Analysis:

A graph is a mathematical structure with vertices and edges that connect the vertices. A graph is a very useful tool for modeling real-world problems. This project is to display the graph. The information for the graph is stored in a file. The number of the vertices n is stored in the first line of the file. The vertices are labeled 0, 1, 2, ..., n-1. The format of each subsequent line is u x y v1, v2, ..., which describes vertex u at location (x, y) and u is connected to vertices v1, v2, etc.

Design:

The program should first read the information about the graph from a file. The program prompts the user to enter the file name and read vertices location information into a two-dimensional array named position, where (position[0][0], position[0][1]) is the x- and y-coordinates for vertex 0. For example, in the sample graph, (position[0][0], position[0][1]) is (30, 30). The program reads the edge information into an array of ArrayList named edge (ArrayList is covered in Chapter 11). For example, in the sample graph, edge[0]

  Is an ArrayList that contains elements 1 and 2, which indicates that vertex 0 is connected to vertex 1 and vertex 2. position and edge should be created as followed:

        int[][] position = new int[n][2];

ArrayList[] edge = new ArrayList[n];

After the graph information are read into position and edge, we can use these information to display the graph. Define a class named GraphView that extends JPanel, as follows:

1. data fields position and edge.

2. a constructor that constructs a GraphView object with the specified position and edge.

3. Implement the paintComponent method to display the graph.

Optional :Draw the UML diagram for GraphView and Exercise14_27:

Coding:

Name your main class Exercise14_27. You code will look like this:

public class Exercise14_27 extends JFrame {

}

class GraphView extends JPanel {

}

Two Testing:

1. use the sample graph given the Description of the problem.

2. use the following graph file to test your program:

7

0 50 45 1 3 4

1 150 25 0 2 3 5

2 250 155 1 4 6

3 150 155 0 1 4

4 30 255 0 2 3 6

5 270 55 1 6

6 60 95 2 4 5

Screen shots:

Include the screen shots for the two tests here:

Must fill in self-evaluation:  

if u want me to rate your answer faster, u can just send the code to my email: bgmds@126.com

I check my email faster than coming to this website. Thanks for the help.

Explanation / Answer

import java.awt.Graphics; import java.util.*; import java.io.*; import javax.swing.JFrame; import javax.swing.JPanel; public class main extends JFrame { public static void main(String[] args) throws Exception { Scanner s = new Scanner(System.in); System.out.println("Please enter the name of the file from which" + " /nyou would like to data to be graphed"); // Check if source file exists String x = s.nextLine(); int[][] Positions = null; File file = new File(x); ArrayList al = new ArrayList(); // confirm existence of the file if (!file.exists()) { System.out.println("That is an invalid file name: " + file); } else { System.out.println("Thanks, file " + file + " is a valid file"); } BufferedReader br = new BufferedReader(new FileReader(x)); String line = br.readLine(); // read first line int vertices = Integer.parseInt(line); Positions=new int[vertices][2]; int i=0; while((line = br.readLine()) != null) { // read each line System.out.print(line+" "); String[] token = line.split(" "); // split each number into different String int[] points = new int[token.length - 3]; // prepare array of int[] - 1 Positions[i][0]=Integer.parseInt(token[1]); Positions[i][1]=Integer.parseInt(token[2]); for(int j = 3; j