title: for loop in run() SurfaceView Runnable Android
body: <p>Hi I am trying to make an application that draws lines based on points a user enters. A user may input as many points possible and the object will draw these lines based on their x and y values. I have looked into many drawing tutorials and found that using a <code>SurfaceView Runnable</code> is the best way to do any kind of drawing or animation in Android. I have run into a problem where the <code>run()</code> function does not draw with for loops. Is there a way to get loops working in <code>run()</code> or somewhere else? My code is below.</p>
  
  <pre><code>public class draw extends SurfaceView implements Runnable {
  
  Thread thread = null;
  updateDraw draw = null;
  boolean canDraw = false;
  Path path;
  
  Bitmap bitmap;
  SurfaceHolder surfaceHolder;
  Context mContext;
  Paint paint;
  
  
  int bitmapX;
  int bitmapY;
  int viewWidth;
  int viewHeight;
  
  ArrayList&lt;ArrayList&lt;Double&gt;&gt; XY;
  
  
  Paint blue_paintbrush_stroke
  
  public draw(Context context, ArrayList&lt;ArrayList&lt;Double&gt;&gt; XY) {
      super(context);
      mContext = context;
      surfaceHolder = getHolder();
      paint = new Paint();
      path = new Path();
  
  }
  
  
  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
      super.onSizeChanged(w, h, oldw, oldh);
  
      viewWidth = w;
      viewHeight = h;
  
      draw = new updateDraw(viewWidth, viewHeight);
  
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inScaled = true;
      options.inMutable = true;
  
      bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bitmap, options);
      setUpBitmap();
  }
  
  
  @Override
  public void run() {
      Canvas canvas;
      prepPaintBrushes();
      while (canDraw) {
          //draw stuff
          if (surfaceHolder.getSurface().isValid()) {
              int x = draw.getX();
              int y = draw.getY();
              canvas = surfaceHolder.lockCanvas();
              canvas.save();
              canvas.drawBitmap(bitmap, bitmapX, bitmapY, paint);
              canvas.drawPath(path, blue_paintbrush_stroke);
  
  
              for(int i = 0; i &lt; XY.size()-1; i++){
                  float aX = (XY.get(i).get(0), XY.get(i).get(1)).get(0) + bitmapX;
                  float aY = (XY.get(i).get(0), XY.get(i).get(1)).get(1) + bitmapY;
  
                  float bX = (XY.get(i+1).get(0), XY.get(i+1).get(0)).get(0) + bitmapX;
                  float bY = (XY.get(i+1).get(0), XY.get(i+1).get(1)).get(1) + bitmapY;
  
                  canvas.drawLine(aX, aY, bX, bY, blue_paintbrush_stroke);
              }
  
              path.rewind();
              canvas.restore();
              surfaceHolder.unlockCanvasAndPost(canvas);
          }
      }
  }
  
  
  private void updateFrame(int newX, int newY) {
      draw.update(newX, newY);
  }
  
  
  private void setUpBitmap() {
      bitmapX = (int) Math.floor(
              Math.random() * (viewWidth - backGround.getWidth()));
      bitmapY = (int) Math.floor(
              Math.random() * (viewHeight - backGround.getHeight()));
  }
  
  
  public void pause() {
      canDraw = false;
      while (true) {
          try {
              thread.join();
              break;
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  }
  
  public void resume() {
      canDraw = true;
      thread = new Thread(this);
      thread.start();
  }
  
  private void prepPaintBrushes() {
      blue_paintbrush_stroke = new Paint();
      blue_paintbrush_stroke.setColor(Color.BLUE);
      blue_paintbrush_stroke.setStyle(Paint.Style.STROKE);
      blue_paintbrush_stroke.setStrokeWidth(10);
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
      float x = event.getX();
      float y = event.getY();
  
      switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
              setUpBitmap();
              // Set coordinates of map.
              updateFrame((int) x, (int) y);
              invalidate();
              break;
          case MotionEvent.ACTION_MOVE:
              // Updated coordinates for map.
              updateFrame((int) x, (int) y);
              invalidate();
              break;
          default:
              // Do nothing.
      }
      return true;
  }
  
  }
  </code></pre>