pixel
pixel
header
    November 27, 2002    

In this Issue

imageJava Programming Language Basics
imageJava Bits
imageMaking Sense of the Java Class Libraries
imageProgram Challenge
imageNew to Java Programming Center Update
imageFor More Information

Pixel

Java Programming Language Basics

How to Create Colors

When I was growing up, a large box of Crayola crayons included 64 colors. Nowadays, the large box has 96 crayons, and Crayola has a core set of 120 crayon colors, including some of the scratch-and-sniff variety. There have been over 400 different crayon colors over their 100-year history.

To get different colored crayons, color pigments are mixed. For example, mixing red and yellow creates orange. On computers, things work differently. Instead of mixing color pigments, you mix shades of light. To get yellow, you actually have to mix red and green.

A mixture of three different light intensities represents all colors. Those three colors are red, green, and blue, frequently referred to as RGB for short. For each of the three lights, there are 256 possible values or 16,777,216 (256 * 256 * 256) different shades in all. And within the standard libraries for the Java 2 Platform is the java.awt.Color class to represent those colors.

For commonly used colors, the Color class contains two sets of thirteen constants. One set has the thirteen common colors in all lowercase letters, as in red, green, and blue, while the second set uses all uppercase letters, as in RED, GREEN, and BLUE. Originally, the only set was all the lowercase constants. The 1.4 release of the Java 2 Standard Edition introduced the second all-uppercase set. While the original set has not been deprecated, the new set now follows the naming conventions of other public constants.

In addition to the 16 million colors available through mixing red, green, and blue lights, there is a fourth option called opacity. This fourth option specifies how opaque, or dense, a particular color setting is, and is also called an alpha channel.

The more opaque a color is, the less transparent it is. Like the red, green, and blue settings, opacity also offers a range of 256 values, giving you a total of 4,294,967,296 variations. Keep in mind that while the Color class supports over 4 billion different color settings, not all user graphic cards do. While most newer computers do, one should try to limit the number of different colors used to some reasonable subset.

The combination of the four possible color settings of alpha, red, green, and blue all fit within an int variable. With each offering 256 settings, that's 8 bits worth of data. So, 4 settings times 8 bits is 32 bits, which is exactly the size of an int. From left to right, those 256 settings in 8 bits are stored alpha first, then red, then green, and finally blue, each being stored as an unsigned value from 0-255.

You can create a color in a number of ways. As previously stated, you can use one of the predefined constants, like RED, GREEN, or BLUE. There are also brighter() and darker() methods that adjust an existing color accordingly. More typical, though, is to create a new Color by providing a set of integer values for the red, green, and blue values, with an optional setting for transparency. Without the last setting, colors are opaque (no transparency).

For example, there are several different ways to get the color red. The first two are the RED and red constants. Both of these provide you with no green or blue light when the color is used to draw a pixel on the screen.

You can also get red by calling the constructor for Color, and passing in the values of 255 for red with 0 for blue and green:

Color red = new Color(255, 0, 0). 

As the final way of getting red, you can combine the four settings for red, green, and blue with transparency by call ing yet another constructor:

int value = ((255 & 0xFF) << 16);
Color red = new Color(value);

This puts the setting for red (255) into the second byte (from the left) of the int. Of course, you could specify each of the settings separately to initialize the color setting:

int value =
     ((alpha & 0xFF) << 24) |
     ((red & 0xFF) << 16) |
     ((green & 0xFF) << 8) |
     ((blue & 0xFF) << 0);

The Color constructor then takes this setting as one value, a new Color(value), instead of as separate settings for alpha, red, green, and blue.

Once you have your Color object, you can use it to paint the screen or set the foreground/background color of a component, among many other tasks.

The secondary mechanism for manipulating colors is through HSB settings, which stands for hue, saturation, and brightness.

There is also the java.awt.SystemColor class, which provides access to the colors a user's specific platform offers. These last settings let you query which colors a component should be so when you create new versions of those components they look like the actual system components.

Test what you just learned about creating colors with this interactive quiz.

Pixel
Pixel

Java Bits

Play Audio Clips

The Java API provides a simple abstraction for playing a sound clip. The process works similarly to retrieving and displaying an image. The AudioClip interface represents an audio file. An AudioClip object created using the getAudioClip or newAduioClip methods have access to the methods in the AudioClip interface.

The three methods are:

  • loop - runs the audio clip by invoking the AudioClip object continuously.
  • play - plays the audio clip represented by the invoking AudioClip object once. Each time this method is called, the clip is restarted from the beginning.
  • stop - stops playing the audio clip.

