英文:
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<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.
答案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<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.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论