/** * */ package visualizationElements; import java.awt.*; import java.util.Vector; /** * Represents a graph to visualize search algorithms. * @author MSchaefer * */ public class Graph extends VisualizationElement{ private Vector vertexes; private Vector edges; private boolean isDirected; private EdgeStyle edgeStyle; /** * Creates a new Graph. * @param vertexes Vertexes containing to the graph. * @param edges Edge containing to the graph. * @param isDirected Value whether the graph is directed or undirected. * @param edgeStyle Value in which style the edges will be drawn. */ public Graph(Vector vertexes, Vector edges, boolean isDirected, EdgeStyle edgeStyle){ this.setVertexes(vertexes); this.setEdges(edges); this.setDirected(isDirected); this.setEdgeStyle(edgeStyle); } @Override public void draw(Graphics g){ if(g != null){ // draw edges for(Edge edge : edges){ g.setColor(edge.getColor()); if(this.isDirected){ if(this.edgeStyle == EdgeStyle.Direct){ drawDirectedDirectEdge(g, edge); } else{ drawDirectedAngeledEdge(g, edge); } } // undirected Graph else{ if(this.edgeStyle == EdgeStyle.Direct){ drawUndirectedDirectEdge(g, edge); } // angeled edges else{ drawUndirectedAngeledEdge(g, edge); } } } // draw all vertexes and print names marking for(Vertex vertex : vertexes){ vertex.draw(g); } } } /** * @param isDirected the isDirected to set */ public void setDirected(boolean isDirected) { this.isDirected = isDirected; } /** * @return the isDirected */ public boolean isDirected() { return isDirected; } /** * @param edgeStyle the edgeStyle to set */ public void setEdgeStyle(EdgeStyle edgeStyle) { this.edgeStyle = edgeStyle; } /** * @return the edge style */ public EdgeStyle getEdgeStyle() { return edgeStyle; } /** * * @param vertexes The vertexes of the graph. */ public void setVertexes(Vector vertexes) { this.vertexes = vertexes; } /** * * @return the graph's vertexes. */ public Vector getVertexes() { return vertexes; } /** * * @param edges The edges of the graph. */ public void setEdges(Vector edges) { this.edges = edges; } /** * * @return the graph's edges. */ public Vector getEdges() { return edges; } /** * Draws an directed angled edge. * @param g Graphics object to draw to drawArea. * @param edge The edge to draw. */ private void drawDirectedAngeledEdge(Graphics g, Edge edge) { // if the edge is a loop draw a rectangle if(edge.getSource() == edge.getDestination()){ //edge.drawDirectedAngeledLoop(g); edge.drawDirectedLoop(g); } else{ edge.drawDirectedAngeledEdge(g); } } /** * Draws an directed direct edge. * @param g Graphics object to draw to drawArea. * @param edge The edge to draw. */ private void drawDirectedDirectEdge(Graphics g, Edge edge){ //drawArrow(g, edge); if(edge.getSource() == ((Edge) edge).getDestination()){ edge.drawDirectedLoop(g); } else{ edge.drawDirectedDirectEdge(g); } } /** * Draws an undirected direct Edge. * @param g Graphics object to draw to drawArea. * @param edge The edge to draw. */ private void drawUndirectedDirectEdge(Graphics g, Edge edge) { // if there is an double edge draw two lines if(hasEdge(edge.getSource(), edge.getDestination()) && hasEdge(edge.getDestination(), edge.getSource()) && edge.getSource() != edge.getDestination()){ edge.drawUndirectedDirectDoubleEdge(g); } else{ // if the edge is a loop draw a oval, otherwise draw a line from source to destination if(edge.getSource() == edge.getDestination()){ edge.drawUndirectedLoop(g); } else{ edge.drawUndirectedDirectEdge(g); } } } /** * Draws an undirected angled Edge. * @param g Graphics object to draw to drawArea. * @param edge The edge to draw. */ private void drawUndirectedAngeledEdge(Graphics g, Edge edge) { // if the edge is a loop draw a rectangle if(edge.getSource() == ((Edge) edge).getDestination()){ edge.drawUndirectedAngeledLoop(g); } else{ edge.drawUndirectedAngledEdge(g); } } /** * Decides whether the graph has an edge between two vertexes. * @param source The source vertex. * @param destination The destination vertex. * @return True if there is an edge between the two vertexes, false otherwise. */ private boolean hasEdge(Vertex source, Vertex destination) { for(Edge edge : edges){ if (edge.getSource() == source && edge.getDestination() == destination) return true; } return false; } }