绘制路径的方法在调用时会引发错误

时间:2015-02-19 17:31:54

标签: java android canvas arraylist path

我有一个绘制在画布上的点数的arraylist。我创建了一个方法(drawLine),根据用户点击的点,从一个点到另一个点绘制路径/线。

点击点的顺序将被放入名为userPath的arraylist中。

drawLine方法然后捕获userPath的最后两个值并将它们存储在另一个名为realPath的arraylist中,并在这两个点之间绘制一条线,如下所示。

//this class draws a line
public void drawLine(float x, float y)
{
    mPath.reset();
    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;
}

但是,当我调用方法(drawLine(x,y))时,它会抛出一个错误。

 switch (event.getAction()) {
          case MotionEvent.ACTION_UP:

              // this will draw the path
              //mContext = this.mContext;
              float Cox = event.getX();
              float Coy = event.getY();

              double CoX = (double) Cox;
              double CoY = (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

              Double NearestDistance = 1000.12; //this is hardcoded for the sake of declaring the variable.
              Point NearestPoint = null;

              for (int i = 0; i < mPoints.size(); i++)
              {
                  Point current = mPoints.get(i);
                  double xi = current.x;
                  double yi = current.y;

                  double dis = Euclid(CoX, CoY, xi, yi);

                  if (dis < NearestDistance)
                  {
                      NearestPoint = current;
                      NearestDistance = dis;
                  }
              }

              String text = "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))
              {
                  String pickPoint = "Pick another point";
                  Toast.makeText(mContext, pickPoint, LENGTH_SHORT).show();
              }
              else
              {
                  userPath.add(NearestPoint);
                  Toast.makeText(mContext, userPath + "", LENGTH_SHORT).show();

                  drawLine(x,y);
                  //need to call the drawLine method here to draw the line between the last 2 elements in the arraylist. this throws an error!!!
              }

              invalidate();
              break;
      }
      return true;
    }

0 个答案:

没有答案