Forum Stats

  • 3,839,860 Users
  • 2,262,546 Discussions
  • 7,901,083 Comments

Discussions

Applet Starts with a Mouse Click: Why?

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Aug 20, 2015 1:18AM in New To Java

Hi,

I have downloaded a program from internet. Its a applet creating Polygons and then moving them by changing their coordinates. I have modified it by using a Time Delay loop instead of built-in Thread function. Its running fine. But I have to right click the mouse to start its execution.

/*

         *  Program:    ArrayFive.java

         *  Purpose:    Using for loop; drawPolygon() & random()

         *  Author:     George Aroush

         *  Date:       1/1/1998

         *  Change Log: None

         *

         *  Full description of Program:

         *      Mutation demo -- randomly displacing points in array

         *      and demo of drawPolygon() method and demonstrates

         *      "for ()" loop & the "+=" to move a shape on the

         *      screen

         */

        public class RandomPolygons extends java.applet.Applet

        {

            final int   ONE_SEC = 1000;

            final int   HALF_SEC = ONE_SEC / 2;

            final int   NUM_PTS = 7;

            final int   NUM_MOVES = 20;

           

            public void paint(java.awt.Graphics g)

            {

                int     x[] = { 100, 110, 110, 150, 150, 100, 100 };

                int     y[] = { 50,  50,  110, 110, 130, 130, 50 };

                int     count = 0;

                g.drawPolygon(x, y, NUM_PTS);   /* draw polygon using x[] & y[] array for 7 vertices */

                //Sleep(100000000);//Delay(ONE_SEC * 4);

  Sleep(1000);

               

                for (count = 0; count < NUM_MOVES; count++) /* draw 20 times */

                {

                    g.clearRect(0, 0, 640, 480);    /* clear screen before each display */

                   

                    g.drawPolygon(x, y, NUM_PTS);   /* draw polygon using x[] & y[] array for 7 vertices */

                   

                    Mutate(x, y, NUM_PTS, 7);

                   

                    Sleep(100000000);

                }

            }

           

           

            /*

             * void    Mutate()

             *

             * Randomly moves each x and y in the two arrays by a random integer between 0 and range

             */

            public void Mutate(int x[], int y[], int n, int range)

            {

                int     p;  /* indexes the arrays */

               

                for (p = 0; p < n; p++)     /* mutate the locations of the 7 vertices */

                {

                    x[p] += Random(range);      /* each x[] moves from 0 to range pixel, right */

                    y[p] += Random(range);      /* each y[] moves from 0 to range pixels, down */

                }

               

                x[n-1] = x[0];      /* make sure the last vertex is the same as */

                y[n-1] = y[0];      /* the first, so the outline will be closed */

            }

           

           

            /*

             *  int     Rand()

             *

             *  Generate a random number between 0 to range - 1

             */

            public int Random(int range)

            {

                return ((int) (Math.random() * range));

            }

           

           

           

           

   static  void Sleep(long val)

    {

        int     i;

        int     junk;

        junk = 0;

        i = 0;

        while (i < val)

        {

            junk = junk + i * junk;

            i++;

        }

    }

        }

Some body please guide me why i need this mouse click?

Zulfi.

This discussion has been closed.