改变Processing中的对象颜色通过鼠标点击。

huangapple go评论103阅读模式
英文:

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.

英文:

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&lt;20; i++){
    barray[i] = new Ball();
  }
  strokeWeight(40);
}
void draw(){
  background(255,255,255);
  for (int i=0; i&lt;barray.length; i++){
    barray[i].paint(); 
  }
  for (int i=0; i&lt;barray.length; i++){
    barray[i].move(); 
  }
  
  if (mousePressed) {
    for (int i = 0; i &lt; 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&lt;0 || x&gt; width){
      diffx *= -1;
    }
    y += diffy;
    if (y&lt;0 || y&gt; height){
      diffy *= -1;
    }
  }
  public void paint(){
    point(x,y);
  }
  
  public void testHint() {
    float d = dist(mouseX,mouseY,this.x,this.y);
    if ( d &lt; 5){
      stroke(255,0,0);
      point(this.x,this.y);
    }
  }
}

Thank you for your help.

答案1

得分: 2

以下是翻译好的内容:

这里有一个可工作的示例(http://hello.processing.org/display/#@-MDZNtlpRsdS_3GSlvvg):

public class Ball {
    int x, diffx;
    int y, diffy;
    boolean 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();
        }
    }
}

你在stroke函数中设置的颜色会在之后的所有绘制中都生效所以在point()调用之后必须将其重置为黑色因此为所有的球都设置了一个active标志在绘制之前改变颜色


<details>
<summary>英文:</summary>

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&lt;0 || x&gt; width){
                 diffx *= -1;
             }
             y += diffy;
             if (y&lt;0 || y&gt; 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 &lt; 5 ) {
                 active = true;
             }
         }
     }

     Ball[] barray= new Ball[20]; 

     void setup(){
         size(400,400);
         for (int i=0; i&lt;20; i++){
             barray[i] = new Ball();
         }
         strokeWeight(40);
     }
     void draw(){
         background(255,255,255);
         for (int i=0; i&lt;barray.length; i++){
             barray[i].paint(); 
         }
         for (int i=0; i&lt;barray.length; i++){
             barray[i].move(); 
         }
  
         if (mousePressed) {
             for (int i = 0; i &lt; 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&#39;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. 

</details>



huangapple
  • 本文由 发表于 2020年7月31日 18:07:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63189794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定