JClass Elements

PreviousNextIndex

20

Icon Creator

Features of JCIconCreator  Classes  Constructors and Methods  Examples


20.1 Features of JCIconCreator

There are times when you would like to have a custom image as part of your toolbars, labels, buttons, and so on, yet you don't want to go to the trouble of using a paint package. JCIconCreator lets you use String arrays to create image icons. The advantages of using JCIconCreator include:


20.2 Classes

This utility consists of a single class, com.klg.jclass.util.swing.JCIconCreator, subclassed from java.lang.Object.


20.3 Constructors and Methods

The JCIconCreator has two constructors are JCIconCreator(), which creates an uninitialized image icon, and JCIconCreator(int w, int h), where the parameters measure the size, in pixels, of the two dimensional array used to hold the characters representing the image.

Methods in JCIconCreator

The following is a list of the methods available for JCIconCreator:

clear()

Clears the icon so that no image is associated with it.

getIcon()

Gets the icon created by this instance of JCIconCreator. An overloaded version of this method takes a passed-in byte array (as might be obtained from a database's image field) and attempts to convert it into an Image.

 

Use getIcon() method to return an ImageIcon. For example:
ImageIcon myLabelIcon = ic.getIcon();

setColor()

Sets the color corresponding to a character passed as its first parameter. Its second parameter is a Color object or an RGB int.

 

Use the setColor() method to associate a character in the array with a color. For example, if ic is an instance of a JCIconCreator,
ic.setColor('G', Color.green);
associates a "G" in the pixel map with a green pixel.

setPixels()

Sets the pixel data. If its parameter is an array of Strings, this represents the data for all rows. If the parameters are a row index and a String, this represents the pixel data for one row.

 

If pixelMap is an array of characters representing pixels, inform the instance about them with
ic.setPixels(pixelMap);

setSize()

Width and height int parameters are used to set the width and height for the image.


20.4 Examples

The following code section shows how to declare a String array, use it as the source for defining the pixels in an icon, and how to convert the JCIconCreator object to an ImageIcon for use as the graphic part of a label.

...
private static final String testIcon[] = {
" BBBBBBBBB ",
" B OOO B ",
" B OOOOO B ",
" B OOOOO B ",
" B OOOOO B ",
" B OOO B ",
" B B ",
" B B ",
" B B ",
" B B ",
" B B ",
" B B ",
" BBBBBBBBB " };

JButton b1;

public ToolbarIcons() {
JToolBar bar;
JLabel label;

setBackground(Color.lightGray);
setLayout(new BorderLayout());

JCIconCreator ic = new JCIconCreator(13, 13);
ic.setColor('B', Color.black);
ic.setColor('O', Color.orange);
ic.setPixels(testIcon);
ImageIcon icon = ic.getIcon();
...
bar = new JToolBar();
b1 = new JButton("Caution", icon);
bar.add(b1);
...

Figure 52 :  Three labels with custom icons created using JCIconCreator.


PreviousNextIndex