[Java for Android App Development] darthvader45:
Any corrections I could make? It seems to crash.

// Project:	Java2Final
// File:	LineDrawView.java
// Date:	4/16/13
// Description:	Final Exam
//
package com.lcc.java2final_lefelhocz;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class LineDrawView extends View
{
private float previousX;
private float previousY;
private float currentX;
private float currentY;
private float startX;
private float startY;

// add for lab 12
private final int LINE = 1;
private final int CIRCLE = 2;
private final int RECTANGLE = 3;
private int drawType = 3;
private float deltaX;
private float deltaY;
private float radius;
private Paint paintStroke;

ArrayList<MyDrawable> shapeArray;

// Paint objects
private Paint paintFill;
int color;
private Canvas canvas;

public LineDrawView(Context context)
{
// call the super class constructor
super(context);

// The Paint class holds the style and color information about how to draw geometries, text and bitmaps.
// For efficiency create the paint objects in the constructor, not in draw
// paint.setStrokeWidth(10); // works on lines
// You can change the color of Paint without effecting objects already drawn
// You can NOT change the style of Paint without effecting objects already drawn
// The Style, TextSize apply to all objects drawn with the paint.

// Create a default Paint object Style=Fill
paintFill = new Paint();

// added for lab12
// Create a Paint Object with Style=Stroke
paintStroke = new Paint();
paintStroke.setStyle(Paint.Style.STROKE);

// set the background color when the view is created
this.setBackgroundColor(Color.LTGRAY);
shapeArray = new ArrayList<MyDrawable>(10);

}

[MENTION=439709]override[/MENTION]
protected void onDraw(Canvas canvas)
{
// Draw a Red Diagonal line from upper left corner to bottom right corner
paintFill.setColor(Color.BLACK);
for (MyDrawable l: shapeArray) {
if (l instanceof MyLine) {
MyLine line = (MyLine) l;
canvas.drawLine(line.startX, line.startY, line.endX, line.endY, paintFill);
} else if (l instanceof MyCircle) {
MyCircle circle = (MyCircle)l;
canvas.drawCircle(circle.startX, circle.startY, circle.radius, paintStroke);
}
}

switch(drawType)
{
case LINE:
// draw the rubber band line
canvas.drawLine(startX, startY, currentX, currentY, paintFill);

break;

case CIRCLE:
radius = (float)Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));

canvas.drawCircle(startX, startY, radius, paintStroke);
break;
}
}
[MENTION=439709]override[/MENTION]
public boolean onTouchEvent(MotionEvent event)
{
// get the current X and Y position from the event
currentX = event.getX();
currentY = event.getY();

// switch on the touch event action
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:

// start the line
startX = currentX;
startY = currentY;
invalidate();
return true;
case MotionEvent.ACTION_UP:
// finish the line, create an object of it and add it to the ArrayList

switch(drawType)
{
case LINE:
MyLine line1 = new MyLine(startX, startY, currentX, currentY, color);
if (line1 instanceof MyLine){
shapeArray.add(line1);
canvas.drawLine(startX, startY, line1.endX, line1.endY, paintFill);
}
canvas.drawLine(startX, startY, currentX, currentY, paintFill);
break;
case CIRCLE:
MyCircle circle1 = new MyCircle(startX, startY,radius, color);
if (circle1 instanceof MyCircle){
shapeArray.add(circle1);
canvas.drawCircle(startX, startY, circle1.radius, paintFill);
}
canvas.drawCircle(startX, startY, radius, paintStroke);
break;
case RECTANGLE:
MyRectangle rect1 = new MyRectangle(startX, startY, currentX, currentY, color);
if (rect1 instanceof MyRectangle){
shapeArray.add(rect1);
canvas.drawRect(startX, startY, rect1.endX, rect1.endY, paintStroke);
}
canvas.drawRect(startX, startY, currentX, currentY, paintStroke);
//Do the same for the rectangle
break;
}
invalidate();
return true;

case MotionEvent.ACTION_MOVE:
previousX = currentX;
previousY = currentY;

deltaX = currentX - startX;
deltaY = currentY - startY;
invalidate();
return true;
default:
return super.onTouchEvent(event);
}
}

// Called back when the view is first created or its size changes.
[MENTION=439709]override[/MENTION]
public void onSizeChanged(int width, int height, int oldWidth, int oldHeight)
{
}

// added for lab 12
public void setDrawType(int aDrawType)
{
drawType = aDrawType;
}
}