Using the graphics class to draw a thick curve in BlackBerry applications

The Graphics class is the heart of the paint() method for BlackBerry Java applications. However, occasionally a common action such as drawing a thick curved line, is more complicated then it should be. If having a thickness of a single pixel is acceptable, you can use the following code.


byte[] pathPoints ={
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_END_POINT
};
int[] xPts = {17,17,17,27,48};
int[] yPts = {39,24,14,14,14};
g.drawOutlinedPath(xPts,yPts,pathPoints,null,false);

However, trying to give this line any thickness complicates things a lot. Using the normal work around of placing a number of lines next to each other works great for the straight segments, but falls apart on the curve, where the layout is less predictable. As a result, the best way to draw a thick curve is to use the drawFilledPath() method, and essentially draw and fill a shape.

The following code draws the same line/curve/line segment as the code above, but gives it a thickness of five pixels.


byte[] pathPoints =
{
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_END_POINT
};
int[] xPts = {17,17,17,27,48,42,27,22,22,22};
int[] yPts = {39,24,14,14,14,19,19,19,24,39};
g.drawFilledPath(xPts,yPts,pathPoints,null);