WhiteNoise Applet
(A Very Poor Virtual Television Tuner ;-)
(Click the screen to stop the noise.)
Here are links to the files used by the applet:
WhiteNoise.class
tvimage.gif
prettycool.gif
whitenoiselink.gif
whitenoiselink2.gif
white.au
Here's the WhiteNoise.java source code:
The WhiteNoise applet is not a particularly useful applet since it is not really a
television tuner at all. It just displays the picture of a television and the
white noise associated with not having a usable signal strength. At least, that's
what used to happen back in the old days. These days, you just get a blue screen
where the signal is too weak to display a decent picture. But, an applet with a blue
screen would not be very impressive would it?
However, despite the relative uselessness of the applet, I have included the source
code, which might be instructive if you're interested in such things as animation
threads and double-buffering.
The applet was written with a simple text editor and compiled with Java Development Kit
1.1 using only Java 1.0 specific code as there are still many browsers out there that
do not fully support all the Java 1.1.x stuff.
The television image is just some old black and white television somebody was trying
to sell on eBay. I downloaded the picture and used a paint program to change the picture tube
portion to a transparent color, then saved as a .gif image. The flickering white noise
image is actually an array of images that are created during the phase of the program where
the pretention is being made to "scan" for available channels. The code creates a number
of images that are roughly 1/4 the television screen size. These are then combined to form
what appears to be one complete image, but is actually 4 images with 1 in each quadrant.
So, you have an array of 1/4 size images that are being combined together into a back buffer
each time through the animation loop. The newly-created white noise image is drawn to the back
buffer first, then the television transparency is drawn over the top. Next, a link image is
drawn on the bottom of the back buffer image, which is finally drawn to the screen at the end
of each loop as one single image. This technique eliminates flashing which would occur if we
tried to do without the back buffer.
The link image is set to 1 of 3 possible images. Initially, it is set to the blue java4free link
.gif. However, when the user puts the mouse over the link image, the image is switched to the
red java4free link .gif. When the user puts the mouse over the upper right-hand portion of the
television image, the link image changes to the pretty cool .gif image. All of the link image
switching is handled in the mouseMove(Event e, int x, int y) and mouseExit(Event e, int x, int y)
methods. We just check the values
of x and y to see where the mouse was at when it was moved. If the user clicks the mouse when it
is over the java4free link image, he or she is redirected to the java4free home page. This is
done with the mouseUp(Event e, int x, int y) method.
Of further interest is the way we generate the random noise in a set of 2 for...next loops. Random
colors are generated, but these are all gray colors because this is, after all, a black and white
television. This is accomplished by always using the same primary color value for all three rgb
components. Anytime you do this, the resulting color is always white, black, or some shade of gray.
Also, note that there is no setPixel() method in the Graphics API, hence we draw little small rectangles.
The use of rectangles also makes it easier to modify the granularity of the resulting images. The higher
the granularity the larger the little gray dots are, but the faster the background images are
produced. By separating the picture into quadrants, I found that I could get away with using
the finest granularity value possible (1) and yet still have acceptable performance during the
background image generation phase.
Since this is an animation loop, the need for a separate animation thread is apparent. But, the thread
is also handy in that it allows us to produce something for the user to look at while the white noise
images are being produced. It's always a good idea to give the user a countdown of some sort
if it will take more than a few seconds to perform some necessary action to initialize your applets. I
have provided it here with a simple countdown of sorts that pretends to be a channel scan. Note the use
of buttons to display messages to the user. You don't normally think of buttons as useful tools
for displaying messages to your users, but they can be the simple, effective solution for doing just that.
As a matter of style, I don't tend to add a lot of comments in my code. Instead, I try to choose meaningful
variable names to convey what is going on. If you have any questions, please feel free to e-mail them to me
and I will try to answer them.
Java4Free! Home