[Java for Android App Development] darthvader45:
Android newbie looking for help: Storing a drawn line in an ArrayList<MyDrawable>?

Okay, I'm able to draw lines(I used code for rubber-band drawing), but now I've run into a problem. How can I store the line that was drawn as an object in the ArrayList<MyDrawable> known as shapeArray?:confused:

The code for LineDrawView.java(where I have my ArrayList):

// 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
private Paint paintFill;
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)
{
// Draw a Red Diagonal line from upper left corner to bottom right corner

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

break;
case CIRCLE:
// distance =
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);
}
}

[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
MyLine line1 = new MyLine(startX, startY, currentX, currentY, color);
shapeArray.add(line1);
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;
}
}