This assignment creates a drawing program, where you can draw circles to the screen by touching and dragging, and it will report the Area and Perimeter of the circle to the user.
First, create a new Android Project called DrawTouch. Use the package edu.centenary.USERNAME.drawtouch and select Android SDK version 17.
Copy the following DrawTouchActivity.java file into the package you created under the src directory. This is the controller which runs the application.
Copy the icon ic_launcher.png file into the res/drawable-hdpi directory. This is the icon for your application.
To complete this project, you will need to make a MyCircle
class that has the following structure:
First, import the library for android.graphics.RectF
. This is how Android
represents a rectangle for drawing objects to the screen. It has floats as data members for the
left, top, right, and bottom boundaries.
You will need three data members, one to keep track of the x coordinate of the center of the circle, one to track the y coordinate of the center, and one to track the radius of the circle.
public MyCircle(double x, double y)
The constructor brings in the initial center of the circle. The initial radius should be 0.
public void setRadius(double x, double y)
This method should recalculate the radius, based on the distance between the given x, y point, and the center of the circle.
public double getArea()
Returns the Area of the circle, based on the radius.
public double getPerimeter()
Returns the perimeter of the circle, based on the radius.
public RectF getBoundingBox()
Calculates the Bounding Box for this circle, a rectangle that completely contains this circle as tightly as possible. This should return a RectF using the constructor that takes four floats, denoting the left, top, right and bottom boundaries for this rectangle.