de.enough.polish.util
Class DrawUtil

java.lang.Object
  extended by de.enough.polish.util.DrawUtil

public final class DrawUtil
extends java.lang.Object

Provides functions for drawing shadows, polygons, gradients, etc.

Copyright (c) Enough Software 2005 - 2008

The implementation for filling a polygon on devices without Nokia-UI-API and without the BlackBerry API is based upon JMicroPolygon: http://sourceforge.net/projects/jmicropolygon, which is licensed under the Apache Software License

 history
        Nov 23, 2005 - rob creation
 

Author:
Robert Virkus, j2mepolish@enough.de

Nested Class Summary
static class DrawUtil.FadeUtil
          This class is used for fadeEffects (FadeTextEffect and FadinAlienGlowEffect).
 
Field Summary
static byte[][] FILTER_GAUSSIAN_2
           
static byte[][] FILTER_GAUSSIAN_3
           
 
Constructor Summary
DrawUtil()
           
 
Method Summary
static void applyFilter(byte[][] filterMatrix, int brightness, int[] argbData, int width, int height)
          Performs a convolution of an image with a given matrix.
static void drawLine(int color, int x1, int y1, int x2, int y2, javax.microedition.lcdui.Graphics g)
          Draws a translucent line on MIDP 2.0+ and Nokia-UI-API devices.
static void drawPolygon(int[] xPoints, int[] yPoints, int color, javax.microedition.lcdui.Graphics g)
          Draws a polygon.
static void drawRgb(int[] rgb, int x, int y, int width, int height, boolean processAlpha, javax.microedition.lcdui.Graphics g)
          Draws an (A)RGB array and fits it into the clipping area.
static void drawRgb(int[] rgb, int x, int y, int width, int height, boolean processAlpha, int clipX, int clipY, int clipWidth, int clipHeight, javax.microedition.lcdui.Graphics g)
          Draws an (A)RGB array and fits it into the clipping area.
static void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3, javax.microedition.lcdui.Graphics g)
          Draws the specified triangle.
static void dropShadow(int[] argbData, int width, int height, int xOffset, int yOffset, int size, int innerColor, int outerColor)
          Paints a dropshadow behind a given ARGB-Array, whereas you are able to specify the shadows inner and outer color.
static void fillPolygon(int[] xPoints, int[] yPoints, int color, javax.microedition.lcdui.Graphics g)
          Draws a filled out polygon.
static void fillRect(int x, int y, int width, int height, int color, javax.microedition.lcdui.Graphics g)
          Draws a (translucent) filled out rectangle.
static void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, javax.microedition.lcdui.Graphics g)
          Fills the specified triangle.
static int getCenter(int n1, int n2, int n3)
          Retrieves the center position of all numbers
static int getComplementaryColor(int color)
          Retrieves the complementary color to the specified one.
static int[] getGradient(int startColor, int endColor, int steps)
          Creates a gradient of colors.
static void getGradient(int startColor, int endColor, int[] gradient)
          Creates a gradient of colors.
static int getGradientColor(int startColor, int endColor, int permille)
          Retrieves the gradient color between the given start and end colors.
static int getGradientColor(int startColor, int endColor, int step, int numberOfSteps)
          Retrieves the gradient color between the given start and end colors.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILTER_GAUSSIAN_2

public static byte[][] FILTER_GAUSSIAN_2

FILTER_GAUSSIAN_3

public static byte[][] FILTER_GAUSSIAN_3
Constructor Detail

DrawUtil

public DrawUtil()
Method Detail

fillRect

public static final void fillRect(int x,
                                  int y,
                                  int width,
                                  int height,
                                  int color,
                                  javax.microedition.lcdui.Graphics g)
Draws a (translucent) filled out rectangle. Please note that this method has to create temporary arrays for pure MIDP 2.0 devices each time it is called, using a TranslucentSimpleBackground is probably less resource intensiv.

