package visualizationElements; import java.awt.Color; import java.awt.Graphics; /** * Represents an edge of a graph. * @author MSchaefer * */ public class Edge { private Vertex source; private Vertex destination; private Color color; private String marking; int[] xPoints; int[] yPoints; float baseX; float baseY ; /** * Creates a new Edge. * @param source The vertex the edge starts at. * @param destination The vertex the edge ends at. * @param marking Marking of the edge. * @param color Coloring the edge is drawn. */ public Edge(Vertex source, Vertex destination, String marking, Color color) { this.setSource(source); this.setDestination(destination); this.setColor(color); this.setMarking(marking); } /** * Creates a new Edge. * @param source The vertex the edge starts at. * @param destination The vertex the edge ends at. * @param color Coloring the edge is drawn. */ public Edge(Vertex source, Vertex destination, Color color) { this.setSource(source); this.setDestination(destination); this.setColor(color); this.setMarking(""); } /** * Creates a new Edge. * @param source The vertex the edge starts at. * @param destination The vertex the edge ends at. * @param marking Marking of the edge. */ public Edge(Vertex source, Vertex destination, String marking) { this.setSource(source); this.setDestination(destination); this.setColor(Color.BLACK); this.setMarking(marking); } /** * Creates a new Edge. * @param source The vertex the edge starts at. * @param destination The vertex the edge ends at. */ public Edge(Vertex source, Vertex destination) { this.setSource(source); this.setDestination(destination); this.setColor(Color.BLACK); this.setMarking(""); } /** * @param source the source to set */ public void setSource(Vertex source) { this.source = source; } /** * @return the source */ public Vertex getSource() { return source; } /** * @param destination the destination to set */ public void setDestination(Vertex destination) { this.destination = destination; } /** * @return the destination */ public Vertex getDestination() { return destination; } /** * @param color the color to set */ public void setColor(Color color) { this.color = color; } /** * @return the color */ public Color getColor() { return color; } /** * @param marking the marking to set */ public void setMarking(String marking) { this.marking = marking; } /** * @return the marking */ public String getMarking() { return marking; } /** * Draws an undirected double edge directly from one vertex to another. * @param g Graphics object to draw to DrawArea. */ public void drawUndirectedDirectDoubleEdge(Graphics g) { g.drawLine( getSource().getXpos() + (getSource().getVertexDiameter()/3), getSource().getYpos() + (getSource().getVertexDiameter()/3), getDestination().getXpos() + (getSource().getVertexDiameter()/3), getDestination().getYpos() + (getSource().getVertexDiameter()/3)); g.drawString(getMarking(), (getSource().getXpos() + getDestination().getXpos())/2, (getSource().getYpos() + getDestination().getYpos())/2); g.drawLine(getSource().getXpos() + ((2*getSource().getVertexDiameter())/3), getSource().getYpos() + ((2*getSource().getVertexDiameter())/3), getDestination().getXpos() + ((2*getSource().getVertexDiameter())/3), getDestination().getYpos() + ((2*getSource().getVertexDiameter())/3)); g.drawString(getMarking(), (getSource().getXpos() + getDestination().getXpos())/2 , (getSource().getYpos() + getDestination().getYpos())/2 + 35); } /** * Draws an undirected loop. * @param g Graphics object to draw to DrawArea. */ public void drawUndirectedLoop(Graphics g) { g.drawOval(getSource().getXpos()+ getSource().getVertexDiameter()/2, getSource().getYpos()+getSource().getVertexDiameter()/2, getSource().getVertexDiameter(), getSource().getVertexDiameter()); } /** * Draws an undirected edge directly from one vertex to another. * @param g Graphics object to draw to DrawArea. */ public void drawUndirectedDirectEdge(Graphics g) { g.drawLine(getSource().getXpos() + getSource().getVertexDiameter()/2, getSource().getYpos() + getSource().getVertexDiameter()/2, getDestination().getXpos() + getDestination().getVertexDiameter()/2, getDestination().getYpos() + getDestination().getVertexDiameter()/2); g.drawString(getMarking(), (getSource().getXpos() + getDestination().getXpos())/2, (getSource().getYpos() + getDestination().getYpos())/2); } /** * Draws an undirected angled loop. * @param g Graphics object to draw to DrawArea. */ public void drawUndirectedAngeledLoop(Graphics g) { g.drawRect(getSource().getXpos()+ getSource().getVertexDiameter()/2, getSource().getYpos() + getSource().getVertexDiameter()/2, getSource().getVertexDiameter(), getSource().getVertexDiameter()); } /** * Draws an undirected angled edge from source vertex to destination vertex. * @param g Graphics object to draw to DrawArea. */ public void drawUndirectedAngledEdge(Graphics g) { g.drawLine(getSource().getXpos() + getSource().getVertexDiameter()/2, getSource().getYpos() + getSource().getVertexDiameter()/2, getSource().getXpos() + getSource().getVertexDiameter()/2, getDestination().getYpos() + getDestination().getVertexDiameter()/2); g.drawLine(getSource().getXpos() + getSource().getVertexDiameter()/2, getDestination().getYpos() + getDestination().getVertexDiameter()/2, getDestination().getXpos() + getDestination().getVertexDiameter()/2, getDestination().getYpos() + getDestination().getVertexDiameter()/2); } /** * Draws a directed edge directly from source vertex to destination vertex. * @param g Graphics object to draw to DrawArea. */ public void drawDirectedAngeledEdge(Graphics g) { createArrowDataForDirectEdges(EdgeStyle.Angled); g.drawLine(getSource().getXpos() + getSource().getVertexDiameter()/2, getSource().getYpos() + getSource().getVertexDiameter()/2, getSource().getXpos() + getSource().getVertexDiameter()/2, getDestination().getYpos() + getDestination().getVertexDiameter()/2); g.drawLine(getSource().getXpos() + getSource().getVertexDiameter()/2, getDestination().getYpos() + getDestination().getVertexDiameter()/2, getDestination().getXpos() + getDestination().getVertexDiameter()/2, getDestination().getYpos() + getDestination().getVertexDiameter()/2); g.fillPolygon(xPoints, yPoints, 3); } /** * Draws a directed loop. * @param g Graphics object to draw to DrawArea. */ public void drawDirectedLoop(Graphics g) { createArrowDataForLoops(); g.drawRect(getSource().getXpos() + getSource().getVertexDiameter()/2, getSource().getYpos() + getSource().getVertexDiameter()/2, 2*getSource().getVertexDiameter(), getSource().getVertexDiameter()); g.fillPolygon(xPoints, yPoints, 3); } /** * Draws a directed edge directly from source vertex to destination vertex. * @param g Graphics object to draw to DrawArea. */ public void drawDirectedDirectEdge(Graphics g) { createArrowDataForDirectEdges(EdgeStyle.Direct); g.drawLine( getSource().getXpos() + getSource().getVertexDiameter()/2, getSource().getYpos() + getSource().getVertexDiameter()/2, (int)baseX, (int)baseY ); g.fillPolygon(xPoints, yPoints, 3); } /** * Creates an arrow for directed edges. */ private void createArrowDataForDirectEdges(EdgeStyle edgeStyle) { int sourceX = getSource().getXpos() + getSource().getVertexDiameter()/2; int sourceY; if(edgeStyle == EdgeStyle.Direct){ sourceY = getSource().getYpos() + getSource().getVertexDiameter()/2; } else{ sourceY = getDestination().getYpos() + getDestination().getVertexDiameter()/2; } int destinationX = getDestination().getXpos() + getDestination().getVertexDiameter()/2; int destinationY = getDestination().getYpos() + getDestination().getVertexDiameter()/2; createArrow(sourceX, sourceY, destinationX, destinationY); } /** * Creates an arrow for directed loops. */ private void createArrowDataForLoops() { int sourceX = getSource().getXpos() + getSource().getVertexDiameter()/2 + getSource().getVertexDiameter(); int sourceY = getSource().getYpos() + getSource().getVertexDiameter()/2; int destinationX = getDestination().getXpos() + getDestination().getVertexDiameter()/2; int destinationY = getDestination().getYpos() + getDestination().getVertexDiameter()/2; createArrow(sourceX, sourceY, destinationX, destinationY); } /** * Creates data to draw an arrow for directed edges. * @param sourceX x position of the source vertex. * @param sourceY y position of the source vertex. * @param destinationX x position of the destination vertex. * @param destinationY y position of the destination vertex. */ private void createArrow(int sourceX, int sourceY, int destinationX,int destinationY) { xPoints = new int[ 3 ] ; yPoints = new int[ 3 ] ; float arrowWidth = 10.0f ; float theta = 0.423f ; float[] vecLine = new float[ 2 ] ; float[] vecLeft = new float[ 2 ] ; float fLength; float th; float ta; xPoints[ 0 ] = destinationX ; yPoints[ 0 ] = destinationY ; // build the line vector vecLine[ 0 ] = (float)xPoints[ 0 ] - sourceX ; vecLine[ 1 ] = (float)yPoints[ 0 ] - sourceY ; // build the arrow base vector - normal to the line vecLeft[ 0 ] = -vecLine[ 1 ] ; vecLeft[ 1 ] = vecLine[ 0 ] ; // setup length parameters fLength = (float)Math.sqrt( vecLine[0] * vecLine[0] + vecLine[1] * vecLine[1] ) ; th = arrowWidth / ( 2.0f * fLength ) ; ta = arrowWidth / ( 2.0f * ( (float)Math.tan( theta ) / 2.0f ) * fLength ) ; // find the base of the arrow baseX = ( (float)xPoints[ 0 ] - ta * vecLine[0]); baseY = ( (float)yPoints[ 0 ] - ta * vecLine[1]); // build the points on the sides of the arrow xPoints[ 1 ] = (int)( baseX + th * vecLeft[0] ); yPoints[ 1 ] = (int)( baseY + th * vecLeft[1] ); xPoints[ 2 ] = (int)( baseX - th * vecLeft[0] ); yPoints[ 2 ] = (int)( baseY - th * vecLeft[1] ); } }