ana
ana

Reputation: 97

Change object color by mouse click in Processing

I am working on a school project in Processing (Java Mode). The task is to change the color of the Ball object if it is clicked.

Unfortunately I am stuck on changing just one object. If I click on the screen all objects change color.

Here are my classes:

Ball[] barray= new Ball[20]; 

void setup(){
  size(400,400);
  for (int i=0; i<20; i++){
    barray[i] = new Ball();
  }
  strokeWeight(40);
}
void draw(){
  background(255,255,255);
  for (int i=0; i<barray.length; i++){
    barray[i].paint(); 
  }
  for (int i=0; i<barray.length; i++){
    barray[i].move(); 
  }
  
  if (mousePressed) {
    for (int i = 0; i < barray.length; i++) {
      barray[i].testHint();
    }
  }
}

Ball class:

public class Ball {
  int x, diffx;
  int y, diffy;
  public Ball() {
    x= (int) random(1, width);
    diffx= (int) random(1, 5);
    y= (int) random(1, height);
    diffy= (int) random(1, 5);
  }
  public void move(){
    x += diffx;
    if (x<0 || x> width){
      diffx *= -1;
    }
    y += diffy;
    if (y<0 || y> height){
      diffy *= -1;
    }
  }
  public void paint(){
    point(x,y);
  }
  
  public void testHint() {
    float d = dist(mouseX,mouseY,this.x,this.y);
    if ( d < 5){
      stroke(255,0,0);
      point(this.x,this.y);
    }
  }
}

Thank you for your help.

Upvotes: 1

Views: 2017

Answers (1)

drodil
drodil

Reputation: 2326

So here is working example (http://hello.processing.org/display/#@-MDZNtlpRsdS_3GSlvvg):

 public class Ball {
     int x, diffx;
     int y, diffy;
     bool active;
     public Ball() {
         active = false;
         x= (int) random(1, width);
         diffx= (int) random(1, 5);
         y= (int) random(1, height);
         diffy= (int) random(1, 5);
     }

     public void move(){
         x += diffx;
         if (x<0 || x> width){
             diffx *= -1;
         }
         y += diffy;
         if (y<0 || y> height){
             diffy *= -1;
         }
     }

     public void paint(){
         if(active) {
             stroke(255,0,0);
         }
         point(x,y);
         stroke(0,0,0);
     }

     public void testHint() {
         float d = dist(mouseX,mouseY,this.x,this.y);
         if ( d < 5 ) {
             active = true;
         }
     }
 }

 Ball[] barray= new Ball[20]; 

 void setup(){
     size(400,400);
     for (int i=0; i<20; i++){
         barray[i] = new Ball();
     }
     strokeWeight(40);
 }
 void draw(){
     background(255,255,255);
     for (int i=0; i<barray.length; i++){
         barray[i].paint(); 
     }
     for (int i=0; i<barray.length; i++){
         barray[i].move(); 
     }

     if (mousePressed) {
         for (int i = 0; i < barray.length; i++) {
             barray[i].testHint();
         }
     }
 }

The color you are giving in the stroke function will be active for all paints after that so that's why you have to reset it back to black after the point() call. And thus having active flag for all your balls to change the color before drawing it.

Upvotes: 2

Related Questions