Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem with my animation applet

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem with my animation applet

    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?


  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with my animation applet

    I accept with code:{
    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
    {

Similar Threads

  1. applet and JPS problem.... please help me
    By rockster14 in forum Java Applets
    Replies: 0
    Last Post: August 7th, 2009, 03:59 PM
  2. servlet applet communication
    By prashanthi_asn in forum Java Servlet
    Replies: 0
    Last Post: May 21st, 2009, 12:50 AM
  3. Replies: 1
    Last Post: April 1st, 2009, 02:47 PM
  4. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM
  5. How to make java based animation?
    By JavaPF in forum The Cafe
    Replies: 1
    Last Post: June 7th, 2008, 06:55 AM