title: Android: drawing concentric circles
body: <p>I am trying to write an app that connects to a remote host and reads lines of x,y tuples and draws them to a scatter plot in real time.  The data is being displayed as expected but when I modified the code to try and display some markings to measure the data it isn't working as expected.  I expected to see a black background with 20 concentric gray circles.  As data rolls in it should be displayed as green dots on the concentric circles.  Instead, what I see when the remote host is down, is that initializeSurface() is called and only the outer gray circle (i=20) is displayed.  And, when the remote host is up initializeSurface() followed quickly by drawPoint() are called and no gray circles but only green dots are displayed.  Code is below, followed by the 2 screen shots described before.</p>
  
  <pre><code>package com.balaguru.ggdiagram;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.pm.ActivityInfo;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.graphics.Rect;
  import android.os.Bundle;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.view.View;
  import android.view.WindowManager;
  import android.widget.Toast;
  
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.io.StringWriter;
  import java.net.Socket;
  
  class ToastThread implements Runnable {
      private String ivMessage;
      private View ivView;
      ToastThread(View pView, String pMessage) { ivView = pView; ivMessage = pMessage; }
      public void run() {
          Toast.makeText(ivView.getContext(), ivMessage, Toast.LENGTH_LONG).show();
      }
  }
  class ConnectionThread extends Thread {
  
      private GGView ivGGView;
  
      ConnectionThread(GGView pGGView) {
          ivGGView = pGGView;
      }
  
      public void run() {
          try {
              ivGGView.initializeSurface();
              //Toast.makeText(MainActivity.context, "office-main", Toast.LENGTH_LONG).show();
              Socket socket = new Socket("192.168.1.197", 10000);
              BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              String line = input.readLine();
              while (line != null) {
                  if (line.equals("not connected")) {
                      ivGGView.drawPoint(0, 0, Color.GREEN);
                  } else {
                      String[] gg = line.split(",");
                      float x = Float.parseFloat(gg[0]);
                      float y = Float.parseFloat(gg[1]);
                      ivGGView.drawPoint(x, y, Color.GREEN);
                  }
                  line = input.readLine();
              }
          } catch (Throwable e) {
              StringWriter error = new StringWriter();
              e.printStackTrace(new PrintWriter(error));
              ToastThread toast = new ToastThread(ivGGView, error.toString());
              Activity activity = (Activity) ivGGView.getContext();
              activity.runOnUiThread(toast);
          }
      }
  }
  
  class GGView extends SurfaceView implements SurfaceHolder.Callback {
      private SurfaceHolder ivHolder;
      public Context ivContext;
      private int ivWidth;
      private int ivHeight;
  
      public GGView(Context context){
          super(context);
          ivContext = context;
          ivHolder = getHolder();
          ivHolder.addCallback(this);
      }
      public void surfaceDestroyed(SurfaceHolder pHolder) {}
      public void surfaceCreated(SurfaceHolder pHolder) {
          ConnectionThread thread = new ConnectionThread(this);
          thread.start();
      }
      public void surfaceChanged(SurfaceHolder pHolder, int pFormat, int pWidth, int pHeight) {}
  
      public void initializeSurface() {
          Canvas canvas = ivHolder.lockCanvas();
          ivWidth = canvas.getWidth();
          ivHeight = canvas.getHeight();
          canvas.drawColor(Color.BLACK);
          Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
          paint.setColor(Color.GRAY);
          paint.setStrokeWidth(4);
          paint.setStyle(Paint.Style.STROKE);
          for (int i = 1; i &lt;= 20; i++) {
              canvas.drawArc(ivWidth/2 - i/20*ivHeight/2,
                  ivHeight/2 - i/20*ivHeight/2,
                  ivWidth/2 + i/20*ivHeight/2,
                  ivHeight/2 + i/20*ivHeight/2,
                  0, 360, false, paint);
          }
          ivHolder.unlockCanvasAndPost(canvas);
      }
  
      public void drawPoint(float pX, float pY, int pColor) {
          float x = pX / 20 * ivHeight/2 + ivWidth/2;
          float y = pY / 20 * ivHeight/2 + ivHeight/2;
          Canvas canvas = ivHolder.lockCanvas(
                  new Rect((int) x-4, (int)y-4, (int) x+4, (int)y+4));
          Paint paint = new Paint();
          paint.setColor(pColor);
          paint.setStrokeWidth(4);
          canvas.drawPoint(x, y, paint);
          ivHolder.unlockCanvasAndPost(canvas);
  
      }
  }
  
  
  public class MainActivity extends Activity {
  
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
          GGView ggView = new GGView(this);
          setContentView(ggView);
      }
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/rvyum.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rvyum.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/6dHC1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6dHC1.png" alt="enter image description here"></a></p>
  
  <p><strong>EDIT</strong>
  If I change the for loop to go from 20 to 40 in initializeSurface() then drawArc appears to draw concentric circles when i=20 and i=40 but none other.  Screen shot below.</p>
  
  <p><a href="https://i.stack.imgur.com/pz2J0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pz2J0.png" alt="enter image description here"></a></p>