Aamna2492
Lurker
I have an arraylist of points which are drawn onto a canvas. I have made a method (drawLine) which draws a path/line from one point to another according to the points the user clicks on.
The order in which the points are clicked are put into an arraylist called userPath.
The drawLine method then captures the last two values of userPath and stores them in another arraylist called realPath, and draws a line between these two points and can be seen below. When I invoke the drawLine method, although I am not getting any errors and it compiles fine, the functionality doesn't happen on my emulator and the app crashes. Does anyone know where I'm going wrong???
this class draws a line public void drawLine(double x,double y){// mPath.reset(); if(isPathStarted){if(userPath.size()>=2);{
{
realPath.add(userPath.get(userPath.size() - 1));
realPath.add(userPath.get(userPath.size() - 2));
// start point
Point p = realPath.get(mLastPointIndex);
mPath.moveTo(p.x, p.y);
// end point
p = realPath.get(mLastPointIndex + 1);
//this goes through every point in realPath array list
mPath.lineTo(p.x, p.y);
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
++mLastPointIndex;
isPathStarted = false;
}
}
}
I invoke the method as the following: drawLine(NearestPoint.x, NearestPoint.y); This can be seen below:
@override public boolean onTouchEvent(MotionEventevent){
//motionevent detects motion from the user //float x; // x = event.getX(); //float y; //y = event.getY();
switch(event.getAction()){caseMotionEvent.ACTION_UP:
// this will draw the path //mContext = this.mContext; floatCox=event.getX();floatCoy=event.getY();
doubleCoX=(double)Cox;doubleCoY=(double)Coy;//the euclid method only accepts double numbers therefore the coordinates of the //points the user is clicking on need to be converted from float to double
DoubleNearestDistance=1000.12;//this is hardcoded for the sake of declaring the variable.PointNearestPoint=null;
for(inti=0;i< mPoints.size();i++){Pointcurrent= mPoints.get(i);doublexi=current.x;doubleyi=current.y;
doubledis=Euclid(CoX,CoY,xi,yi);
if(dis<NearestDistance){NearestPoint=current;NearestDistance=dis;}
}
Stringtext="the closest point to where you clicked is: "+NearestPoint+" and the coordinates are: "+NearestPoint.x+", "+NearestPoint.y;Toast.makeText(mContext,text, LENGTH_SHORT).show();
if(userPath.contains(NearestPoint)){StringpickPoint="Pick another point";Toast.makeText(mContext,pickPoint, LENGTH_SHORT).show();}else{
userPath.add(NearestPoint);Toast.makeText(mContext, userPath +"", LENGTH_SHORT).show();
//isPathStarted = true;
drawLine(NearestPoint.x,NearestPoint.y);//need to call the drawLine method here to draw the line between the last 2 elements in the arraylist.
}
invalidate();break;}returntrue;
}
The order in which the points are clicked are put into an arraylist called userPath.
The drawLine method then captures the last two values of userPath and stores them in another arraylist called realPath, and draws a line between these two points and can be seen below. When I invoke the drawLine method, although I am not getting any errors and it compiles fine, the functionality doesn't happen on my emulator and the app crashes. Does anyone know where I'm going wrong???
this class draws a line public void drawLine(double x,double y){// mPath.reset(); if(isPathStarted){if(userPath.size()>=2);{
{
realPath.add(userPath.get(userPath.size() - 1));
realPath.add(userPath.get(userPath.size() - 2));
// start point
Point p = realPath.get(mLastPointIndex);
mPath.moveTo(p.x, p.y);
// end point
p = realPath.get(mLastPointIndex + 1);
//this goes through every point in realPath array list
mPath.lineTo(p.x, p.y);
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
++mLastPointIndex;
isPathStarted = false;
}
}
}
I invoke the method as the following: drawLine(NearestPoint.x, NearestPoint.y); This can be seen below:
@override public boolean onTouchEvent(MotionEventevent){
//motionevent detects motion from the user //float x; // x = event.getX(); //float y; //y = event.getY();
switch(event.getAction()){caseMotionEvent.ACTION_UP:
// this will draw the path //mContext = this.mContext; floatCox=event.getX();floatCoy=event.getY();
doubleCoX=(double)Cox;doubleCoY=(double)Coy;//the euclid method only accepts double numbers therefore the coordinates of the //points the user is clicking on need to be converted from float to double
DoubleNearestDistance=1000.12;//this is hardcoded for the sake of declaring the variable.PointNearestPoint=null;
for(inti=0;i< mPoints.size();i++){Pointcurrent= mPoints.get(i);doublexi=current.x;doubleyi=current.y;
doubledis=Euclid(CoX,CoY,xi,yi);
if(dis<NearestDistance){NearestPoint=current;NearestDistance=dis;}
}
Stringtext="the closest point to where you clicked is: "+NearestPoint+" and the coordinates are: "+NearestPoint.x+", "+NearestPoint.y;Toast.makeText(mContext,text, LENGTH_SHORT).show();
if(userPath.contains(NearestPoint)){StringpickPoint="Pick another point";Toast.makeText(mContext,pickPoint, LENGTH_SHORT).show();}else{
userPath.add(NearestPoint);Toast.makeText(mContext, userPath +"", LENGTH_SHORT).show();
//isPathStarted = true;
drawLine(NearestPoint.x,NearestPoint.y);//need to call the drawLine method here to draw the line between the last 2 elements in the arraylist.
}
invalidate();break;}returntrue;
}