`

System.outadb logcat
Log.e(getClass().toString(), message);
Log.d for debug, Log.w for warnings etc.View
protected void onDraw(Canvas canvas)
Canvas is like Graphics2DShape classes
canvas.drawCircle(50, 250, 20, paint)
Paint object has color, stroke width, and so on.onTouchEvent:
public boolean onTouchEvent(MotionEvent event) {
int x = Math.round(event.getX()); // It's a float
int y = Math.round(event.getY());
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) { . . . }
return true;
}
postInvalidate();

androidLab/screen3.png via GitAdapted from https://github.com/codepath/android_guides/wiki/Basic-Painting-with-Views
public class SimpleDrawingView extends View {
public SimpleDrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
<edu.sjsu.cs151.graphicsdemo.SimpleDrawingView
android:id="@+id/simpleDrawingView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
@Override protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(new Rect(10, 20, 30, 40), paint);
}Rect. Why is it a square?private Rect square. Add this code to the onDraw method:
if (square != null) canvas.drawRect(square, paint);
onTouchEvent method to your view class. Inside, add this code:
square = new Rect(x - 10, y - 10, 20, 20);Run your program. What happens?
onDraw method got called. Add a statement
Log.i(getClass().getName(), "onDraw square=" + square);Run your program. You should see the log message at least once in the Run window.
Rect. It may also be helpful to click all the way in the top left corner.)rect instance variable with an ArrayList<Rect> rects. In the onDraw method, draw them all:
for (Rect r : rects) canvas.drawRect(r, paint);In the
onTouchEvent method, add a new one at the touch location:
Rectangle current = new Rect(x - 10, y - 10, x + 10, y + 10); rects.add(current);Run your program. If all is well, you should see a bunch of rectangles, one for each touch.
current an instance variable since we need to remember it from one touch to the next. Then add this method:
private Rect find(int x, int y)
{
for (Rect r : rects)
if (r.contains(x, y)) return r;
return null;
} In the onTouchEvent method, add a new rectangle only if one doesn't touch an existing one:
int action = event.getActionMasked();
if (action == MotionEvent.ACTION_DOWN) {
current = find(x, y);
if (current == null) { . . . }
} Also add a case
else if (action == MotionEvent.ACTION_MOVE) {
current.set(x - 10, y - 10, x + 10, y + 10);
} What happens when you touch a rectangle and drag it? Why?