I have the following thing to do:
I've made the Node and Edge class (I hope I got them right) and I'm stuck on the GraphModel class. Here is my code so far:
Node.java (ignore the getNr)
import java.awt.Rectangle;
public class Node {
private String name;
private Rectangle rectangle;
public Node(int x, int y, int width, int height, String name) {
this.rectangle =new Rectangle(x, y, width, height);
this.name = name;
}
public Node() {
this.rectangle = new Rectangle (0,0,0,0); //gonna change the parameters later
this.name = "default name";
}
public int getNr() {
return nr;
}
public String getName() {
return name;
}
}
Edge.java:
public class Edge {
private Node nodeA;
private Node nodeB;
public Edge(Node nodeA, Node nodeB) {
this.nodeA = nodeA;
this.nodeB = nodeB;
}
public Node getNodeA() {
return nodeA;
}
public Node getNodeB() {
return nodeB;
}
}
Now, I hope they are correct but if you notice there is something not right please let me know.
Now about the GraphModel, I used a code that makes a graph with a given number of nodes (which is not good because I want to increase/decrease the N (number of nodes, you will see in the code) when I add/remove a node) . Also, the nodes are represented by integers, I wonder how can I make them be represented by the actual node class that I've created (because I will later have to make a GUI for that). I've tried to use ArrayList<ArrayList<Node>> but I have no idea what should I do next. Here is the code
GraphModel.java:
import java.util.*;
public class GraphModel {
private int numNode;
private Node node;
/* Add edge for undirected graph (I don't understand why these give opposite errors like java do you want an int or a node????
static void addEdge(ArrayList<ArrayList<Node>> gr, Edge edge) { <---- Initially made list of Node type but I'm noob and it doesn't work
gr.get(edge.getNodeA().getNr()).add(edge.getNodeB().getNr());
gr.get(edge.getNodeB()).add(edge.getNodeA());
}
*/
static void addEdge(ArrayList<ArrayList<Integer>> gr, int s, int d) {
gr.get(s).add(d);
gr.get(d).add(s);
}
/*next todo
static void delEdge(ArrayList<ArrayList<Integer>> gr,
int s, int d)
*/
public static void makeGraph(){
int NR=4; //I've tried to make it dynamic but I've no idea how (like, adjust it's size when adding edges (will do)
ArrayList<ArrayList<Integer>> gr = new ArrayList<ArrayList<Integer>>(NR);
for (int i = 0; i < NR; i++)
gr.add(new ArrayList<Integer>());
GraphModel.addEdge(gr,0,1);
GraphModel.addEdge(gr,0,2);
GraphModel.addEdge(gr,0,3);
GraphModel.addEdge(gr,1,2);
GraphModel.addEdge(gr,1,3);
printGraph(gr);
}
static void printGraph(ArrayList<ArrayList<Integer>> gr) {
for (int i = 0; i < gr.size(); i++) {
System.out.println("\nNode " + i + ":");
for (int j = 0; j < gr.get(i).size(); j++) {
System.out.print(" -> " + gr.get(i).get(j));
}
System.out.println();
}
}
}
Hope that makes sense, if not feel free to ask me anything. Also keep in mind that I am a beginner in Object oriented programming, started it like 1 week ago. Thank you very much for your time!