Shaded Spheres Applet

This applet draws several two-dimensional, colored circles, and uses shading with a light source to make them appear as though they are three-dimensional spheres. Each sphere is drawn with different coordinates in the z-plane, so some part of each sphere intersects with part of another sphere.

To deal with this issue, the Z-buffer algorithm is implemented. Two arrays are used, each with an element per pixel in the image. One (zPixel) initially holds the color of the background, while the other (zBuffer) initially holds the distance from the background to the viewer. Every time a pixel for an object must be drawn, its distance to the viewer must be checked with the corresponding pixel location in the zBuffer array. If the distance is less than the value in the zBuffer array, then the pixel on the new sphere is closer to the viewer, and the pixel should be drawn. Thus, the distance from the new sphere to the viewer should be placed in the ZBuffer array and the color of the closer sphere should be placed in the zPixel array.