Most read:
Popular archives:
Java Q&A Forums - Let the great migration begin
We're pleased to announce the first phase of the integration of the Java Q&A Forums with our community platform, JavaWorld's
Daily Brew. Whether you're one of our longtime forum users or a brand newbie, we hope you'll visit the Java Q&A Forums in their new home alongside JW Blogs.
| Enterprise AJAX - Transcend the Hype |
| Memory Analysis in Eclipse |
| Oracle Compatibility Developer's Guide |
| Memory Analysis in Eclipse |

An Easter egg
As a result, this article is instead about developing a Java applet that draws textured Easter eggs. The textures are just
a tile pattern built from a straightforward mathematical function of sines and cosines. We will transform these planar textures
onto a sphere's surface to produce our finished product. To quickly draw these images to the screen we will render them into
Java Image objects using classes from the java.awt.image package, letting the browser take care of any issues involved in actually displaying the resulting pictures. See Resources for the complete source code.
I must admit that the original inspiration for this article comes from Clifford A. Pickover's Computers, Pattern, Chaos and Beauty: Graphics from an Unseen World (St. Martin's Press, ISBN: 031206179X). If pretty computer-generated pictures interest you, I recommend you pick up a copy of this book.
The first issue we encounter when generating our eggs is what texture to use. So as not to unduly restrict ourselves I'm going
to start by defining a generic Texture interface that can be supported by a variety of different texture functions.

A texture function
public interface Texture {
public RGB getTexel (double i, double j);
}
An implementation of this interface must provide a method that returns the color of the texture element at the specified texture
coordinate (i,j). The texture coordinate will be a value 0.0 <= i,j < 1.0, meaning that a texture function will define a texture over a square domain paramaterized by i and j. The texture function should, however, accept values outside this range, clipping, replicating, or extending the texture
as appropriate. The value returned from the getTexel() method is of type RGB:
public class RGB {
double r, g, b;
public RGB (double r, double g, double b) {
this.r = r;
this.g = g;
this.b = b;
}
public RGB (int rgb) {
r = (double) (rgb >> 16 & 0xff) / 255;
g = (double) (rgb >> 8 & 0xff) / 255;
b = (double) (rgb >> 0 & 0xff) / 255;
}
public void scale (double scale) {
r *= scale;
g *= scale;
b *= scale;
}
public void add (RGB texel) {
r += texel.r;
g += texel.g;
b += texel.b;
}
public int toRGB () {
return 0xff000000 | (int) (r * 255.99) << 16 |
(int) (g * 255.99) << 8 | (int) (b * 255.99) << 0;
}
}
Our RGB class is similar to the standard Color class, except that it stores RGB colors in double precision; the color components should have values 0.0 <= r,g,b <= 1.0. We also provide some helper methods to convert, scale, and combine colors.
An image-based texture
This class implements a texture that uses an Image object as a source. We can use this class to map images onto a sphere by first converting the image into an array of integer
RGB values (using the java.awt.image.PixelGrabber class) and then using this array to calculate texel values (as pixel is to picture element, so texel is to texture element).