Parameters:
x - the horizontal start position
y - the vertical start position
width - the width of the rectangle
height - the heigh of the rectanglet
color - the argb color of the rectangle, when there is no alpha value (color & 0xff000000 == 0), the traditional g.fillRect() method is called
g - the graphics context
See Also:
TranslucentSimpleBackground

drawPolygon

public static void drawPolygon(int[] xPoints,
                               int[] yPoints,
                               int color,
                               javax.microedition.lcdui.Graphics g)
Draws a polygon.

Parameters:
xPoints - the x coordinates of the polygon
yPoints - the y coordinates of the polygon
color - the color of the polygon
g - the graphics context

fillPolygon

public static final void fillPolygon(int[] xPoints,
                                     int[] yPoints,
                                     int color,
                                     javax.microedition.lcdui.Graphics g)
Draws a filled out polygon.

Parameters:
xPoints - the x coordinates of the polygon
yPoints - the y coordinates of the polygon
color - the color of the polygon
g - the graphics context

fillTriangle

public static void fillTriangle(int x1,
                                int y1,
                                int x2,
                                int y2,
                                int x3,
                                int y3,
                                javax.microedition.lcdui.Graphics g)
Fills the specified triangle.

Parameters:
x1 - the x coordinate of the first vertex of the triangle
y1 - the y coordinate of the first vertex of the triangle
x2 - the x coordinate of the second vertex of the triangle
y2 - the y coordinate of the second vertex of the triangle
x3 - the x coordinate of the third vertex of the triangle
y3 - the y coordinate of the third vertex of the triangle
g - the graphics context

getCenter

public static int getCenter(int n1,
                            int n2,
                            int n3)
Retrieves the center position of all numbers

Parameters:
n1 - first number
n2 - second number
n3 - third number
Returns:
the center of all numbers: min( n1, n2, n3 ) + (max( n1, n2, n3 ) - min( n1, n2, n3 )) / 2

drawTriangle

public static void drawTriangle(int x1,
                                int y1,
                                int x2,
                                int y2,
                                int x3,
                                int y3,
                                javax.microedition.lcdui.Graphics g)
Draws the specified triangle.

Parameters:
x1 - the x coordinate of the first vertex of the triangle
y1 - the y coordinate of the first vertex of the triangle
x2 - the x coordinate of the second vertex of the triangle
y2 - the y coordinate of the second vertex of the triangle
x3 - the x coordinate of the third vertex of the triangle
y3 - the y coordinate of the third vertex of the triangle
g - the graphics context

getGradient

public static final int[] getGradient(int startColor,
                                      int endColor,
                                      int steps)
Creates a gradient of colors. This method is highly optimized and only uses bit-shifting and additions (no multitplication nor devision), but it will create a new integer array in each call.

Parameters:
startColor - the first color
endColor - the last color
steps - the number of colors in the gradient, when 2 is given, the first one will be the startColor and the second one will the endColor.
Returns:
an int array with the gradient.
See Also:
getGradient(int, int, int[]), getGradientColor(int, int, int)

getGradient

public static final void getGradient(int startColor,
                                     int endColor,
                                     int[] gradient)
Creates a gradient of colors. This method is highly optimized and only uses bit-shifting and additions (no multitplication nor devision).

Parameters:
startColor - the first color
endColor - the last color
gradient - the array in which the gradient colors are stored.
See Also:
getGradientColor(int, int, int, int)

getGradientColor

public static final int getGradientColor(int startColor,
                                         int endColor,
                                         int permille)
Retrieves the gradient color between the given start and end colors.

Parameters:
startColor - the start color
endColor - the end color
permille - the permille between 0 and 1000 - 0 will return the startColor, 1000 the endColor, 500 a gradient color directly in the middlet between start and endcolor.
Returns:
the gradient color

getGradientColor

public static final int getGradientColor(int startColor,
                                         int endColor,
                                         int step,
                                         int numberOfSteps)
