New to processing, I need some help with classes

yesterday I tried to make a program that displays a cool ellipse with ellipses around it and I made it and everything worked fine,

I made a class named DotsArray and made the ellipse array an object, I even added a feature that lets you position the object according to the mouse and everything worked great, and its steal works but then I decided to make every ellipse an object so I can have more flexibility in the colors and alphas.
so I wrote the ellipse object in a separate sketch and it worked fine but then when I tried to add it to the main code and nothing worked.
The code is not broken and will run without the class but if I change one of the ellipses to the ellipse object they just won’t be displayed.

someone can please help me with the code? I’m new to coding (1 week since I started
doing things) and I have beginner knowledge but I’m a fast learner so don’t be afraid to use more advanced level code.
the code -
MAIN FOLDER -
DotsArray ar1 = new DotsArray(50,50,0,0);
void setup()
{
fill(random(0,255),random(0,255),random(0,255));
size(1000,800);
background(0);
strokeWeight(2.5 );
ellipseMode(RADIUS);
}

void draw()
{
ar1.display();
ar1.DAMove();
}
void mousePressed()
{
fill(random(0,255),random(0,255),random(0,255));
}
The Folder With The Object
class DotsArray
{

//The variables
float Xpos; //the Middle X
float Ypos; //the Middle Y
float Xsize;//the ellipse X size
float Ysize;//the ellipse Y size
float XposDelta; //delta from Xmouse position for multi objects array.
float YposDelta; //delta from Xmouse position for multi objects array.
//end of the variables

//start of the elipse construction
Ellipse ellipse0 = new Ellipse(Xpos,Ypos,Xsize,Ysize);
//end of the elipse construction

//start of the Variable setup
DotsArray (float IXsize, float IYsize, float IXposDelta, float IYposDelta)
{
Ysize = IYsize;
Xsize = IXsize;
XposDelta = IXposDelta;
YposDelta = IYposDelta;
//end of the Variable setup
}

//
/
THE ELLIPSE OBJECT /
/
/
class Ellipse
{
float XEpos,YEpos;
float XEsize,YEsize;
Ellipse (float IXEpos, float IYEpos,float IXEsize, float IYEsize)
{
XEpos = IXEpos; //The Chain - Dots Array XPos>ELlipse IXEpos>ellipse XPos>ellipse setup
YEpos = IYEpos; //The Chain - Dots Array YPos>ELlipse IYEpos>ellipse YPos>ellipse setup
XEsize = IXEsize; //The Chain - Dots Array Xsize>ELlipse IXEsize>Ellipse Xsize>ellipse setup
YEsize = IYEsize; //The Chain - Dots Array Ysize>ELlipse IYEsize>Ellipse YEsize>ellipse setup
}

void display() //draws the ellipse
{
  ellipse(XEpos,YEpos,XEsize,YEsize);
}

}
//
/
END OF THE ELLIPSE OBJECT /
/
/

void display() {
ellipse(Xpos,Ypos,Xsize,Ysize); //middle
ellipse(Xpos - Xsize1.7,Ypos,Xsize/1.5,Ysize/1.5); //left
ellipse(Xpos + Xsize
1.7,Ypos,Xsize/1.5,Ysize/1.5); //right
ellipse(Xpos ,Ypos - Ysize2 ,Xsize/1.5 ,Ysize/1.5); // high
ellipse(Xpos ,Ypos + Ysize
2,Xsize/1.5 ,Ysize/1.5); // low
ellipse(Xpos - Xsize1.7,Ypos - Ysize1.7,Xsize/1.5,Ysize/1.5); // top left
ellipse(Xpos + Xsize1.7,Ypos - Ysize1.7 ,Xsize/1.5 ,Ysize/1.5); // top right
ellipse(Xpos + Xsize1.7 ,Ypos + Ysize1.7 ,Xsize/1.5 ,Ysize/1.5); // low right
ellipse(Xpos - Xsize1.7 ,Ypos + Ysize1.7,Xsize/1.5 ,Ysize/1.5); // low left
}
void DAMove()
{
Xpos = mouseX + XposDelta;
Ypos = mouseY + YposDelta;
}
}

Hello,

and welcome to the forum!

Good to have you here!

Hm.

I looked at your code and made a working code.

You had the class Ellipse INSIDE the class DotsArray.

This is possible but unusual.

I put class Ellipse BEHIND the class DotsArray now.

Also, because you posted your code not as code some * got lost. I corrected this.

Anyway, at the moment, DotsArray ar1 is not an array and class DotsArray doesn’t use the class Ellipse really (it’s not used).

Depending from what you want to achieve you want to make an array of ellipses (objects of the class Ellipse) inside class DotsArray. Then you would store the ellipses data and draw from the array; then you would also use background() at the start of draw() to have a fresh canvas every frame and just draw what is stored. Without background you see on the screen everything that has been drawn accumulated (and you don’t see the data you stored).
I haven’t started this yet.

I welcome your feedback.

Regards, Chrisir



//  MAIN TAB 
DotsArray ar1 = new DotsArray(50, 50, 0, 0);


void setup() {
  size(1000, 800);
  background(0);
  strokeWeight(2.5 );
  ellipseMode(RADIUS);
  fill(random(0, 255), random(0, 255), random(0, 255));
}

void draw() {
  ar1.DADisplay();
  ar1.DAMove();
}

void mousePressed() {
  fill(random(0, 255), random(0, 255), random(0, 255));
}

// ================================================================================
// The TAB  With The CLASS DotsArray

class DotsArray {

  //The variables
  float Xpos; //the Middle X
  float Ypos; //the Middle Y
  float Xsize;//the ellipse X size
  float Ysize;//the ellipse Y size
  float XposDelta; //delta from Xmouse position for multi objects array.
  float YposDelta; //delta from Xmouse position for multi objects array.
  //end of the variables

  //start of the elipse construction
  //  Ellipse ellipse0 = new Ellipse(Xpos, Ypos, Xsize, Ysize);
  //end of the elipse construction

  //start of the Variable setup
  DotsArray (float IXsize, float IYsize, 
    float IXposDelta, float IYposDelta) {

    Ysize = IYsize;
    Xsize = IXsize;
    XposDelta = IXposDelta;
    YposDelta = IYposDelta;
    //end of the Variable setup
  }

  void DADisplay() {
    ellipse(Xpos, Ypos, Xsize, Ysize); //middle
    ellipse(Xpos - Xsize*1.7, Ypos, Xsize/1.5, Ysize/1.5); //left
    ellipse(Xpos + Xsize*1.7, Ypos, Xsize/1.5, Ysize/1.5); //right
    ellipse(Xpos, Ypos - Ysize*2, Xsize/1.5, Ysize/1.5); // high
    ellipse(Xpos, Ypos + Ysize*2, Xsize/1.5, Ysize/1.5); // low
    ellipse(Xpos - Xsize*1.7, Ypos - Ysize*1.7, Xsize/1.5, Ysize/1.5); // top left
    ellipse(Xpos + Xsize*1.7, Ypos - Ysize*1.7, Xsize/1.5, Ysize/1.5); // top right
    ellipse(Xpos + Xsize*1.7, Ypos + Ysize*1.7, Xsize/1.5, Ysize/1.5); // low right
    ellipse(Xpos - Xsize*1.7, Ypos + Ysize*1.7, Xsize/1.5, Ysize/1.5); // low left
  }

  void DAMove() {
    Xpos = mouseX + XposDelta;
    Ypos = mouseY + YposDelta;
  }
}// class 

// ========================================================================================================================
// THE ELLIPSE CLASS /
//
class Ellipse22222222222222222222222222 {  // ???????????????????????????????????????

  float XEpos, YEpos;
  float XEsize, YEsize;

  Ellipse22222222222222222222222222 (float IXEpos, float IYEpos, float IXEsize, float IYEsize) {
    XEpos = IXEpos; //The Chain - Dots Array XPos>ELlipse IXEpos>ellipse XPos>ellipse setup
    YEpos = IYEpos; //The Chain - Dots Array YPos>ELlipse IYEpos>ellipse YPos>ellipse setup
    XEsize = IXEsize; //The Chain - Dots Array Xsize>ELlipse IXEsize>Ellipse Xsize>ellipse setup
    YEsize = IYEsize; //The Chain - Dots Array Ysize>ELlipse IYEsize>Ellipse YEsize>ellipse setup
  }

  void display() //draws the ellipse
  {
    //ellipse(XEpos, YEpos, XEsize, YEsize);
  }
  //
} // END OF THE ELLIPSE CLASS /
//
2 Likes

thanks a lot for the attention and the help, but it hasn’t really what I meant, my goal is to be able to change

void DADisplay(){
ellipse(Xpos, Ypos, Xsize, Ysize); //middle
ellipse(Xpos - Xsize1.7, Ypos, Xsize/1.5, Ysize/1.5); //left
ellipse(Xpos + Xsize
1.7, Ypos, Xsize/1.5, Ysize/1.5); //right
ellipse(Xpos, Ypos - Ysize2, Xsize/1.5, Ysize/1.5); // high
ellipse(Xpos, Ypos + Ysize
2, Xsize/1.5, Ysize/1.5); // low
ellipse(Xpos - Xsize1.7, Ypos - Ysize1.7, Xsize/1.5, Ysize/1.5); // top left
ellipse(Xpos + Xsize1.7, Ypos - Ysize1.7, Xsize/1.5, Ysize/1.5); // top right
ellipse(Xpos + Xsize1.7, Ypos + Ysize1.7, Xsize/1.5, Ysize/1.5); // low right
ellipse(Xpos - Xsize1.7, Ypos + Ysize1.7, Xsize/1.5, Ysize/1.5); // low left
}

to

Ellipse ellipse0 = new Ellipse(Xpos, Ypos, Xsize, Ysize); //middle
Ellipse ellipse1 = new Ellipse(Xpos - Xsize1.7, Ypos, Xsize/1.5, Ysize/1.5); //left
Ellipse ellipse2 = new Ellipse(Xpos + Xsize
1.7, Ypos, Xsize/1.5, Ysize/1.5); //right
Ellipse ellipse3 = new Ellipse(Xpos, Ypos - Ysize2, Xsize/1.5, Ysize/1.5); // high
Ellipse ellipse4 = new Ellipse(Xpos, Ypos + Ysize
2, Xsize/1.5, Ysize/1.5); // low
Ellipse ellipse5 = new Ellipse(Xpos - Xsize1.7, Ypos - Ysize1.7, Xsize/1.5, Ysize/1.5); // top left
Ellipse ellipse6 = new Ellipse(Xpos + Xsize1.7, Ypos - Ysize1.7, Xsize/1.5, Ysize/1.5); // top right
Ellipse ellipse7 = new EllipseXpos + Xsize1.7, Ypos + Ysize1.7, Xsize/1.5, Ysize/1.5); // low right
Ellipse ellipse8 = new Ellipse(Xpos - Xsize1.7, Ypos + Ysize1.7, Xsize/1.5, Ysize/1.5); // low left

void DADisplay(){
ellipse0.display;
ellipse1.display;
ellipse2.display;
ellipse3.display;
ellipse4.display;
ellipse5.display;
ellipse6.display;
ellipse7.display;
ellipse8.display;
}

before you end up with variables like


ellipse0 
ellipse1 
ellipse2

you really should look into arrays (list, like a shopping list).

It would look like ellipse[0], ellipse[1], ellipse[2] ....

Further Readings

See https://github.com/Kango/Processing-snippets/wiki/Variables,-Arrays-and-object-oriented-programming

Also, there is a tutorial about arrays: https://www.processing.org/tutorials/arrays/.

You can have arrays which elements are int, but the elements can also be objects, instantiated from a class

1 Like

P.S.

In your last posts, your * vanished again.

When you post code, please select the entire code section and click on </> in the small command bar.

Thank you!

Regards,

Chrisir

1 Like

I made a new version and I can lead you through

1 Like

you made a new version of the code?

Yes, just for my own entertainment.

But I don’t want to spoil the fun for you so soon

1 Like

I suggested to go for arrays. Let me correct this.

Inside the class you might as well use what you‘ve said:

Ellipse ellipse0 = new Ellipse(Xpos, Ypos, Xsize, Ysize); //middle
Ellipse ellipse1 = new Ellipse(Xpos - Xsize 1.7, Ypos, Xsize/1.5, Ysize/1.5); //left
Ellipse ellipse2 = new E


but note that you can pass the mouseX, mouseY from the function mousePressed to the class. Since the figure doesn’t move you can use the initial position to make the ellipses in the constructor of the class DAarray

And…

