Let's Take Control

Today we will create StarApplet and the RGBColorBox applet, using controls that are available as classes in the standard Java Development Kit.

StarApplet


This applet enables the user to create a star by selecting the number of points (1 to 40), using the vertical scroll bar, and the skip factor (1 to 40), using the horizontal scroll bar. The skip factor is the distance in number of points of the two ends of each line used to build the star. For example, the commonplace five-pointed star that most people draw by hand has 5 points and a skip factor of 2. This is the initial condition in this applet. We will use the notation p, s to denote the configuration of a star, where p is the number of points and s is the skip factor. Note that 5, 3 is equivalent to 5, 2. Try 40, 17 for a forty-point star. Also note that certain configurations form polygons, while others leave the screen blank. Try 5, 1 for a pentagon and 1, 1 for a blank screen. A one-pointed star is infinitesimally small and, therefore, invisible. Two-pointed stars appear as a straight line if s is odd, and are invisible if s is even. Try to figure out why. If p and s have common denominators, then star p, s appears to have less than p points. Try to figure that out, too.

The applet is composed of two custom-made classes, Star to implement stars, and StarApplet to implement the applet. In addition it uses the Scrollbar class and the Graphics class from the Java Development Kit. Play with the applet, then take a look at the source code to see how it was set up.

Source Code



The RGBColorBox Applet




The components utilized by this applet include:

A Label
the RGBColorBox title.
A TextArea
The box that contains the instructions.
A ColorCanvas
The box that displays the color selected by the controls. The ColorCanvas class extends the Canvas class and is implemented in the ColorClass.java file that was written for this applet.
Three Scrollbars
Control the colors.
Three TextFields
Control the colors and display the RGB color values in numerical form.
A Button
Changes the color to its complement.

The code for this applet is stored in the files RGBColorBox.java and ColorCanvas.java.

I have noticed that some of the controls are not completely functional on certain platforms. For instance, on my computer, with Windows NT 3.51, the Scrollbars are missing their scrollboxes in Netscape 3.0, but they display and work properly in the Appletviewer. On another machine, running Windows NT 4.0, I have found that the Scrollbars get stuck, and return values that are not consistent with the position of the scrollboxes. I have heard rumors of very poorly-behaved Scrollbars running on UNIX machines, but it sounds scary, and I am afraid to check it out myself. Evidently, platform independence is still an unrealized ideal.

Last modified March 10, 1997


[Anyone for Java? home]