📄 gbn.java
字号:
repaint();
nextseq++;
if (nextseq == base+window_len)
send.setEnabled(false);
start();
}
else if (cmd == "fast") // Faster button pressed
{
fps+=2;
statusMsg = "Simulation speed increased by 2 fps.";
}
else if (cmd == "slow" && fps>2)
{
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)
statusMsg = "Packet "+ selected +" destroyed. Timer running for packet "+base+".";
else
statusMsg = "Acknowledgement of packet "+ selected +" destroyed. Timer running for packet "+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)
{
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 is overriden
{
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+base*(pack_width+7)-4, 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("Base = "+base,h_offset+(pack_width+7)*total_packet+10,v_offset+v_clearance/2);
offGraphics.drawString("NextSeq = "+nextseq,h_offset+(pack_width+7)*total_packet+10,v_offset+v_clearance/2+20);
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,80,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;
}
// checkes all the packets before packno. Returns false if any packet has
// not reached destination and true if all the packets have reached destination.
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()
{
for (int i=0; i<total_packet; i++)
if (sender[i] != null)
sender[i] = null;
base = 0;
nextseq = 0;
selected = -1;
fps = 5;
timerFlag = false;
timerSleep = false;
gbnThread = null;
timerThread = null;
if(stop.getActionCommand()=="startanim") // in case of pause mode, enable all 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();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -