Why can't i color this bubble?

Also, look at your variables at the very beginning:

Why is your temporary variable c1 here? But your variables above are not temporary variables…

ok i give up, i try what i can do but still doesn’t work, give me corect answer

This is essentially what your code is doing:

You are mixing variable types and assignment statements are incorrect.

Take a good look at this and try to understand where you are making the errors.

Um @Rai
This isn’t how it works, demanding the correct answer.
Several people have given you very constructive advice.
As I have stated, you are very close. And because you have set up your x, y, and diameter variables correctly, you seem to understand the purpose of the temporary variables in the constructor. This is where the problem is. Carefully proofreading all of your color variables in your class code will unlock the answer. Is your temporary color variable in its correct place? How many times have you indicated use of the temporary variable? Is that correct?

class Bubble {
  float x;
  float y;
  float diameter;

  Bubble(float x1, float y1, float tempD, color c) {
    x = x1;
    y = y1;
    diameter = tempD;
  }
  void ascend() {
    y--;
    x = x + random (-2, 2);
  }
  void display() {
    noStroke();
    fill(c);
    ellipse(x, y, diameter, diameter);
    println (c);
  }

do you mean like this ?

Basically:

  • Have you read the tutorial on objects? https://www.processing.org/tutorials/objects/
  • The class is the cookie maker, the many objects are the cookies
  • The constructor is the part where the cookies are made
  • to do this, we pass parameters to the constructor: `
  • Bubble(float x1, float y1, float tempD1, color c1) {`. These are temporary since when the constructor is done, the object forgets them
  • To make the parameters permanent, we copy them into the classes /Objects own variables which are above the constructor.
  float x;
  float y;
  float diameter;
  color c; 
  • The process of copying is done in the constructor and looks like this:
    x = x1;
    y = y1;
    diameter = tempD1;
  • Now when we would forget to assign a variable in the class a value it stays 0. Or when we assign a value to a parameter, it gets lost after we leave the constructor. Bad.

But in your case it’s just a matter of proofreading.

  • And be consistent in your naming please: When all parameters have …1 at the end, then all variables in the class should not.

Chrisir

2 Likes

Now go back up to the top:

1 Like

Ok thank you…

So you figured it out and the bubbles now appear in color?

class Bubble {
  float x;
  float y;
  float diameter;
  int c1; /// use color/int

  Bubble(float x1, float y1, float tempD, color c) { /// use color/int
    x = x1;
    y = y1;
    c1 = c ;///reverse
    diameter = tempD;
  }
  void ascend() {
    y--;
    x = x + random (-2, 2);
  }
  void display() {
    noStroke();
    fill(c1);
    ellipse(x, y, diameter, diameter);
    println (c1);
  }

thank you…
i thougt i’m never get answer

1 Like

See comments below next to problem areas:

1 Like

That looks nice.

Congratulation!

You solved it!

Just a remark:

Your sketch is not wrong.

Please note though that the naming of the parameters is not consistent:

Bubble(float x1, float y1, float tempD, color c) { ///

could be

Bubble(float x1, float y1, float d1, color c1) { /// all parameters end with 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Also, your variables are

float x;
float y;
float diameter;
int c1; /// use color/int   ---- here naming with a 1 !!! 

could be

  // class variables (or fields) - none of them ends with a 1 !!!!!!!!!!!!!!!!!!!
  float x;
  float y;
  float diameter;
  color c;  

and in the constructor accordingly

    x = x1;  // parameters with 1 are on the right, variables on the left 
    y = y1;
    c = c1; ///  nice 
    diameter = d1;

That looks like a small thing, but being consistent let’s you spot errors faster that are otherwise hard to find.

Chrisir

My full sketch




Bubble b1;
Bubble b2;

void setup() {
  size(640, 360);
  b1 = new Bubble(200, 360, 60, color(random(255), 100, 0));
  b2 = new Bubble(400, 360, 60, color(0, 100, 200));
}//func 

void draw() {
  background(255);

  b1.ascend();
  b1.display();
  b1.top();

  b2.ascend();
  b2.display();
  b2.top();
}//func 

// ===================================================================================

class Bubble {
  // one Bubble

  // class variables (or fields) - none of them ends with 1 !!!!!!!!!!!!!!!!!!!
  float x;
  float y;
  float diameter;
  color c; /// use color/int


  // constructor 
  Bubble(float x1, float y1, /// all parameters end with 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!
    float d1, /// all parameters end with 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!
    color c1) {                 /// all parameters end with 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!
    x = x1;
    y = y1;
    diameter = d1;
    c = c1 ;///
  }// constructor 

  void ascend() {
    y--;
    x = x + random (-2, 2);
  }//method

  void display() {
    noStroke();
    fill(c);
    ellipse(x, y, 
      diameter, diameter);
    // println (c);
  }//method

  void top() {
    if (y < - diameter/2) {
      y = height + diameter/2;
    }
  }//method
  //
}//class
//
1 Like

when i try int c1; or color c1; it’s look’s same output what i realize is c1 = c ; reverse
i thougt ;c1 = c ; same with c = c1;

I think most important at this point is the code runs and you are seeing the result you want.
Maybe look at this project again in a couple weeks after you’ve been coding some more and refer to the thread and you will see a slightly different way to write this that is more consistent with the other variables in your program.
But, Congratulations!!! You worked it out!!!

1 Like

thank you…

1 Like

thank you glv…

thank you debxyz…

1 Like