Last active
May 9, 2019 03:47
-
-
Save ahmednasserpro/74b6d3077439a52b82987870730c6118 to your computer and use it in GitHub Desktop.
(Display a graph) 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 ..., de…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import java.awt.*; | |
import java.util.*; | |
import java.io.*; | |
import javax.swing.*; | |
public class Test { | |
public static void main(String[] args) throws Exception { | |
File file = new File(args[0]); | |
JFrame frame = new JFrame("Graph"); | |
frame.add(new MyPanel(file)); | |
frame.setSize(300, 300); | |
frame.setLocationRelativeTo(null); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setVisible(true); | |
} | |
static class MyPanel extends JPanel { | |
File file; | |
MyPanel(File file) { | |
this.file = file; | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
try { | |
Scanner input = new Scanner(file); | |
int n = input.nextInt(); | |
input.nextLine(); | |
int[][] points = new int[n][2]; | |
StringBuilder sb = new StringBuilder(); | |
while (input.hasNext()) { | |
String line = input.nextLine(); | |
addPoints(points, sb, line); | |
} | |
// Draw oval and string | |
g.setColor(Color.red); | |
for (int i = 0; i < n; i++) { | |
g.fillOval(points[i][0], points[i][1], 20, 20); | |
g.drawString(i + "", points[i][0], points[i][1] - 5); | |
} | |
// Draw lines | |
g.setColor(Color.black); | |
int[][] linesPoints = getLinesPoints(sb); | |
for (int i = 0; i < linesPoints.length; i++) { | |
int p1 = linesPoints[i][0]; | |
int p2 = linesPoints[i][1]; | |
// If line is horizontal | |
if ((p1 % 2 == 0 && p2 % 2 == 0) || (p1 % 2 == 1 && p2 % 2 == 1)) { | |
int x1 = points[p1][0] + 10; | |
int y1 = points[p1][1] + 20; | |
int x2 = points[p2][0] + 10; | |
int y2 = points[p2][1]; | |
g.drawLine(x1, y1, x2, y2); | |
// If line is vertical | |
} else if (p1 % 2 == 0 && p2 % 2 == 1) { | |
int x1 = points[p1][0] + 20; | |
int y1 = points[p1][1] + 10; | |
int x2 = points[p2][0]; | |
int y2 = points[p2][1] + 10; | |
g.drawLine(x1, y1, x2, y2); | |
// Else | |
} else { | |
int x1 = points[p1][0] + 5; | |
int y1 = points[p1][1] + 15; | |
int x2 = points[p2][0] + 15; | |
int y2 = points[p2][1] + 5; | |
g.drawLine(x1, y1, x2, y2); | |
} | |
} | |
} catch (FileNotFoundException ex) { | |
System.out.println(ex); | |
} | |
} | |
void addPoints(int[][] points, StringBuilder sb, String line) { | |
Scanner input = new Scanner(line); | |
int numberOfPoints = input.nextInt(); | |
points[numberOfPoints][0] = input.nextInt(); | |
points[numberOfPoints][1] = input.nextInt(); | |
while (input.hasNext()) { | |
sb.append(numberOfPoints).append(" ").append(input.nextInt()).append("\r\n"); | |
} | |
} | |
int[][] getLinesPoints(StringBuilder sb) { | |
int numOfLines = 0; | |
Scanner input = new Scanner(sb.toString()); | |
while (input.hasNext()) { | |
input.nextLine(); | |
numOfLines++; | |
} | |
input = new Scanner(sb.toString()); | |
int currentLine = 0; | |
int[][] linesPoints = new int[numOfLines][2]; | |
while (input.hasNext()) { | |
int p1 = input.nextInt(); | |
int p2 = input.nextInt(); | |
linesPoints[currentLine][0] = p1; | |
linesPoints[currentLine][1] = p2; | |
input.nextLine(); | |
currentLine++; | |
} | |
int[][] a = revising(linesPoints); | |
return a; | |
} | |
/** | |
* This method is used to revise linesPoints array so the returned array | |
* has the actual indices | |
* | |
* @param linesPoints | |
* @return | |
*/ | |
int[][] revising(int[][] linesPoints) { | |
int[][] array = new int[linesPoints.length][2]; | |
array[0][0] = linesPoints[0][0]; | |
array[0][1] = linesPoints[0][1]; | |
int i = 1; | |
for (int j = 1; j < array.length; j++) { | |
if (!isLineRepeated(array, linesPoints[j][0], linesPoints[j][1], i)) { | |
array[i][0] = linesPoints[j][0]; | |
array[i][1] = linesPoints[j][1]; | |
i++; | |
} | |
} | |
int[][] newArray = new int[i][2]; | |
for (int j = 0; j < newArray.length; j++) { | |
newArray[j][0] = array[j][0]; | |
newArray[j][1] = array[j][1]; | |
} | |
return newArray; | |
} | |
/** | |
* Check if the line is repeated or not | |
* | |
* @param a is the array that has line's vertices | |
* @param p1 is vertex number 1 | |
* @param p2 is vertex number 2 | |
* @param i the current checked index for array a | |
* @return true if the line is repeated, else return false | |
*/ | |
boolean isLineRepeated(int[][] a, int p1, int p2, int i) { | |
for (int j = 0; j < i; j++) { | |
if ((a[j][0] == p1 && a[j][1] == p2) | |
|| (a[j][0] == p2 && a[j][1] == p1)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment