org.inclou.Palette
Class PaletteCanvas

java.lang.Object
  extended byjavax.microedition.lcdui.Displayable
      extended byjavax.microedition.lcdui.Canvas
          extended byorg.inclou.Palette.PaletteCanvas
All Implemented Interfaces:
CommandListener

public class PaletteCanvas
extends Canvas
implements CommandListener

You should note that this is a javax.microedition.lcdui.Canvas subclass and therefore the present library must be used by J2 Micro Edition applications. You should note that the GrayScale screen mode might show non-grayscale colors, as the system maps the RGB color system to the most similar displayable color.

This Canvas subclass shows a palette where one can choose a color. A palette is like a table where each square contains a color. It can be used as library by other J2ME applications such as MIDlets. The palette can contain color squares of a fixed size (as many as fit in the screen) or a determined number of color squares (its size depending on the screen size). It also allows you to preselect a color.

A ChoserInterface interface must be passed to the constructor in order to tell the PaletteCanvas what to do when a color is selected by user, as well as passing to the application the selected color.

Colors can be selected by means of game pad or pointer. Application can select FullColor or GrayScale screen mode by calling setFullColorMode(boolean full), usually before PaletteCanvas is made visible. By default, Full Color mode is initially set. Both palettes will have the same number of rows and cols.

The number of rows (square size recalculated) are rounded so rows is ultiple of 2 and/or 3. It is rounded to the nearest lower value using the PaletteCanvas.facts precalculated table. Rows and cols can also become to different lower values if the display can actually display fewer colors than rows and cols specified in constructor, according to the parameter passed to the constructor.

There is a fixed margin of 3 px between squares. Those in the boundaries have an external margin of 2 px. Canvas should be 5x5px at least. The palette will show 4 rows and 2 cols at least, even if display can actually show only 2 colors for example.

The number of rows and cols, as well as the size of squares, can actually be different than specified in constructor as a result of considerations above.

If you want to know exactly how the PaletteCanvas.facts values have been calculated please refer to PaletteCanvas.java source code (see very bottom).

Canvas diagram Canvas diagram which shows drawing details. This is a 4 rows and 3 columns PaletteCanvas. Width and height of squares are both 3 pixels. This represents to be a 23x39px Canvas. Note that there is 1px left in the right and 2 px left in the bottom.

A grid has been drawed, and gray pixels are those used for drawing the black surrounding rectangle which indicates current square selection (in this case row 0 column 0). Neither grid nor gray pixels are drawn. This is only a diagram, and this colors are arbitrarily selected.

Version:
Version 1.0 of package org.inclou.Palette
This package is MIDP 1.0 compliant.
This package has been compiled under 1.4.2 JDK.
Author:
Joel Samper. This code is published in www.inclou.org/freeware where you will find more php/java libraries. Please refer to this web for use conditions and other documentation.
See Also:
MIDpaletteSelector, ChoserInterface

Field Summary
static byte[] factA
          facts[n] values are already split into two factors for speeding execution.
static byte[] factB
          facts[n] values are already split into two factors for speeding execution.
static short[] facts
          Possible values for numrows parameter in constructor.
static byte SQUARES_SIZE
          Used when specifying a certain size for squares.
static boolean TABLE_SIZE
          Used when specifying a certain number of rows and cols.
 
Fields inherited from class javax.microedition.lcdui.Canvas
DOWN, FIRE, GAME_A, GAME_B, GAME_C, GAME_D, KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9, KEY_POUND, KEY_STAR, LEFT, RIGHT, UP
 
Constructor Summary
PaletteCanvas(ChoserInterface cc)
          Basic constructor.
PaletteCanvas(ChoserInterface cc, int max)
          Basic constructor.
PaletteCanvas(ChoserInterface cc, short numrows, short numcols, int max, boolean b)
          Advanced constructor.
PaletteCanvas(ChoserInterface cc, short squarewidth, short squareheight, int max, byte c)
          Advanced constructor.
 
Method Summary
 void commandAction(Command c, Displayable d)
           
 boolean isInFullColorMode()
          Returns information about what screen color mode of PaletteCanvas will be shown if it is requested to be shown.
protected  void keyPressed(int keyCode)
           
protected  void keyReleased(int keyCode)
           
protected  void keyRepeated(int keyCode)
           
 void paint(Graphics g)
           
protected  void pointerDragged(int x, int y)
           
protected  void pointerPressed(int x, int y)
           
protected  void pointerReleased(int x, int y)
           
 void setColorMode(boolean fullcolor)
          Switches between FullColor and GrayScale palette screen mode.
 void setInterface(ChoserInterface ci)
          Sets desired ChoserInterface, usually used if one want to set a different inferface after instantiation of a PaletteCanvas.
 int setSelected(int c)
          Preselects a color, generally before PaletteCanvas is shown by a main application.
 
Methods inherited from class javax.microedition.lcdui.Canvas
getGameAction, getKeyCode, getKeyName, hasPointerEvents, hasPointerMotionEvents, hasRepeatEvents, hideNotify, isDoubleBuffered, repaint, repaint, serviceRepaints, setFullScreenMode, showNotify
 
