Go Back   Java Programming Forums > Java Standard Edition Programming Help > AWT / Java Swing > Java Applets


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 24-08-2009, 02:32 PM
Junior Member
 

Join Date: Aug 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
cBlue is on a distinguished road
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!!!!

Java Code

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:

Java Code
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?



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 09-12-2009, 11:49 PM
Junior Member
 

Join Date: Dec 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
cingriclo is on a distinguished road
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
{
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
applet and JPS problem.... please help me rockster14 Java Applets 0 07-08-2009 08:59 PM
servlet applet communication prashanthi_asn Java Servlet 0 21-05-2009 05:50 AM
Java Animation ASAP. Having Problems with Multithreading Two Animations antitru5t Threads 1 01-04-2009 07:47 PM
Calculator-applet AnithaBabu1 Java Applets 0 16-08-2008 04:42 AM
Funny Java based animation JavaPF The Cafe 1 07-06-2008 11:55 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:52 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.