Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
We'll be looking at a fairly specific subject: generating and rendering terrain maps, such as the surface of Mars or a few atoms of gold. Terrain-map rendering can be used for more than just aesthetic purposes -- many data-visualization techniques produce data that can be rendered as terrain maps. My intentions are, of course, entirely artistic, as you can see by the picture below! Should you so desire, the code that we will produce is general enough that with only minor tweaking it can also be used to render 3D structures other than terrains.

A 3D fractal landscape
Click here to view and manipulate the terrain applet.
In preparation for our discussion today, I suggest that you read June's "Draw textured spheres" if you haven't already done so. The article demonstrates a ray-tracing approach to rendering images (firing rays into a
virtual scene to produce an image). In this article, we'll be rendering scene elements directly onto the display. Although
we're using two different techniques, the first article contains some background material on the java.awt.image package that I will not rehash in this discussion.

A terrain map
Let us define our terrain as an interface:
public interface Terrain {
public double getAltitude (double i, double j);
public RGB getColor (double i, double j);
}
For the purpose of this article we will assume that 0.0 <= i,j,altitude <= 1.0. This is not a requirement, but will give us a good idea where to find the terrain that we'll be viewing.
The color of our terrain is described simply as an RGB triplet. To produce more interesting images we might consider adding other information such as the surface shininess, etc. For now, however, the following class will do:
public class RGB {
private double r, g, b;
public RGB (double r, double g, double b) {
this.r = r;
this.g = g;
this.b = b;
}
public RGB add (RGB rgb) {
return new RGB (r + rgb.r, g + rgb.g, b + rgb.b);
}
public RGB subtract (RGB rgb) {
return new RGB (r - rgb.r, g - rgb.g, b - rgb.b);
}
public RGB scale (double scale) {
return new RGB (r * scale, g * scale, b * scale);
}
private int toInt (double value) {
return (value < 0.0) ? 0 : (value > 1.0) ? 255 :
(int) (value * 255.0);
}
public int toRGB () {
return (0xff << 24) | (toInt (r) << 16) |
(toInt (g) << 8) | toInt (b);
}
}
The RGB class defines a simple color container. We provide some basic facilities for performing color arithmetic and converting a
floating-point color to packed-integer format.