Drawing Rectangles

We can also draw rectangles with the Canvas:

Canvas.drawRect(float topleftX, float topleftY, float bottomRightX, float bottomRightY, Paint paint);

The first two arguments specify the coordinates of the top-left corner of the rectangle, the next two arguments specify the coordinates of the bottom-left corner of the rectangle, and the Paint specifies the color and style of the rectangle. So what can the style be and how do we set it?

To set the style of a Paint instance we call the following method: Paint.setStyle(Style style);

Style is an enumeration that has the values Style.FILL, Style.STROKE, and Style.FILL_AND_STROKE. If we specify Style.FILL, the rectangle will be filled with the color of the Paint. If we specify Style.STROKE, only the outline of the rectangle will be drawn, again with the color and stroke width of the Paint. If Style.FILL_AND_STROKE is set, the rectangle will be filled, and the outline will be drawn with the given color and stroke width.

0 0

Post a comment