Filling Shapes

Iguana fills its shapes with a number of colors, using the setPaint( ) method of Graphics2D. setPaint( ) sets the current color in the graphics context, so we set it to a different color before each drawing operation. This method accepts any object that implements the Paint interface. The 2D API includes three implementations of this interface, representing solid colors, color gradients, and textures.

Solid Colors

The java.awt.Color class handles color in Java. A Color object describes a single color. You can create an arbitrary Color by specifying the red, green, and blue values, either as integers between 0 and 255 or as floating-point values between 0.0 and 1.0. You can also use getColor( ) to look up a named color in the system properties table, as described in Chapter 9. getColor( ) takes a String color property name, retrieves the integer value from the Properties list, and returns the Color object that corresponds to that color.

The Color class also defines a number of static final color values; these are what we used in the Iguana example. These constants, such as Color.black and Color.red, provide a convenient set of basic color objects for your drawings.

Color Gradients

A color gradient is a smooth blend from one color to another. The GradientPaint class encapsulates this idea in a handy implementation of the Paint interface. All you need to do is specify two points and the color at each point. The GradientPaint will take care of the details so that the color fades smoothly from one point to the other. For example, in the previous example, the ellipse is filled with a gradient this way:

g2.setPaint(new GradientPaint(40, 40, Color.blue,
    60, 50, Color.white, true));

The last parameter in GradientPaint’s constructor determines whether the gradient is cyclic. In a cyclic gradient, the colors keep fluctuating beyond the two points that you’ve specified. Otherwise, the gradient just draws a single blend from one point to the other. Beyond each endpoint, the color is solid.

Textures

A texture is simply an image that is repeated over and over like a floor tile. This concept is represented in the 2D API with the TexturePaint class. To create a texture, just specify the image to be used and the rectangle that will be used to reproduce it. But to do this, you need to know how to create and use images, which we’ll get to a little later.

Desktop Colors

The Color class makes it easy to construct a particular color; however, that’s not always what you want to do. Sometimes you want to match a preexisting color scheme. This is particularly important when you are designing a user interface; you might want your components to have the same colors as other components on that platform, and to change automatically if the user redefines his or her color scheme.

That’s what the SystemColor class is for. A system color represents the color used by the local windowing system in a certain context. The SystemColor class holds lots of predefined system colors, just like the Color class holds some predefined basic colors. For example, the field activeCaption represents the color used for the background of the title bar of an active window; activeCaptionText represents the color used for the title itself. menu represents the background color of menu selections; menuText represents the color of a menu item’s text when it is not selected; textHighlightText is the color used when the menu item is selected; and so on. You could use the window value to set the color of a Window to match the other windows on the user’s screen—whether or not they’re generated by Java programs.

myWindow.setBackground( SystemColor.window );

Because the SystemColor class is a subclass of Color, you can use it wherever you would use a Color. However, the SystemColor constants are tricky. They are constants as far as you, the programmer, are concerned; your code is not allowed to modify them. However, they can be modified at runtime. If the user changes his color scheme, the system colors are automatically updated to follow suit; as a result, anything displayed with system colors will also change color the next time it is redrawn. For example, the window myWindow would automatically change its background color to the new background color.

The SystemColor class has one noticeable shortcoming. You can’t compare a system color to a Color directly; the Color.equals( ) method doesn’t return reliable results. For example, if you want to find out whether the window background color is red, you can’t call:

Color.red.equals(SystemColor.window);

Instead, you should use getRGB( ) to find the color components of both objects and compare them, rather than comparing the objects themselves.

Get Learning Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.