Unwanted Color Changes

Hi All. I wrote the following codes to follow my mouse when I pressed the left button with red and when I pressed the right button with green colors but color of all the shapes changes when I changed the mouse buttons.

It must start new shapes with new color and must not change the rest/left ones when I change the mouse buttons but it changes the color of all.

I need your guide. Thanks.

Here is the code

ArrayList <PVector> points= new ArrayList <PVector>();
float x, y;
float easing = 0.2;
float diameter = 12;
int distance=100;
int a;
void setup() {
  size(1200, 1000);
  background(0);
  smooth();
  surface.setResizable(true);
  colorMode(RGB);
}
void draw() {
  float targetX = mouseX;
  float targetY=mouseY;
  y += (targetY - y) * easing;
  x += (targetX - x) * easing;
  if (mousePressed&&mouseButton==LEFT) {
    points.add(new PVector(x, y));

    for (int i=0; i<points.size(); i++) {
      float k=  points.get(i).x;
      float l = points.get(i).y;
      stroke(255, 0, 0);
      fill(255, 0, 0);
      ellipse(k, l, 12, 12);
      println(targetX + " : " + x);
     
      if (points.size()>distance) {
        points.remove(0);
        clear();
      }
    }
  }  
  if (mousePressed&&mouseButton==RIGHT) {
    points.add(new PVector(x, y));

    for (int i=0; i<points.size(); i++) {
      float m=  points.get(i).x;
      float n = points.get(i).y;
      stroke(0, 255, 0);
      fill(0, 255, 0);
      ellipse(m, n, 12, 12);
      println(targetX + " : " + x);
      if (points.size()>distance) {
        points.remove(0);
        clear();
      }
    }
  }
}
1 Like

can you please copy your above code back to a blank
PDE sketch and tell me why it is not running?

code paste please use

</> Preformatted text button from editor menu

looks like
```
type or paste code here
```


anyhow from program flow i understand:
on mouse button press you want add a new vector to a array.

and then draw all from array circles

with the only difference that
on mouse RIGHT GREEN
on mouse LEFT RED

so actually you could take out all that code from the IF ( and put below )
only change the color inside.


but actually you not want that at all,
sorry, if you want each circle to have its own color you need a memory for that.
and a PVector is not the best one ( could use .z )

so need color array ( same long as PVector )

or move position vector and color inside a class.

1 Like