📄 sr.java
字号:
statusMsg = "Simulation speed increased by 2 fps.";
} else if (cmd == "slow" && fps > 8) {
fps -= 2;
statusMsg = "Simulation speed decreased by 2 fps.";
} else if (cmd == "stopanim") {
gbnThread = null;
if (timerThread != null) {
timerFlag = true;
timerThread = null; // added later
}
stop.setLabel("Resume");
stop.setActionCommand("startanim");
// disableing all the buttons
send.setEnabled(false);
slow.setEnabled(false);
fast.setEnabled(false);
kill.setEnabled(false);
statusMsg = "Simulation paused.";
repaint();
} else if (cmd == "startanim") {
statusMsg = "Simulation resumed.";
stop.setLabel("Stop Animation");
stop.setActionCommand("stopanim");
if (timerFlag) {
statusMsg += " Timer running.";
timerThread = new Thread(this);
timerSleep = true;
timerThread.start();
}
// enabling the buttons
send.setEnabled(true);
slow.setEnabled(true);
fast.setEnabled(true);
kill.setEnabled(true);
repaint(); // repainted to show new messages
start();
} else if (cmd == "kl") {
if (sender[selected].packet_ack)
//????为什么Timer 是base的
statusMsg = "Packet " + selected
+ " destroyed. Timer running for packet" + snd_base + ".";
else
statusMsg = "Acknowledgement of packet " + selected
+ " destroyed. Timerrunning for packet " + snd_base + ".";
sender[selected].on_way = false;
kill.setEnabled(false);
selected = -1;
repaint();
} else if (cmd == "rst")
reset_app();
}
public boolean mouseDown(Event e, int x, int y) {
//System.out.println("mouseDown()");
int i, xpos, ypos;
//找出选中的位置
i = (x - h_offset) / (pack_width + 7);
if (sender[i] != null) {
xpos = h_offset + (pack_width + 7) * i;
ypos = sender[i].packet_pos;
if (x >= xpos && x <= xpos + pack_width && sender[i].on_way) {
if ((sender[i].packet_ack && y >= v_offset + ypos && y <= v_offset
+ ypos + pack_height)
|| ((!sender[i].packet_ack)
&& y >= v_offset + v_clearance - ypos && y <= v_offset
+ v_clearance - ypos + pack_height)) {
statusMsg = "Packet " + i + " selected.";
sender[i].selected = true;
selected = i;
kill.setEnabled(true);
} else
statusMsg = "Click on a moving packet to select.";
} else
statusMsg = "Click on a moving packet to select.";
}
return true;
}
public void paint(Graphics g) // To eliminate flushing, update isoverriden
{
update(g);
}
public void update(Graphics g) {
Dimension d = size();
// Create the offscreen graphics context, if no good one exists.
if ((offGraphics == null) || (d.width != offDimension.width)
|| (d.height != offDimension.height)) {
offDimension = d;
offImage = createImage(d.width, d.height);
offGraphics = offImage.getGraphics();
}
// Erase the previous image.
offGraphics.setColor(Color.white);
offGraphics.fillRect(0, 0, d.width, d.height);
// drawing window
offGraphics.setColor(Color.black);
offGraphics.draw3DRect(h_offset + snd_base * (pack_width + 7) - 4,
v_offset - 3, (window_len) * (pack_width + 7) + 1,
pack_height + 6, true);
offGraphics.draw3DRect(h_offset + rcv_base * (pack_width + 7) - 4,
v_clearance+v_offset - 3, (window_len) * (pack_width + 7) + 1,
pack_height + 6, true);
for (int i = 0; i < total_packet; i++) {
// drawing the sending row
if (sender[i] == null) {
offGraphics.setColor(Color.black);
offGraphics.draw3DRect(h_offset + (pack_width + 7) * i,
v_offset, pack_width, pack_height, true);
offGraphics.draw3DRect(h_offset + (pack_width + 7) * i,
v_offset + v_clearance, pack_width, pack_height, true);
} else {
if (sender[i].acknowledged)
offGraphics.setColor(ack_color);
else
offGraphics.setColor(unack_color);
offGraphics.fill3DRect(h_offset + (pack_width + 7) * i,
v_offset, pack_width, pack_height, true);
// drawing the destination packets
offGraphics.setColor(dest_color);
if (sender[i].reached_dest)
offGraphics.fill3DRect(h_offset + (pack_width + 7) * i,
v_offset + v_clearance, pack_width, pack_height,
true);
else
offGraphics.draw3DRect(h_offset + (pack_width + 7) * i,
v_offset + v_clearance, pack_width, pack_height,
true);
// drawing the moving packets
if (sender[i].on_way) {
if (i == selected)
offGraphics.setColor(sel_color);
else if (sender[i].packet_ack)
offGraphics.setColor(roam_pack_color);
else
offGraphics.setColor(roam_ack_color);
if (sender[i].packet_ack)
offGraphics.fill3DRect(h_offset + (pack_width + 7) * i,
v_offset + sender[i].packet_pos, pack_width,
pack_height, true);
else
offGraphics.fill3DRect(h_offset + (pack_width + 7) * i,
v_offset + v_clearance - sender[i].packet_pos,
pack_width, pack_height, true);
}
}
} // for loop ends
// drawing message boxes
offGraphics.setColor(Color.black);
int newvOffset = v_offset + v_clearance + pack_height;
int newHOffset = h_offset;
offGraphics.drawString(statusMsg, newHOffset, newvOffset + 25);
// offGraphics.drawString(strCurrentValues,newHOffset,newvOffset+40);
offGraphics.drawString("Packet", newHOffset + 15, newvOffset + 60);
offGraphics.drawString("Acknowledge", newHOffset + 85, newvOffset + 60);
offGraphics.drawString("Received Pack", newHOffset + 185,
newvOffset + 60);
offGraphics.drawString("Selected", newHOffset + 295, newvOffset + 60);
offGraphics.drawString("Snd_Base =" + snd_base, h_offset + (pack_width + 7)
* total_packet + 10, v_offset + v_clearance / 2-60);
offGraphics.drawString("NextSeq =" + nextseq, h_offset
+ (pack_width + 7) * total_packet + 10, v_offset + v_clearance
/ 2 + 20-60);
offGraphics.drawString("Rcv_Base =" + rcv_base, h_offset + (pack_width + 7)
* total_packet + 10, v_offset + v_clearance / 2+60);
offGraphics.setColor(Color.blue);
offGraphics.drawString("Sender", h_offset + (pack_width + 7)
* total_packet + 10, v_offset + 12);
offGraphics.drawString("Receiver", h_offset + (pack_width + 7)
* total_packet + 10, v_offset + v_clearance + 12);
offGraphics.setColor(Color.gray);
offGraphics.draw3DRect(newHOffset - 10, newvOffset + 42, 360, 25, true);
offGraphics.setColor(Color.red);
offGraphics.draw3DRect(h_offset + (pack_width + 7) * total_packet + 5,
v_offset + v_clearance / 2 - 15-60, 85, 40, true);
offGraphics.draw3DRect(h_offset + (pack_width + 7) * total_packet + 5,
v_offset + v_clearance / 2 - 15+50, 85, 40, true);
offGraphics.setColor(roam_pack_color);
offGraphics.fill3DRect(newHOffset, newvOffset + 50, 10, 10, true);
offGraphics.setColor(roam_ack_color);
offGraphics.fill3DRect(newHOffset + 70, newvOffset + 50, 10, 10, true);
offGraphics.setColor(dest_color);
offGraphics.fill3DRect(newHOffset + 170, newvOffset + 50, 10, 10, true);
offGraphics.setColor(sel_color);
offGraphics.fill3DRect(newHOffset + 280, newvOffset + 50, 10, 10, true);
g.drawImage(offImage, 0, 0, this);
} // method paint ends
// checks out if an array is on the way to source or destination
public boolean onTheWay(packet pac[]) {
for (int i = 0; i < pac.length; i++)
if (pac[i] == null)
return false;
else if (pac[i].on_way)
return true;
return false;
}
public void removeSndSequence( int [] seq ){
for( int i=0; i<seq.length; i++){
snd_buffer.remove( new Integer(seq[i]));
}
}
public int[] getSndSequence(int packno){
Iterator<Integer> it = snd_buffer.iterator();
int[] temp = new int[5];
int i=0;
temp[i++] = packno;
Integer itg = null;
while( it.hasNext() ){
itg = it.next();
if( packno+1 == itg.intValue() ){
temp[i++] = itg.intValue();
packno++;
}
}
int[] temp2 = new int[i];
for( int j=0; j<i; j++)
temp2[j] = temp[j];
return temp2;
}
public void removeRcvSequence( int [] seq ){
for( int i=0; i<seq.length; i++){
rcv_buffer.remove( new Integer(seq[i]));
}
}
public int[] getRcvSequence(int packno){
Iterator<Integer> it = rcv_buffer.iterator();
int[] temp = new int[5];
int i=0;
temp[i++] = packno;
Integer itg = null;
while( it.hasNext() ){
itg = it.next();
if( packno+1 == itg.intValue() ){
temp[i++] = itg.intValue();
packno++;
}
}
int[] temp2 = new int[i];
for( int j=0; j<i; j++)
temp2[j] = temp[j];
return temp2;
}
// checkes all the packets before packno. Returns false if any packet has
// not reached destination and true if all the packets have
// reacheddestination.
public boolean check_upto_n(int packno) {
for (int i = 0; i < packno; i++)
if (!sender[i].reached_dest)
return false;
return true;
}
public void reset_app() {
snd_buffer = new TreeSet<Integer>();
rcv_buffer = new TreeSet<Integer>();
rcv_base = 0;
for (int i = 0; i < total_packet; i++)
if (sender[i] != null)
sender[i] = null;
snd_base = 0;
nextseq = 0;
selected = -1;
fps = 10;
timerFlag = false;
timerSleep = false;
gbnThread = null;
timerThread = null;
if (stop.getActionCommand() == "startanim") // in case of pause mode,
// enableall buttons
{
slow.setEnabled(true);
fast.setEnabled(true);
}
send.setEnabled(true);
kill.setEnabled(false);
stop.setLabel("Stop Animation");
stop.setActionCommand("stopanim");
statusMsg = "Simulation restarted. Press 'Send New' to start.";
repaint();
}
}
//包里放一个发送时间变量
class packet {
//packet_ack表示这是否是一个确认,true表示是数据包,false表示确认
//on_way表示是否这个数据包(发送方发的)在路上,即没被kill,如果为false也就不会显示
boolean on_way, reached_dest, acknowledged, packet_ack, selected;
long snd_time;
int packet_pos;
packet() {
on_way = false;
selected = false;
reached_dest = false;
acknowledged = false;
packet_ack = true;
packet_pos = 0;
snd_time=System.currentTimeMillis();
}
packet(boolean onway, int packetpos) {
on_way = onway;
selected = false;
reached_dest = false;
acknowledged = false;
packet_ack = true;
packet_pos = packetpos;
snd_time=System.currentTimeMillis();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -