[Java for Android App Development] darthvader45:
Is there anything else I should remove? Circles are still drawing twice.

// 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 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
public static Paint paintFill;
public int color;
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)
{

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);
}
else if(l instanceof MyRectangle) {
MyRectangle rectangle = (MyRectangle)l;
canvas.drawRect(rectangle.startX, rectangle.startY, rectangle.endX, rectangle.endY, 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;

case RECTANGLE:
canvas.drawRect(startX, startY, currentX, currentY, 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:
switch(drawType)
{
case LINE:
MyLine line1 = new MyLine(startX, startY, currentX, currentY, color);
shapeArray.add(line1);
break;
case CIRCLE:
MyCircle circle1 = new MyCircle(startX, startY,radius, color);
shapeArray.add(circle1);
break;
case RECTANGLE:
MyRectangle rect1 = new MyRectangle(startX, startY, currentX, currentY, color);
shapeArray.add(rect1);
//Do the same for the rectangle
break;
}
invalidate();
return true;

case MotionEvent.ACTION_MOVE:
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;
}
}