Methods inherited from class javax.microedition.lcdui.Displayable
addCommand, getHeight, getTicker, getTitle, getWidth, isShown, removeCommand, setCommandListener, setTicker, setTitle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

facts

public static final short[] facts
Possible values for numrows parameter in constructor.
These are: 4, 6, 8, 9, 12, 16, 18, 24, 27, 32, 36, 48, 54, 64, 72, 81, 96, 108, 128, 144, 162, 192, 216, or 243


factA

public static final byte[] factA
facts[n] values are already split into two factors for speeding execution.
facts[n]=factA[n]*factA[n]


factB

public static final byte[] factB
facts[n] values are already split into two factors for speeding execution.
facts[n]=factA[n]*factA[n]


TABLE_SIZE

public static final boolean TABLE_SIZE
Used when specifying a certain number of rows and cols. Square size is calculated according to this.

See Also:
Constant Field Values

SQUARES_SIZE

public static final byte SQUARES_SIZE
Used when specifying a certain size for squares. Number of rows and columns are calculated according to this.

See Also:
Constant Field Values
Constructor Detail

PaletteCanvas

public PaletteCanvas(ChoserInterface cc)
Basic constructor. This is equivalent to call PaletteCanvas(cc, 0).

Parameters:
cc - Interface that must be called when a color is selected.

PaletteCanvas

public PaletteCanvas(ChoserInterface cc,
                     int max)
Basic constructor.

Parameters:
cc - Interface that must be called when a color is selected.
max - Maximum number of colors that the screen can actually display, or desired maximum number of displayable colors. If set to 0 or less, no boundaries are regarded.

PaletteCanvas

public PaletteCanvas(ChoserInterface cc,
                     short squarewidth,
                     short squareheight,
                     int max,
                     byte c)
Advanced constructor. Actual square size may actually be reduced in order to show 4 rows and 2 columns at least or for Canvas resizing. Number of columns and rows are calculated accordingly to complete screen. Size will not be less than 1x1.

Parameters:
cc - Interface that must be called when a color is selected.
max - Maximum number of colors that the screen can actually display, or desired maximum number of displayable colors. If set to 0 or less, no boundaries are regarded.
squarewidth - Desired square width for each color of the palette.
squareheight - Desired square height for each color of the palette.
c - This argument has no effect. SQUARES_SIZE static value can be used. It only selects this type of constructor and not PaletteCanvas (ColorChoser cc, int numrows, int numcols, int max, boolean b).
See Also:
SQUARES_SIZE, TABLE_SIZE

PaletteCanvas

public PaletteCanvas(ChoserInterface cc,
                     short numrows,
                     short numcols,
                     int max,
                     boolean b)
Advanced constructor. Actual number of rows and columns shown may actually be reduced to a subset as a result of applying predefined values for rows, maximum number of colors displayable, minimum size of 1x1 for squares and/or Canvas resizing. Squares size is calculated accordingly to complete screen.

Parameters:
cc - Interface that must be called when a color is selected.
max - Maximum number of colors that the screen can actually display, or desired maximum number of displayable colors. If set to 0 or less, no boundaries are regarded.
numrows - Desired number of rows of the palette.
numcols - Desired number of columns of the palette.
b - This argument has no effect. TABLE_SIZE static value can be used. It only selects the type of constructor, not PaletteCanvas (ColorChoser cc, int squarewidth, int squareheight, int max, char c).
See Also:
SQUARES_SIZE, TABLE_SIZE
Method Detail

isInFullColorMode

public boolean isInFullColorMode()
Returns information about what screen color mode of PaletteCanvas will be shown if it is requested to be shown.

Returns:
True if Full Color; false if Gray Scale.

setInterface

public void setInterface(ChoserInterface ci)
Sets desired ChoserInterface, usually used if one want to set a different inferface after instantiation of a PaletteCanvas.

Parameters:
ci - ChoserInterface pointer desired to handle PaletteCanvas events.

setSelected

public int setSelected(int c)
Preselects a color, generally before PaletteCanvas is shown by a main application. An application may wish to have a color preselected. Note that actually the MOST SIMILAR color is selected from the set of displayed colors in the palette (only calculated for the current screen mode).

Parameters:
c - Color that is wanted to be preselected, in RGB format. Filtered with c&0xFFFFFF so bits in the left are ignored. If it is less than 0 no action is performed.
Returns:
Color actually preselected due to considerations above; -1 if a number less than 0 was specified.

paint

public void paint(Graphics g)

commandAction

public void commandAction(Command c,
                          Displayable d)
Specified by:
commandAction in interface CommandListener

setColorMode

public void setColorMode(boolean fullcolor)
Switches between FullColor and GrayScale palette screen mode. It should be called before PaletteCanvas is displayed. This method does not commit a repaint() call. FullColor mode is the default initial mode.

Parameters:
fullcolor - True if Full Color screen mode desired. False if Gray Scale mode desired.

keyPressed

protected void keyPressed(int keyCode)

keyReleased

protected void keyReleased(int keyCode)

keyRepeated

protected void keyRepeated(int keyCode)

pointerDragged

protected void pointerDragged(int x,
                              int y)

pointerPressed

protected void pointerPressed(int x,
                              int y)

pointerReleased

protected void pointerReleased(int x,
                               int y)