Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of

What this is about.


Because this is an applet,

its init method is called first. Here, the applet's colour is set .

The applet's paint method is called next, when it appears for the first time.

Three variables are used for numbers without fraction :

One is used for fractional numbers :

The color used for drawing is set to white by calling the setColor method that's in the Graphics class.

The count variable is initialized to zero ....

and a repeating block is started, that will be executed 200 times :

We call the method Math.random to get a fractional number between 0 and 1, that we store in r1 :

This is multiplied by 350 ( width of the applet ) to get a number in the range 0 - 349.99999999999 :

We need a number without fraction for drawing, though, so, before r1's value is copied to x, we convert the copied value to int :

This is called CASTING and it doesn't change r1's value.

Same is done for y.

At position x, y we draw a tiny white circle, 3 pixels wide and high.

Variable count is incremented by one :

The block is executed again because count is still less than 200

, and again, and again,...., until count reaches a value of 200.

Then the repetition is ended

and exit the paint method.

Each time this applet paints itself, it's different !! You can see this, if you cover and uncover the applet.


These variables are created anew each time, the paint method is executed and they cease to exist when the paint method is left.

That's why they're only visible inside the paint method and they are called LOCAL variables.




Conditional expressions always between round brackets.


By itself, EXTENSIVE repetition within a drawing method is not a wise thing to do, because then the application is effectively locked up. There's no user interaction, no event handling, possible until the paint method has finished.


We could have done without r1 by stating :
    x = (int) ( Math.random( ) * 350 ) ;


You shouldn't use the random method for lotteries and such, because the numbers generated are NOT genuine random numbers.


Swing painting is different, you can find this in the Lessons for Licensees.