'Drawing particle ( Circles ) network on canvas on Android

  1. I draw particles and move it successfully but when i draw a line between particles, it does not work correctly. I set the min distance is 20, so if the distance between 2 particles is smaller than 20 ,then drawing a line. But it still draws a line eventhough the distance is higher than 20. Here is the example problem picture. example problem
  2. I found that the line is redrawed fastly, sometime it causes flashing redrawing line. Anyone have any ideas how to make smoother animation ?
  3. Here is the code: MainActivity.java
public class MainActivity extends AppCompatActivity {
  private canvasview canvasView;
  final private int InitParticle=10;
  final private ArrayList<particle> arrayList=new ArrayList<particle>(InitParticle);

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      getSupportActionBar().hide();
      canvasView=new canvasview(this) ;
      setContentView(canvasView);

      // Create particles object and pass to the arrayList
      InitArray();

      // Passing particle arrayList to canvas
      canvasView.passingArrFromMain(arrayList);


  }


  public void InitArray()
  {
      for(int i=0;i<InitParticle;i++)
      {
          particle Particle=new particle();
          Particle.initialize(canvasView.get_WidthScreen(),canvasView.get_HeightScreen());
          arrayList.add(Particle);
      }
  }

}

particle.java

public class particle {
    private int x,y;
    final private Random random=new Random();

    private int xdirection,ydirection,randomdirection;
    public particle()
    {
        this.x=0;
        this.y=0;
        this.xdirection=3;
        this.ydirection=3;
        this.randomdirection=random.nextInt(99); // random number for chosing direction
    }

    // Get position of particle
    public int getx()
    {
        return x;
    }
    public int gety()
    {
        return y;
    }

    // Random position of particle
    public void initialize(int a,int b)
    {

        this.x= random.nextInt(a);
        this.y= random.nextInt(b);

    }
    public int getXdirection()
    {
        return xdirection;
    }

    public int getYdirection()
    {
        return ydirection;
    }

    public void setXdirection()
    {
        this.xdirection=-this.xdirection;
    }

    public void setYdirection()
    {
        this.ydirection=-this.ydirection;
    }


    public void move(int x,int y) {

        if (this.randomdirection >= 0 && this.randomdirection < 25) {
            this.x = this.x + x;
            this.y = this.y + y;
        } else if (this.randomdirection >= 25 && this.randomdirection < 50) {
            this.x = this.x - x;
            this.y = this.y + y;
        } else if (this.randomdirection >= 50 && this.randomdirection < 75) {
            this.x = this.x - x;
            this.y = this.y - y;
        } else if (this.randomdirection >= 75 && this.randomdirection < 100) {
            this.x = this.x + x;
            this.y = this.y - y;
        }
    }
}

canvasview.java

public class canvasview extends View {

    final private int mindistance=20;
    final private Paint paint;
    final private int H=getContext().getResources().getDisplayMetrics().heightPixels;
    final private int W=getContext().getResources().getDisplayMetrics().widthPixels;
    private ArrayList<particle> arrayParticles;
    final private Paint linepaint=new Paint(Paint.ANTI_ALIAS_FLAG);


    public canvasview(Context context)
    {
        super(context);
        // Set canvas background
        setBackgroundColor(Color.parseColor("#898984"));
        setLayoutParams(new RelativeLayout.LayoutParams(W,H));

        // Set paint for drawing circle
        paint=new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setColor(Color.parseColor("#E7E7E5"));

        // Set paint for drawing line
        linepaint.setStrokeWidth(5);
        linepaint.setColor(Color.parseColor("#6B6C65"));

    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

            // Drawing Particles
            for(int i=0;i<arrayParticles.size();i++) {
                canvas.drawCircle(arrayParticles.get(i).getx(), arrayParticles.get(i).gety(), 20, paint);
            }

            // Drawing line between particles
            for(int i=0;i<arrayParticles.size();i++) {
                for(int j=i+1;j<arrayParticles.size();j++) {

                    int a=arrayParticles.get(i).getx()-arrayParticles.get(j).getx();
                    int b=arrayParticles.get(i).gety()-arrayParticles.get(j).gety();

                    if(Math.sqrt(a^2+b^2)<mindistance) {
                        canvas.drawLine(arrayParticles.get(i).getx(), arrayParticles.get(i).gety(), arrayParticles.get(j).getx(), arrayParticles.get(j).gety(), linepaint);

                    }
                }
            }

            // Moving particles
            update();
    }

    // Get Width and Height of Screen for random position of particle
    public int get_WidthScreen()
    {
        return W;
    }
    public int get_HeightScreen()
    {
        return H;
    }

    // Get a copy of particle arrayList form MainActivity
    public void passingArrFromMain(ArrayList<particle> arrayList)
    {
        this.arrayParticles=arrayList;

    }

    // Updating the new position of particles
    public void update()
    {
        for (int i=0;i<arrayParticles.size();i++)
        {

            // Check edge of the screen
            if(arrayParticles.get(i).getx()>get_WidthScreen())
                arrayParticles.get(i).setXdirection();

            if (arrayParticles.get(i).gety()>get_HeightScreen())
                arrayParticles.get(i).setYdirection();

            if (arrayParticles.get(i).getx()<0)
                arrayParticles.get(i).setXdirection();

            if (arrayParticles.get(i).gety()<0)
                arrayParticles.get(i).setYdirection();


            arrayParticles.get(i).move(arrayParticles.get(i).getXdirection(),arrayParticles.get(i).getYdirection());

            // Redraw the canvas
            invalidate();
        }
    }


}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source