FileDocCategorySizeDatePackage
ColorChooser.javaAPI DocJ2ME MIDP 2.011139Thu Nov 07 12:02:16 GMT 2002example.chooser

ColorChooser

public class ColorChooser extends Canvas
A Color chooser. This screen can be used to display and choose colors. The current color is always available via the getColor and getGrayScale methods. It can be set with setColor. A palette provides some reuse of colors, the current index in the palette can get set and retrieved. When the chooser is active the user may set the index in the palette, and change the red, green, and blue components. The application using the chooser must add commands to the chooser as appropriate to terminate selection and to change to other screens. The chooser adapts to the available screen size and font sizes.

Fields Summary
private int
width
private int
height
private Font
font
private int
label_w
private int
label_h
private int
gray
private int
rgbColor
private int
radix
private int
delta
private int
ndx
private boolean
isColor
private int
numColors
private int
pndx
private int[]
palette
static final int
BORDER
Constructors Summary
public ColorChooser(boolean isColor, int numColors)

		     
    
         
	this.isColor = isColor;
	this.numColors = numColors;
	ndx = 0;
	pndx = 0;
	setColor(palette[pndx]);

	width = getWidth();
	height = getHeight();

	font = Font.getDefaultFont();
	label_h = font.getHeight();
	if (label_h > (height / 6)) {
	    font = Font.getFont(Font.FACE_SYSTEM,
				Font.STYLE_PLAIN,
				Font.SIZE_SMALL);
	    label_h = font.getHeight();
	}
	label_w = font.stringWidth("999");
    
Methods Summary
private voidcolorPaint(Graphics g)


        
	// Scale palette cells to fit 10 across
	int p_w = width / palette.length;
	int p_h = (height - (BORDER*3)) / 4;
	int usable_w = p_w * palette.length;

	int sample_w = p_w * palette.length - 1;
	int sample_h = p_h;

	int sample_x = (width - usable_w) / 2;
	int sample_y = 0;
	int p_x = sample_x;
	int p_y = sample_y + sample_h + 4;

	// Fill the background
	g.setColor(0xffffff);
	g.fillRect(0, 0, width, height);

	// Fill in the color sample
	g.setColor(rgbColor);
	gray = g.getGrayScale();
	g.fillRect(sample_x, sample_y, sample_w, sample_h);
	g.setColor((ndx < 0) ? 0x000000 : 0x808080);
	g.drawRect(sample_x, sample_y, sample_w-1, sample_h-1);

	// Draw the palette
	for (int i = 0; i < palette.length; i++) {
	    g.setColor(palette[i]);
	    int shift = ((i == pndx) ? BORDER*2 : 0);
	    g.fillRect(p_x + i*p_w, p_y-shift, p_w-1, p_h);

	    // If the palette is selected, outline it
	    if (i == pndx) {
		g.setColor((ndx < 0) ? 0x000000 : 0x808080);
		g.drawRect(p_x + i*p_w, p_y-shift-1, p_w-2, p_h+1);
	    }
	}

	int bars_x = sample_x;
	int bars_y = p_y + p_h + BORDER;

	int bar_h = label_h + BORDER;
	int bar_w = usable_w - label_w - BORDER;

	int b_x = label_w + BORDER;
	int b_y = bars_y + BORDER;
	int g_y = b_y + bar_h;
	int r_y = g_y + bar_h;

	// Draw the colorbars
	g.setColor(0, 0, 255);
	int b_w = (bar_w * getBlueComponent()) / 255;
	g.fillRect(b_x, b_y, b_w, bar_h - BORDER);
	g.setColor((ndx == 0) ? 0x000000 : 0xa0ffa0);
	g.drawRect(b_x, b_y, bar_w - 1, bar_h - BORDER - 1);

	int g_w = (bar_w * getGreenComponent()) / 255;
	g.setColor(0, 255, 0);
	g.fillRect(b_x, g_y, g_w, bar_h - BORDER);
	g.setColor((ndx == 1) ? 0x000000 : 0xa0ffa0);
	g.drawRect(b_x, g_y, bar_w - 1, bar_h - BORDER - 1);

	int r_w = (bar_w * getRedComponent()) / 255;
	g.setColor(255, 0, 0);
	g.fillRect(b_x, r_y, r_w, bar_h - BORDER);
	g.setColor((ndx == 2) ? 0x000000 : 0xffa0a0);
	g.drawRect(b_x, r_y, bar_w - 1, bar_h - BORDER - 1);

	g.setFont(font);
	g.setColor(0, 0, 0);
	g.drawString(format(getBlueComponent()), 
		     label_w, b_y+bar_h,  Graphics.BOTTOM|Graphics.RIGHT);
	g.drawString(format(getGreenComponent()),
		     label_w, g_y+bar_h,  Graphics.BOTTOM|Graphics.RIGHT);
	g.drawString(format(getRedComponent()),
		     label_w, r_y+bar_h,  Graphics.BOTTOM|Graphics.RIGHT);
    
private java.lang.Stringformat(int num)

	String s = Integer.toString(num, radix);
	if (radix == 10 || s.length() >= 2)
	    return s;
	return "0" + s;
    
public intgetBlueComponent()
Gets the blue component of the current color.

return
integer value in range 0-255
see
#setColor(int, int, int)

        return rgbColor & 0xff;
    
public intgetColor()
Gets the current color.

return
an integer in form 0x00RRGGBB
see
#setColor(int, int, int)

        return rgbColor;
    
public intgetDelta()
Get the delta used to increment/decrement.

	return delta;
    
