Rectangle

Rectangles can be drawn in a few different ways, the easiest being to specify the left y coordinate, the top x coordinate, the right y coordinate, and the bottom x coordinate along with a Paint object.

paint.setColor(Color.GREEN);

paint.setStyle(Paint.Style.FILL_AND_STROKE);

paint.setStrokeWidth(10);

float leftx = 20;

float rightx = 50;

float bottomy = 100;

canvas.drawRect(leftx, topy, rightx, bottomy, paint);

Another means to draw a rectangle is to pass in a RectF object. RectF is a class that defines a rectangle using float values representing the left, top, right, and bottom coordinates.

Paint paint = new Paint(); float leftx = 20; float topy = 20; float rightx = 50; float bottomy = 100;

RectF rectangle = new RectF(leftx,topy,rightx,bottomy);

canvas.drawRect(rectangle, paint);

Was this article helpful?

0 0

Post a comment