/* Distributed Systems: Practical Exercise 2 This class represent a message with a specific type, value, sender and receiver. 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 Message extends Component{ private static int width = 120; private static int height = 40; private static int messageCount = 0; private String type; private int id; private int value; private Node sender; private Node receiver; private Rectangle2D.Double graphicalRep; private Color color = Color.yellow; public Message(String type, int value, Node sender, Node receiver){ this.type = type; this.id = messageCount++; this.value = value; this.sender = sender; this.receiver = receiver; Rectangle rn = sender.graphicalRep.getBounds(); int x = rn.x + (rn.width)/2; int y = rn.y + (rn.height)/2; graphicalRep = new Rectangle2D.Double(x, y, width, height); sender.network.addMessage(this); } public String getType(){ return type; } public int getValue(){ return value; } public Node getSender(){ return sender; } public Node getReceiver(){ return receiver; } public void send(){ sender.network.updateInterface(); try{ Thread.sleep(1500); } catch(InterruptedException e){ ; } Rectangle rn2 = receiver.graphicalRep.getBounds(); this.graphicalRep.x = rn2.x + (rn2.width)/2; this.graphicalRep.y = rn2.y + (rn2.height)/2; receiver.network.updateInterface(); receive(); } private void receive(){ try{ Thread.sleep(1000); } catch(InterruptedException e){ ; } receiver.receiveMessage(this); } public int getId(){ return id; } public void paint(Graphics g){ Graphics2D g2 = (Graphics2D) g; g2.setPaint(color); g2.draw(graphicalRep); g2.setPaint(color); g2.fill(graphicalRep); g.setColor(Color.black); Rectangle rn = this.graphicalRep.getBounds(); g.drawString(String.valueOf(type), (int)graphicalRep.getX(), (int) graphicalRep.getY() + (rn.height) * 1/4); g.drawString("Value: " + String.valueOf(value), (int)graphicalRep.getX(), (int) graphicalRep.getY() + (rn.height) * 3/4); } }