package MaxFlowApplet.graphic; /* Contains classes for graphic representation of the graph */ import MaxFlowApplet.graph.*; import java.awt.*; /* The class draws an arrow, i.e. triangle, in different positions. * * It receives some data about the point, where the arrow top must * * be and the rotating angle from the default position (see below). * * The class extends the Polygon class, so some of its functionali- * * ty is iherited. */ class Arrow extends Polygon { // (1) + // | \ // | \ (0) (top) // ------| +---- // | / // | / // (2) + public Arrow (int x, int y, double rotate_angle) { super(); int xCoor[] = new int[4]; int yCoor[] = new int[4]; double temp_x1, temp_x2, temp_y1, temp_y2; //calculate the points, without any rotation xCoor[0] = xCoor[3] = x; yCoor[0] = yCoor[3] = y; temp_x1 = temp_x2 = - Math.cos(DEFAULTTOPANGLE/2.0)*DEFAULTSIDELEN; temp_y1 = - Math.sin(DEFAULTTOPANGLE/2)*DEFAULTSIDELEN; temp_y2 = Math.sin(DEFAULTTOPANGLE/2)*DEFAULTSIDELEN; //rotate the arrow around the top xCoor[1] = (int)((double)x + temp_x1*Math.cos(rotate_angle) - temp_y1*Math.sin(rotate_angle)); yCoor[1] = (int)((double)y + temp_x1*Math.sin(rotate_angle) + temp_y1*Math.cos(rotate_angle)); xCoor[2] = (int)((double)x + temp_x2*Math.cos(rotate_angle) - temp_y2*Math.sin(rotate_angle)); yCoor[2] = (int)((double)y + temp_x2*Math.sin(rotate_angle) + temp_y2*Math.cos(rotate_angle)); for (int i=0; i<4; i++) this.addPoint(xCoor[i], yCoor[i]); } public void draw( Graphics g, Color color) { Color cur_color; cur_color = g.getColor(); g.setColor(color); g.fillPolygon((Polygon)this); g.setColor(cur_color); } protected double DEFAULTSIDELEN = 20.0; protected double DEFAULTTOPANGLE = Math.PI*0.2; }; /*************************************************************************/ /* The class extends the FlowEdge class, by adding functions for drawing * * the edge, its arrow (direction), the capacity and flow values. * *************************************************************************/ public class GraphicEdge extends FlowEdge { public GraphicEdge( GraphicVertex from_v, GraphicVertex to_v, int cap) { from = from_v; to = to_v; capacity = cap; color = DEFAULTCOLOR; updateArrowLocation(); } public Color getColor() { return color; }; public void setColor(Color newColor) { color = newColor; }; public void setFlow(int newFlow) { flow = newFlow; }; public boolean inside(int x, int y) { return ((Polygon)arrow).inside(x, y); }; public Arrow getArrow() { return arrow; }; public void updateArrowLocation() { int arrow_top_x, arrow_top_y; Point p1 = ((GraphicVertex)from).getPosition(); Point p2 = ((GraphicVertex) to).getPosition(); //some calculations for the arrow arrow_top_x = p1.x + (int)((float)(p2.x-p1.x)*0.8); arrow_top_y = p1.y + (int)((float)(p2.y-p1.y)*0.8); slant = Math.atan2((double)(p2.y-p1.y),(double)(p2.x-p1.x)); arrow = new Arrow(arrow_top_x, arrow_top_y, slant); }; public void draw(Graphics g, Color c) { color = c; draw(g); } public void draw(Graphics g) { Point p1 = ((GraphicVertex)from).getPosition(); Point p2 = ((GraphicVertex)to).getPosition(); int center_x = p1.x + (int)((float)(p2.x-p1.x)*0.8); int center_y = p1.y + (int)((float)(p2.y-p1.y)*0.8); Point flowBase = null; Point capacityBase = null; g.setColor(color); g.drawLine( p1.x, p1.y, p2.x, p2.y); updateArrowLocation(); arrow.draw(g, color); //decide where to place the flow and capacity numbers // \(4) / // \ / // (3) \/ // /\ (1) // / \ // / (2)\ if ((slant <= Math.PI*0.25)&&(slant > -Math.PI*0.25)) { // (1) flowBase = new Point(center_x - 15, center_y + 15); capacityBase = new Point(center_x - 15, center_y - 5); } else if ((slant <= Math.PI*0.75)&&(slant > Math.PI*0.25)) { // (2) flowBase = new Point(center_x + 5, center_y); capacityBase = new Point(center_x - 15, center_y); } else if ((slant <= -Math.PI*0.75)||(slant > Math.PI*0.75)) { // (3) flowBase = new Point(center_x, center_y + 15); capacityBase = new Point(center_x, center_y - 5); } else if ((slant <= -Math.PI*0.25)&&(slant > -Math.PI*0.75)) { // (4) flowBase = new Point(center_x + 5, center_y + 15); capacityBase = new Point(center_x - 15, center_y + 15); } if (flow > 0) { g.setColor(FLOWCOLOR); g.drawString((new Integer(flow)).toString(), flowBase.x, flowBase.y); } g.setColor(CAPACITYCOLOR); g.drawString((new Integer(capacity)).toString(), capacityBase.x, capacityBase.y); } protected Color color; protected Arrow arrow; protected double slant; protected final int MAXCAPACITY = 50; protected final Color DEFAULTCOLOR = Color.gray; protected final Color CAPACITYCOLOR = Color.white; protected final Color FLOWCOLOR = Color.yellow; }; //**********************************************************************