Java Reference
In-Depth Information
Enabling elements to be rotated just involves adding an extra transform following the translation trans-
form before each element is drawn. Because you a transform and draw each element at the origin, rotat-
ing an element becomes extremely simple. The only real work was implementing the GUI process.
CHOOSING CUSTOM COLORS
It is rather boring only having four colors for elements. It would be a shame to limit Sketcher like this, so
let's add a GUI capability to enable any color to be chosen. This is going to be easier than you might ima-
gine, but it's not trivial. Along the way you not only learn about a new standard dialog for choosing colors,
you also learn how to modify icon images programmatically and how to get information about text layout.
First, add new icons to the SketcherConstants class:
public final static ImageIcon CUSTOM24 =
new ImageIcon(new BufferedImage(24, 24, BufferedImage.TYPE_INT_ARGB));
public final static ImageIcon CUSTOM16 =
new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB));
These are the icons you use with the menu item and toolbar button for custom color selection. You
display CUSTOM16 in the status bar when the element color is a selected color. The icon references are of type
java.swing.ImageIcon rather than type Icon like the others, because you are calling some of the methods
that the ImageIcon class defines. Of course, you could make all of the icon references type ImageIcon if
you want. These statements create ImageIcon objects from a java.awt.image.BufferedImage object. Al-
though CUSTOM16 and CUSTOM24 are declared as final , you can still call their methods, and the getImage()
method returns a reference to the image the icon contains as type Image , which is an abstract class type.
Because the image reference is a BufferedImage , you can cast it to that type and then do all kinds of things
with it, including modifying it. Although the CUSTOM16 and CUSTOM24 objects are immutable, the images
they contain are not.
You must add the following import statement to SketcherConstants.java :
import java.awt.image.BufferedImage;
Using a Buffered Image
The arguments to the BufferedImage constructor you are using are the width of the image, the height of the
image, and the image type. All three arguments are values of type int . You can create a BufferedImage
object with a variety of different representations for the underlying image. A range of standard types are
defined as static fields of type int in the BufferedImage class. The one you use to create the underlying
images for the custom color icons is TYPE_INT_ARGB , which represents each pixel as four 8-bit bytes in an
integer that are the alpha channel, and the red, green, and blue color components. The alpha channel determ-
ines the transparency, 0 being completely transparent and 255 being completely opaque. There are 13 other
types, including types with no alpha channel and grayscale images.
The createGraphics() method for a BufferedImage object returns a reference to a Graphics2D object
that is the graphics context for the image. You can then use the Graphics2D methods that you have already
Search WWH ::




Custom Search