import java.awt.*; import java.awt.geom.*; /** * An end host in a network. * * @author Eric Lemar */ public class HostNode extends NetworkNode { /** True if currently connected to a router*/ protected boolean amConnected; /** The size of the graphic */ protected double size; /** The color of the graphic */ protected Color color; public HostNode() { this.color = Color.WHITE; this.size = 20; amConnected = false; } /** * Determine whether the new edge is legal. * * @param e The edge to check * @param n The other node * @return false if the edge is to anything other than * a RouterNode or if this node is already connected to a RouterNode. * true otherwise */ public boolean isOkConnect(NetworkEdge e,NetworkNode n) { if (n instanceof RouterNode) { if (false == amConnected) { return true; } } return false; } /** * Tell this node an edge was added so that it can keep track * of whether it's connected to a router. * @param e The added edge * @param otherNode The other node */ public void addedEdge(NetworkEdge e,NetworkNode otherNode) { assert(amConnected == false); amConnected = true; } /** * Tell this node an edge was removed so that it can keep track * of whether it's connected to a router. * * @param e The edge being removed */ public void signalRemove(NetworkEdge e) { amConnected = false; } public void draw(Graphics2D g2) { //Borrowed from CircleNode Ellipse2D circle = new Ellipse2D.Double( x, y,size,size); Color oldColor = g2.getColor(); g2.setColor(color); g2.fill(circle); g2.setColor(oldColor); g2.draw(circle); } public Rectangle2D getBounds() { return new Rectangle2D.Double(x, y, size, size); } public boolean contains(Point2D p) { Ellipse2D circle = new Ellipse2D.Double(x, y, size, size); return circle.contains(p); } public Point2D getConnectionPoint(Point2D other) { double centerX = x+size/2; double centerY = y+size/2; //Borrowed fddrom CircleNode double dx = other.getX() - centerX; double dy = other.getY() - centerY; double distance = Math.sqrt(dx * dx + dy * dy); if (distance == 0) { return other; } else { return new Point2D.Double( centerX + dx * (size / 2) / distance, centerY + dy * (size / 2) / distance); } } }