Retrieves the gradient color between the given start and end colors. This method returns getGradientColor(startColor, endColor, (step * 1000)/numberOfSteps);

Parameters:
startColor - the start color
endColor - the end color
step - the step/position within the gradient
numberOfSteps - the maxium step (=100%)
Returns:
the gradient color
See Also:
getGradientColor(int, int, int)

getComplementaryColor

public static int getComplementaryColor(int color)
Retrieves the complementary color to the specified one.

Parameters:
color - the original argb color
Returns:
the complementary color with the same alpha value

dropShadow

public static final void dropShadow(int[] argbData,
                                    int width,
                                    int height,
                                    int xOffset,
                                    int yOffset,
                                    int size,
                                    int innerColor,
                                    int outerColor)

Paints a dropshadow behind a given ARGB-Array, whereas you are able to specify the shadows inner and outer color.

Note that the dropshadow just works for fully opaque pixels and that it needs a transparent margin to draw the shadow.

Choosing the same inner and outer color and varying the transparency is recommended. Dropshadow just works for fully opaque pixels.

Parameters:
argbData - the images ARGB-Array
width - the width of the ARGB-Array
height - the width of the ARGB-Array
xOffset - use this for finetuning the shadow's horizontal position. Negative values move the shadow to the left.
yOffset - use this for finetuning the shadow's vertical position. Negative values move the shadow to the top.
size - use this for finetuning the shadows radius.
innerColor - the inner color of the shadow, which should be less opaque than the text.
outerColor - the outer color of the shadow, which should be less than opaque the inner color.

applyFilter

public static final void applyFilter(byte[][] filterMatrix,
                                     int brightness,
                                     int[] argbData,
                                     int width,
                                     int height)
Performs a convolution of an image with a given matrix.

Parameters:
filterMatrix - a matrix, which should have odd rows an colums (not neccessarily a square). The matrix is used for a 2-dimensional convolution. Negative values are possible.
brightness - you can vary the brightness of the image measured in percent. Note that the algorithm tries to keep the original brightness as far as is possible.
argbData - the image (RGB+transparency)
width - of the given Image
height - of the given Image Be aware that the computation time depends on the size of the matrix.

drawLine

public static void drawLine(int color,
                            int x1,
                            int y1,
                            int x2,
                            int y2,
                            javax.microedition.lcdui.Graphics g)
Draws a translucent line on MIDP 2.0+ and Nokia-UI-API devices. Note that on pure MIDP 1.0 devices without support for the Nokia-UI-API the translucency is ignored.

Parameters:
color - the ARGB color
x1 - horizontal start position
y1 - vertical start position
x2 - horizontal end position
y2 - vertical end position
g - the graphics context

drawRgb

public static void drawRgb(int[] rgb,
                           int x,
                           int y,
                           int width,
                           int height,
                           boolean processAlpha,
                           javax.microedition.lcdui.Graphics g)
Draws an (A)RGB array and fits it into the clipping area.

Parameters:
rgb - the (A)RGB array
x - the horizontal start position
y - the vertical start position
width - the width of the RGB array
height - the heigt of the RGB array
processAlpha - true when the alpha values should be used so that pixels are blended with the background
g - the graphics context

drawRgb

public static void drawRgb(int[] rgb,
                           int x,
                           int y,
                           int width,
                           int height,
                           boolean processAlpha,
                           int clipX,
                           int clipY,
                           int clipWidth,
                           int clipHeight,
                           javax.microedition.lcdui.Graphics g)
Draws an (A)RGB array and fits it into the clipping area.

Parameters:
rgb - the (A)RGB array
x - the horizontal start position
y - the vertical start position
width - the width of the RGB array
height - the heigt of the RGB array
processAlpha - true when the alpha values should be used so that pixels are blended with the background
clipX - the horizontal start of the clipping area
clipY - the vertical start of the clipping area
clipWidth - the width of the clipping area
clipHeight - the height of the clipping area
g - the graphics context