> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.PKnDDqu1vYo/rev.12
 * 
 * authors: 
 *   GoToLoop

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/**
 * Clickable Balls (v1.0.2)
 * Jcov (2017-May-20)
 * Mod GoToLoop
 *
 * Forum.Processing.org/two/discussion/22678/
 * problem-regarding-arraylist#Item_9
 *
 * Studio.ProcessingTogether.com/sp/pad/export/ro.9NGjx94YMTtlu
 */

import java.util.List;
static final int BALLS = 10;
final List<Ball> balls = new ArrayList<Ball>();

void setup() {
  size(500, 500);
  smooth(3);
  noLoop();

  colorMode(RGB);
  ellipseMode(CENTER);
  strokeWeight(1.5);
  stroke(#00FFFF);
  fill(#0000FF);
}

void draw() {
  background(#FF00A0);
  if (balls.isEmpty())  makeBalls();
  for (final Ball b : balls)  b.display();
}

void mousePressed() {
  for (int len = balls.size(), i = len; i-- != 0; )
    if (balls.get(i).isMouseWithinCircle()) {
      balls.set(i, balls.get(--len));
      balls.remove(len);
      redraw();
      return;
    }
}

void makeBalls() {
  balls.clear();
  for (int i = 0; i++ < BALLS; balls.add(new Ball()));
}

static final int sq(final int n) {
  return n*n;
}

class Ball {
  static final int DIAM = 20;
  static final int RAD = DIAM >> 1;
  static final int RAD_SQ = RAD * RAD;

  final short x = (short) round(random(RAD, width  - RAD));
  final short y = (short) round(random(RAD, height - RAD));

  void display() {
    ellipse(x, y, DIAM, DIAM);
  }

  boolean isMouseWithinCircle() {
    return sq(mouseX - x) + sq(mouseY - y) < RAD_SQ;
  }
}