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: 3543931 • 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. positionand 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:

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<int[]> al = new ArrayList<int[]>();   

            // 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 < token.length; j++)              // skip first one
                          points[j-3] = Integer.parseInt(token[j]);       // store as int
                       al.add(points);                                    // save in ArrayList
           i++;        
           }
            GraphView g=new GraphView(Positions,al);
            JFrame frame = new JFrame("ShowImage");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(1000,1000);

           
            frame.setContentPane(g);
            frame.setVisible(true);
        }
       
    }
   
   
   
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;
    import java.util.ArrayList;

    import javax.swing.JFrame;
    import javax.swing.JPanel;

    class GraphView extends JPanel {
             private int[][] position;
        private ArrayList<int[]> edge;
            //construct a graphview object
            public GraphView() {
            }
       
            //construct a graphview object with specified position and edge
            public GraphView(int[][] position, ArrayList<int[]> edge) {
        this.position=position;
        this.edge=edge;
   
            }
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
               
                Graphics2D g2 = (Graphics2D) g;
                for(int i=0;i<edge.size();i++){
                   
                    Point2D.Double point = new Point2D.Double(position[i][0],position[i][1]);
                    g2.setColor(Color.black);
                   
                    g2.drawString("("+position[i][0]+","+position[i][1]+")", 80,1440);
                    for(int j=0;j<edge.get(i).length;j++){
                        g2.draw(new Line2D.Double(position[i][0],position[i][1],position[edge.get(i)[j]][0],position[edge.get(i)[j]][1]));
                    }
                }
               
             
       
       
       }
       
    }