hello,
I am trying to simulate a network protocol with java.
There is a sender row and a receiver row. Sender has a window which packets inside that are ready to be sent.
I don't have problem with algorithm and these kind of staffs. I can work them out if I the graphic thing works.
I will put a brief version of program here (other things are not necessary to be posted). The main problem is that I can not access packets in sender array. The line which I commented if I uncomment it the paint method won't work!!!!
import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class test extends Applet implements ActionListener, Runnable { Button start=new Button("Start"); Button stop=new Button("Stop"); int base; int nextSeq; int windowSizeValue = 5; int messageSizeValue = 12; int packetHeight = 15; int packetWidth = 15; int senderX = 100; int senderY = 150; int recieverY = 300; int interval; Packet sender[]; Thread animThread; Dimension offD; Image offImage; Graphics offG; boolean keepDrawing = false; String message; public void init() { try { } catch (Exception e) { } add(start); start.addActionListener(this); add(stop); stop.addActionListener(this); base = 0; nextSeq = 0; Thread animThread = null; sender = new Packet[windowSizeValue]; } public void actionPerformed(ActionEvent e) { if (e.getSource() == start) { start.setEnabled(false); repaint(); this.startAnim(); } else if (e.getSource() == stop) { start.setEnabled(true); this.stopAnim(); } } public void startAnim() { if (animThread == null) animThread = new Thread(this); keepDrawing = true; animThread.start(); for (int i=0; i<messageSizeValue; i++) { sender[i] = new Packet(0 , 0); } } public void stopAnim() { if ( animThread != null && animThread.isAlive()) keepDrawing = false; animThread = null; } public void run() { while (keepDrawing) { while (base<messageSizeValue-windowSizeValue) {base+=1; repaint(); try { animThread.sleep(1000); //sleep a little to slow it down } catch (InterruptedException e) {} } } } public void paint(Graphics g) { update(g); } public void update(Graphics g) { Dimension d = getSize(); offD = d; offImage = createImage(getWidth(), getHeight()); offG = offImage.getGraphics(); offG.setColor(Color.WHITE); offG.fillRect(0,0,getWidth(), getHeight()); for (int i = 0 ; i < messageSizeValue; i++) { // if (sender[i].getStatus() == 0) // { offG.setColor(Color.BLACK); offG.drawRect(senderX+20*i, 150, 15, 15); offG.drawRect(senderX+20*i, 300, 15, 15); // } } offG.setColor(Color.BLACK); offG.drawRect(senderX+base*20-3, 146, windowSizeValue*20+1, 23); g.drawImage(offImage, 0, 0, this); } }
and this one is packet class:
class Packet { int status; int position; Packet() { this.status = 0; this.position = 0; } Packet (int pStatus, int pPosition) { status = pStatus; position = pPosition; /* * 0 = null * 1 = ready * 2 = on the way * 3 = acknowledge * 4 = on the way ack * 5 = recieved * 6 = unacknowledge */ } public int getStatus() {return status;} public int getPosition() {return position;} public void setStatus(int s) { this.status = s; } public void setPosition(int p) { this.position = p; } }
anyone has any suggestion?