Hey all,

Am new here . Nice to see fellow developers helping one another.

I have recently began to delve into looking at Java Applets and animation of objects within applets. As a mini-project I have embarked on creating a "crab" Applet that simulates some sort of crab behaviour.

Each instance of a "crab" is to move around randomly on the screen (within the limits of the applet window) and when a collision is detected the coordinates are inverted to creat a "bounce" effect. However, the work I have done so far is very simple and does not necessarily do much to what I would like to achieve from this little adventure .

The source code so far is...

Code:
import java.awt.*;
import java.applet.*;

class CollideCrab
{
        int width, height;
        public static final int diameter=20;
        double x, y, xinc, yinc, coll_x, coll_y;
        boolean collide;
        Color color;
        Graphics g;
        
        public CollideCrab(int w, int h, int x, int y, double xinc, double yinc, Color c)
        {
                width=w;
                height=h;
                this.x=x;
                this.y=y;
                this.xinc=xinc;
                this.yinc=yinc;
                color=c;
        }

        public double getCenterX() {return x+diameter/2;}
        public double getCenterY() {return y+diameter/2;}

        public void move()
        {
                if (collide)
                {
                        double xvect=coll_x-getCenterX();
                        double yvect=coll_y-getCenterY();

                        if((xinc>0 && xvect>0) || (xinc<0 && xvect<0))
                                xinc=-xinc;

                        if((yinc>0 && yvect>0) || (yinc<0 && yvect<0))
                                yinc=-yinc;

                        collide=false;
                }

                x+=xinc;
                y+=yinc;

                if(x<0 || x>width-diameter)
                {
                    xinc=-xinc;
                    x+=xinc;
                }

                if(y<0 || y>height-diameter)
                {
                    yinc=-yinc;
                    y+=yinc;
                }
        }

        public void hit(CollideCrab b)
        {
                if(!collide)
                {
                        coll_x=b.getCenterX();
                        coll_y=b.getCenterY();
                        collide=true;
                }
        }

        public void paint(Graphics gr)
        {
                g=gr;
                g.setColor(color);
                g.fillOval((int)x,(int)y,diameter,diameter);
        }
}

public class fiddlerCrabs extends Applet implements Runnable
{
        Thread runner;
        Image Buffer;
        Graphics gBuffer;
        CollideCrab crab[];

        static final int MAX=5;

        public void init()
        {
                Buffer=createImage(size().width,size().height);
                gBuffer=Buffer.getGraphics();

                crab=new CollideCrab[MAX];

                int w=size().width;
                int h=size().height;

                crab[0]=new CollideCrab(w,h,50,20,1.5,2.0,Color.orange);
                crab[1]=new CollideCrab(w,h,60,100,2.0,-3.0,Color.red);
                crab[2]=new CollideCrab(w,h,15,70,-2.0,-2.5,Color.pink);
                crab[3]=new CollideCrab(w,h,150,60,-2.7,-2.0,Color.cyan);
                crab[4]=new CollideCrab(w,h,210,30,2.2,-3.5,Color.magenta);
        }

        public void start()
        {
                if (runner == null)
                {
                        runner = new Thread (this);
                        runner.start();
                }
        }

        public void stop()
        {
                if (runner != null)
                {
                        runner.stop();
                        runner = null;
                }
        }

        public void run()
        {
                while(true)
                {
                        try {Thread.currentThread().sleep(30);}
                        catch (Exception e) { }

                        for(int i=0;i<MAX;i++)
                                crab[i].move();

                        handleCollision();

                        repaint();
                }
        }

        boolean collide(CollideCrab b1, CollideCrab b2)
        {
                double wx=b1.getCenterX()-b2.getCenterX();
                double wy=b1.getCenterY()-b2.getCenterY();

                double distance=Math.sqrt(wx*wx+wy*wy);

                if(distance<b1.diameter)
                {
                        return true;
                }

                        return false;
        }


        private void handleCollision()
        {
                for(int i=0;i<MAX;i++)
                        for(int j=0;j<MAX;j++)
                                {
                                        if(i!=j)
                                        {
                                                if(collide(crab[i], crab[j]))
                                                {
                                                        crab[i].hit(crab[j]);
                                                        crab[j].hit(crab[i]);
                                                }
                                        }
                                }
        }

        public void update(Graphics g)
        {
                paint(g);
          }

          public void paint(Graphics g)
        {
                gBuffer.setColor(Color.blue);
                gBuffer.fillRect(0,0,size().width,size().height);

                for(int i=0;i<MAX;i++)
                                crab[i].paint(gBuffer);

                g.drawImage (Buffer,0,0, this);
          }
}
As you can see from the code, it's very simple. I would like help on the following issues though .

I would like the crabs to each have a label above them (crabx) to keep a track of what each crab is.

A for loop for creating an instance of each crab with the random startx and starty variables to move a crab across the scene. (so that more crabs can be added dynamically and not have to hard code each crab).

If anyone could help, it would be most appreciated!