public intgetGrayScale()

	return gray;
    
public intgetGreenComponent()
Gets the green component of the current color.

return
integer value in range 0-255
see
#setColor(int, int, int)

        return (rgbColor >> 8) & 0xff;
    
public intgetPaletteIndex()
Get the current palette index.

return
the current index in the palette.

	return ndx;
    
public intgetRadix()
Get the radix used to display numbers.

	return radix;
    
public intgetRedComponent()
Gets the red component of the current color.

return
integer value in range 0-255
see
#setColor(int, int, int)

        return (rgbColor >> 16) & 0xff;
    
private voidgrayPaint(Graphics g)

	int sample_w = width;
	int sample_h = height / 2;
	int g_y = height / 2;
	int g_w = width - (label_w + BORDER);
	int g_h = label_h + BORDER;

	// Fill the background
	g.setGrayScale(0xff);
	g.fillRect(0, 0, width, height);

	// Fill in the gray sample
	g.setGrayScale(gray);
	g.fillRect(0, 0, sample_w, sample_h);
	g.setGrayScale(0);
	g.drawRect(0, 0, sample_w-1, sample_h-1);

	// Fill in the gray bar
	g.setGrayScale(0);
	g.fillRect(label_w+BORDER, g_y + BORDER, (g_w * gray) / 255, g_h);
	g.drawRect(label_w+BORDER, g_y + BORDER, g_w-1, g_h-1);
	g.drawString(format(gray), label_w,
		     g_y+BORDER + g_h,  Graphics.BOTTOM|Graphics.RIGHT);
    
protected voidkeyPressed(int key)

	int action = getGameAction(key);
	int dir = 0;
	switch (action) {
	case RIGHT: dir += 1; break;
	case LEFT: dir -= 1; break;
	case UP: ndx -= 1; break;
	case DOWN: ndx += 1; break;
	default:
	    return;		// nothing we recognize, exit
	}
	
	// Gray scale event handling is simpler than color
	if (isColor) {
	    // Limit selection to r,g,b and palette 
	    if (ndx < -1)
		ndx = -1;
	    if (ndx > 2)
		ndx = 2;
	    if (ndx >= 0) {
		int v = (rgbColor >> (ndx*8)) & 0xff;
		v += dir * delta;
		if (v < 0)
		    v = 0;
		if (v > 255)
		    v = 255;
		int mask = 0xff << (ndx*8);
		rgbColor = (rgbColor & ~mask) | v << (ndx*8);
		palette[pndx] = rgbColor;
	    } else {
		pndx += dir;
		if (pndx < 0)
		    pndx = 0;
		if (pndx >= palette.length)
		    pndx = palette.length-1;
		rgbColor = palette[pndx];
	    }
	} else {
	    /*
	     * Gray scale; multiple dir and add to gray
	     * ignore (up/down) there is only one thing to select.
	     */
	    gray += dir * delta;
	    if (gray < 0)
		gray = 0;
	    if (gray > 255)
		gray = 255;
	}

	repaint();
    
public voidkeyRepeated(int key)

	keyPressed(key);
    
protected voidpaint(Graphics g)

	if (isColor) {
	    colorPaint(g);
	} else {
	    grayPaint(g);
	}
    
public voidsetColor(int red, int green, int blue)
Sets the current color to the specified RGB values.

param
red The red component of the color being set in range 0-255.
param
green The green component of the color being set in range 0-255.
param
blue The blue component of the color being set in range 0-255.

	red   = (red   < 0) ? 0 : (red   & 0xff);
	green = (green < 0) ? 0 : (green & 0xff);
	blue  = (blue  < 0) ? 0 : (blue  & 0xff);

	setColor((red << 16) | (green << 8) | blue);
    
public voidsetColor(int RGB)
Sets the current color to the specified RGB values. All subsequent rendering operations will use this specified color. The RGB value passed in is interpreted with the least significant eight bits giving the blue component, the next eight more significant bits giving the green component, and the next eight more significant bits giving the red component. That is to say, the color component is specified like 0x00RRGGBB.

param
RGB The color being set.

	rgbColor = RGB & 0x00ffffff;
	palette[pndx] = rgbColor;
	updateGray();
    
public voidsetDelta(int delta)
Set the delta used to increment/decrement. The default is 32.

	if (delta > 0 && delta <= 128) {
	    this.delta = delta;
	}
    
public voidsetGrayScale(int value)
Sets the current grayscale. For color the value is used to set each component.

param
the value in range 0-255

        setColor(value, value, value);
    
public booleansetPaletteIndex(int index)
Select which entry in the Palette to use for the current color.

param
index index into the palette; 0..10.

	if (index >= palette.length)
	    return false;

	pndx = index;
	setColor(palette[index]);
	return true;
    
public voidsetRadix(int rad)
Set the radix used to display numbers. The default is decimal (10).

	if (rad != 10 && rad != 16) {
	    throw new IllegalArgumentException();
	}
	radix = rad;
        repaint();
    
protected voidshowNotify()
The canvas is being displayed. Compute the relative placement of items the depend on the screen size.

    
private voidupdateGray()

	/*
	 * REMIND: change this, if the spec allows it
	 *
	 * Gray is the value according to HSV.  I think it would
	 * make more sense to use NTSC gray, but that's not what
	 * is currently in the spec.   -- rib 20 Mar 2000
	 */
	gray = Math.max((rgbColor >> 16) & 0xff,
			Math.max((rgbColor >> 8) & 0xff, rgbColor & 0xff));