Common audio formats used in applications or applets are WAV, AU, and SND.

The WhaleSounds applet demonstrates how to create an AudioClip object, and play and stop the sound clip with JButtons and the appropriate methods. To recreate this applet on your system, you need to download by right-clicking and selecting Save Link As:

After compiling and running the applet, you'll see the following:

Pixel
Pixel

Making Sense of the Java Class Libraries

The JSlider Class

Slider objects allow users to graphically choose a numeric value bounded by a minimum and maximum value, such as volume, color brightness, size of objects, and so forth.

Fortunately, the Java API comes with a handy class that allows developers to create customized sliders for specific application needs. This class is packaged as a part of the Swing library, javax.swing.JSlider.

Sliders can show whatever range of numbers you need, as well as major tick marks and minor tick marks between them.

To create a Slider object, call one of the following constructors:

  • JSlider()
    Creates a horizontal slider with the range 0 to 100 and an initial value of 50.
  • JSlider(int min, int max)
    JSlider(int min, int max, int value)
    Creates a horizontal slider with the specified minimum and maximum values. The third int argument, when present, specifies the slider's initial value.
  • JSlider(BoundedRangeModel)
    Creates a horizontal slider with the specified model, which manages the slider's minimum, maximum, and current values and their relationship.

To work with the slider labels, tick marks, and orientation, the JSlider class has many methods available. Here are a few:

  • void setMinimum(int)
    void setMaximum(int)
    Set the minimum or maximum values of the slider. Together, these methods set or get the slider's range.
  • void setPaintTicks(boolean)
    Set whether tick marks are painted on the slider.

To add functionality to a slider, you implement ChangeListener and provide a stateChanged method, where you write the instructions for what you want to happen when the slider is moved.

The following program illustrates a JSlider constructor and methods to create one vertical slider and one horizontal slider. When you move the slider button, the stateChanged method is called, and the value for the slider appears at the bottom of the screen in a text area.

JSliderExample.java produces the following:

Pixel
Pixel

Program Challenge

Create the ColorChanger Application

  1. Create a program that allows the user to set the background color of a label based upon the current setting of three sliders:
    • Red
    • Green
    • Blue
  2. Provide two buttons for changing the color to be brighter or darker. When either of those buttons are selected, the background color changes and the sliders adjust to their new settings.

See a possible solution to Challenge:

Pixel
Pixel

New to Java Programming Center Update

Read through the articles and tutorials in the New to Java Programming center a step at a time, then test what you learned in the newly added Step 4: Review What You've Learned. Step 4 includes links to interactive online quizzes and puzzles.

Pixel
Pixel

For More Information

Class Color

Class JSlider

How to Use Sliders

Interface AudioClip

Pixel
Pixel

Program Challenge Solution

See one possible solution to the November Program Challenge:

Pixel
Pixel

Downloading the Java 2 Platform

For most Java development, you need the class libraries, compiler, tools, and runtime environment provided with the J2SE development kit.

Pixel
Pixel

Reader Feedback

  Very worth reading    Worth reading    Not worth reading 

If you have other comments or ideas for future newsletters, please type them here:

 

Have a question about Java programming? Use Java Online Support.

Pixel
Pixel
Pixel

Subscribe to the following newsletters for the latest information about technologies and products in other Java platforms:

- Core Java Technologies Newsletter. Learn about new products, tools, resources, and events of interest to developers working with core Java technologies.
- Wireless Developer Newsletter. Learn about the latest releases, tools, and resources for developers working on wireless and Java Card technologies and applications.
- Core Java Technologies Tech Tips (formerly JDC Tech Tips) Get expert tips, sample code solutions, and techniques for developing in the Java 2 Platform, Standard Edition (J2SE)
You can subscribe to these and other JDC publications on the JDC Newsletters and Publications page

IMPORTANT: Please read our Terms of Use, Privacy, and Licensing policies:
http://www.sun.com/share/text/termsofuse.html
http://www.sun.com/privacy/
http://developer.java.sun.com/berkeley_license.html


Comments? Send your feedback on the Java Technology Fundamentals Newsletter to: dana.nourie@sun.com

Go to the subscriptions page to subscribe or unsubscribe to this newsletter.

ARCHIVES: You'll find the Java Technology Fundamentals Newsletter archives at:
http://java.sun.com/developer/onlineTraining/new2java/supplements/


Copyright 2002 Sun Microsystems, Inc. All rights reserved. 901 San Antonio Road, Palo Alto, California 94303 USA.

Sun, Sun Microsystems, Java, J2SE are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.

Sun Microsystems, Inc.
image
image