DADisplay(){
ellipse0.display;
ellipse1.display;
ellipse2.display;
ellipse3.display;
ellipse4.display;

But on sketch level / before setup() I‘d suggest you use an array.

Then background (0); at the start of draw() and a for loop over the array, displaying its content (the stamps with 9 ellipses each)

2 Likes

progress here?

Wanna see my version?

1 Like

I decided to left it untill I have enough knowladge to do it, but I would love to see your code, maybe I can learn from it

1 Like

I hope so!

//  MAIN TAB 

// array / list of stamps
OneStamp[] ar1  = new OneStamp[0];

void setup() {
  size(1000, 800);

  background(0);
  ellipseMode(RADIUS);
}

void draw() {
  background(0);

  fill(255); // white 
  text ("Click mouse to place a stamp", 18, 18);

  for (OneStamp currentStamp : ar1) {
    currentStamp.DADisplay();
  }//for
}

//-------------------------------------------------------------------------------

void mousePressed() {

  // make stamp at mouse position 
  OneStamp newDA = new OneStamp(mouseX, mouseY, 
    50, 50, 
    color (random(0, 255), random(0, 255), random(0, 255)) );

  // add to array 
  ar1 = (OneStamp[]) append (ar1, newDA);
}

// ================================================================================
// The TAB  With The CLASS OneStamp

class OneStamp {

  // ONE stamp 

  // VARS
  color col; 

  // make this another array !!! 
  Ellipse ellipse0 ;
  Ellipse ellipse1 ;
  Ellipse ellipse2 ;
  Ellipse ellipse3 ;
  Ellipse ellipse4 ;
  Ellipse ellipse5 ;
  Ellipse ellipse6 ;
  Ellipse ellipse7 ;
  Ellipse ellipse8 ;
  //end of the variables

  //start of the constructor
  OneStamp (float IXpos, float IYpos, 
    float IXsize, float IYsize, 
    color IColor) {

    //The variables
    float Xpos; //the Middle X
    float Ypos; //the Middle Y
    float Xsize;//the ellipse X size
    float Ysize;//the ellipse Y size

    Xpos=IXpos; //the Middle X
    Ypos=IYpos;

    Ysize = IYsize;
    Xsize = IXsize;

    col=IColor;

    ellipse0 = new Ellipse(Xpos, Ypos, Xsize, Ysize); //middle
    ellipse1 = new Ellipse(Xpos - Xsize*1.7, Ypos, Xsize/1.5, Ysize/1.5); // left
    ellipse2 = new Ellipse(Xpos + Xsize*1.7, Ypos, Xsize/1.5, Ysize/1.5); // right
    ellipse3 = new Ellipse(Xpos, Ypos - Ysize*2, Xsize/1.5, Ysize/1.5);   // high
    ellipse4 = new Ellipse(Xpos, Ypos + Ysize*2, Xsize/1.5, Ysize/1.5);   // low
    ellipse5 = new Ellipse(Xpos - Xsize*1.7, Ypos - Ysize*1.7, Xsize/1.5, Ysize/1.5); // top left
    ellipse6 = new Ellipse(Xpos + Xsize*1.7, Ypos - Ysize*1.7, Xsize/1.5, Ysize/1.5); // top right
    ellipse7 = new Ellipse(Xpos + Xsize*1.7, Ypos + Ysize*1.7, Xsize/1.5, Ysize/1.5); // low right
    ellipse8 = new Ellipse(Xpos - Xsize*1.7, Ypos + Ysize*1.7, Xsize/1.5, Ysize/1.5); // low left
  }  //end of the constructor

  void DADisplay() {

    fill(col); 

    ellipse0.display();
    ellipse1.display();
    ellipse2.display();
    ellipse3.display();
    ellipse4.display();
    ellipse5.display();
    ellipse6.display();
    ellipse7.display();
    ellipse8.display();
  }
}// class 

// ========================================================================================================================
// THE ELLIPSE CLASS 
//
class Ellipse {

  // ONE ellipse 

  float XEpos, YEpos;
  float XEsize, YEsize;

  //start of the constructor
  Ellipse (float IXEpos, float IYEpos, 
    float IXEsize, float IYEsize) {

    XEpos = IXEpos; //The Chain - Dots Array XPos>ELlipse IXEpos>ellipse XPos>ellipse setup
    YEpos = IYEpos; //The Chain - Dots Array YPos>ELlipse IYEpos>ellipse YPos>ellipse setup

    XEsize = IXEsize; //The Chain - Dots Array Xsize>ELlipse IXEsize>Ellipse Xsize>ellipse setup
    YEsize = IYEsize; //The Chain - Dots Array Ysize>ELlipse IYEsize>Ellipse YEsize>ellipse setup
  } //end of the constructor

  void display() {
    //draws the ellipse
    ellipse(XEpos, YEpos, 
      XEsize, YEsize);
  }
  //
} // END OF THE ELLIPSE CLASS
//
1 Like