/* Distributed Systems: Practical Exercise 2 This class represent a unidirectional channel between two nodes. You should not change anything in this file. This template file (C) The University of Edinburgh, 2002 Author: Yussuf Abu Shaaban */ import java.awt.*; import java.awt.geom.*; public class Channel extends Component implements Comparable{ private static int channelCount = 0; private Color color = Color.black; private int id; private Node node1; private Node node2; Line2D.Double graphicalRep; public Channel(Node node1, Node node2, int id){ this.id = id; this.node1 = node1; this.node2 = node2; Rectangle rn1 = node1.graphicalRep.getBounds(); Rectangle rn2 = node2.graphicalRep.getBounds(); this.graphicalRep = new Line2D.Double(rn1.x + (rn1.width)/2, rn1.y + (rn1.height)/2, rn2.x + (rn2.width)/2, rn2.y + (rn2.height)/2); } public int getId(){ return id; } public Node getNode1(){ return node1; } public Node getNode2(){ return node2; } public void paint(Graphics g){ Graphics2D g2 = (Graphics2D) g; g2.setPaint(color); g2.draw(graphicalRep); drawArrow((int)graphicalRep.x1, (int)graphicalRep.y1, (int)graphicalRep.x2, (int)graphicalRep.y2, g2); } public int compareTo(Object o){ Channel temp = (Channel) o; if(this.getId() < temp.getId()){ return -1;} else if(this.getId() == temp.getId()) { return 0;} else return 1; } private void drawArrow(int x1, int y1, int x2, int y2, Graphics2D g2){ double thetaR; if(x2 != x1){ thetaR = Math.atan2(Math.abs(y2 - y1), Math.abs(x2 - x1)); } else{ thetaR = Math.PI / 2; } double alpha = 30.0*Math.PI/180.0; int signX = (x1 > x2) ? 1 : -1; int signY = (y1 > y2) ? 1 : -1; int w = 10; double a = ((double)x2+(signX*w*Math.cos(thetaR-alpha))); double b = ((double)y2+(signY*w*Math.sin(thetaR-alpha))); g2.draw(new Line2D.Double(a, b, x2, y2)); a = (double)x2+(signX*w*Math.cos(thetaR+alpha)); b = (double)y2+(signY*w*Math.sin(thetaR+alpha)); g2.draw(new Line2D.Double(a, b, x2, y2)); } }