Loading...
Logo
Processing Forum

Procedural to OOP

in Programming Questions  •  1 year ago  
My first attempt at converting a procedural program to OOP is not behaving as it should. I can't pinpoint what's wrong with the code, but I suspect the class I wrote is at fault somewhere. Is it the way I called/setup the mouseClicked function?


        Procedural version. 

Copy code
  1. float x = 40;
  2. float y = 40;

  3. int bxW = 60;
  4. int bxH = 60;

  5. boolean rectClicked = false;
  6. float moveX = 2;

  7. int countClick = 0;


  8. ///////////////////////////////

  9. void setup() {

  10.   size(700, 600);
  11.   smooth();
  12.   noStroke();

  13. }

  14. ///////////////////////////////////////

  15. void draw() {

  16.   background(255);
  17.   box1();
  18.   emptyRect();
  19. }////////////////////////////// end draw



  20. //----------- functions -----------

  21. /////////////////////////////////////////
  22. void box1 () {

  23.   // draw the square
  24.   fill(#7178E5);
  25.   noStroke();
  26.   rect(x, y, bxW, bxH);


  27.   if (rectClicked) {

  28.     countClick = 1;

  29.     x += moveX;

  30.     if (x>= 400) {

  31.       x = 400;
  32.       moveX = - moveX;
  33.     }

  34.     if (x<=40) {

  35.       x = 40;
  36.       moveX = - moveX;
  37.     }
  38.   }

  39.   else {
  40.     countClick = 0;
  41.     x = 40;
  42.   }
  43. }
  44. ////////////////////////////////
  45. void emptyRect() {
  46.   //empty rect
  47.   stroke(#3C3D40);
  48.   fill(255, 15);
  49.   rect(40, 40, 60, 60);
  50. }
  51. //////////////////////////////////


  52. void mouseClicked() {
  53.   box1MouseClick();
  54. }

  55. ///////- box1 mouse click functions ///////////

  56. void box1MouseClick() {
  57.   if ((mouseX>42) && (mouseX<=99) && (mouseY >44) && (mouseY<99)) {

  58.     rectClicked = true;
  59.   }

  60.   if ((countClick >= 1) && (mouseX>42) && (mouseX<=99) && (mouseY >44) && (mouseY<99)) {
  61.     rectClicked = false;
  62.   }
  63. }




      

         OOP version:
Copy code
  1. /////////globals //////////

  2.   
  3. boolean rectClicked = false;
  4. int countClick = 0;

  5. /////////////////////////



  6. class Boxen {
  7.   


  8.   float x;
  9.   float y;

  10.   int bxW;
  11.   int bxH;
  12.   float speedX;

  13.   int startX;
  14.   int endX;
  15.   int startY;
  16.   int endY;
  17.   ///////////////////////////////////
  18.   Boxen() {

  19.     x = 40;
  20.     y = 40;
  21.     bxW = 60;
  22.     bxH = 60;
  23.     speedX = 2;
  24.   }
  25.   ////////////////////////////////////
  26.   void displayBoxen() {
  27.     fill(#7178E5);
  28.     noStroke();
  29.     rect(x, y, bxW, bxH);
  30.   }

  31.   void moveBoxen() {
  32.     if (rectClicked) {

  33.       countClick = 1;

  34.       x += speedX;

  35.       if (x>= 400) {

  36.         x = 400;
  37.         speedX = - speedX;
  38.       }

  39.       if (x<=40) {

  40.         x = 40;
  41.         speedX = - speedX;
  42.       }
  43.     }
  44.     else {
  45.       countClick = 0;
  46.       x = 40;
  47.     }
  48.   }
  49.   /////////// mouseClick function ///////////

  50.   void mouseClicked(int tempStartX, int tempEndX, int tempStartY, int tempEndY ) {

  51.     startX = tempStartX;
  52.     endX   = tempEndX;
  53.     startY = tempStartY;
  54.     endY   = tempEndY;
  55.     //////////////////
  56.     // First Square X: Parameters: 42, 99
  57.     // 1st Square Y Parameters: 44, 99
  58.     //
  59.     //
  60.     //
  61.     //
  62.     ////////////////////

  63.     if ((mouseX>startX) && (mouseX<=endX) && (mouseY >startY) && (mouseY<endY)) {

  64.       rectClicked = true;
  65.     }

  66.     if ((countClick >= 1) && (mouseX>startX) && (mouseX<=endX) && (mouseY >startY) && (mouseY<endY)) {
  67.       rectClicked = false;
  68.     }
  69.   }
  70.   
  71.   

  72.   
  73.   
  74. }/// end class




  75. Boxen myBoxen;


  76. void setup() {

  77.   size(700,600);
  78.   background(255);
  79.   smooth();
  80.   noStroke();
  81.   myBoxen = new Boxen();
  82.   
  83. }


  84. void draw(){

  85. myBoxen.displayBoxen();
  86. myBoxen.mouseClicked(42,99,44,99);
  87. myBoxen.moveBoxen();

  88. emptyRect();

  89. }


  90. //////////// other function ////////////

  91.   void emptyRect()
  92.  {
  93.   stroke(#3C3D40);
  94.   fill(255, 15);
  95.   rect(40, 40, 60, 60);
  96.  
  97.  }

Replies(9)

Well, it is better to handle the mouseClicked() method in the mouseClicked() function, don't you think?
Copy code
  1. void draw(){
  2.  
  3. myBoxen.displayBoxen();
  4. myBoxen.moveBoxen();
  5.  
  6. emptyRect();
  7.  
  8. }
  9.  
  10. void mouseClicked()
  11. {
  12. myBoxen.mouseClicked(42,99,44,99);
  13. }

I tried your code, but now nothing happens. 

Copy code
  1. /////////globals //////////

  2.   
  3. boolean rectClicked = false;
  4. int countClick = 0;

  5. /////////////////////////



  6. class Boxen {
  7.   


  8.   float x;
  9.   float y;

  10.   int bxW;
  11.   int bxH;
  12.   float speedX;

  13.   int startX;
  14.   int endX;
  15.   int startY;
  16.   int endY;
  17.   ///////////////////////////////////
  18.   Boxen() {

  19.     x = 40;
  20.     y = 40;
  21.     bxW = 60;
  22.     bxH = 60;
  23.     speedX = 2;
  24.   }
  25.   ////////////////////////////////////
  26.   void displayBoxen() {
  27.     fill(#7178E5);
  28.     noStroke();
  29.     rect(x, y, bxW, bxH);
  30.   }

  31.   void moveBoxen() {
  32.     if (rectClicked) {

  33.       countClick = 1;

  34.       x += speedX;

  35.       if (x>= 400) {

  36.         x = 400;
  37.         speedX = - speedX;
  38.       }

  39.       if (x<=40) {

  40.         x = 40;
  41.         speedX = - speedX;
  42.       }
  43.     }
  44.     else {
  45.       countClick = 0;
  46.       x = 40;
  47.     }
  48.   }
  49.   /////////// mouseClick function ///////////

  50.   void mouseClicked(int tempStartX, int tempEndX, int tempStartY, int tempEndY ) {

  51.     
  52.     myBoxen.mouseClicked(42,99,44,99);
  53.     
  54.     startX = tempStartX;
  55.     endX   = tempEndX;
  56.     startY = tempStartY;
  57.     endY   = tempEndY;
  58.     //////////////////
  59.     // First Square X: Parameters: 42, 99
  60.     // 1st Square Y Parameters: 44, 99
  61.     //
  62.     //
  63.     //
  64.     //
  65.     ////////////////////

  66.     if ((mouseX>startX) && (mouseX<=endX) && (mouseY >startY) && (mouseY<endY)) {

  67.       rectClicked = true;
  68.     }

  69.     if ((countClick >= 1) && (mouseX>startX) && (mouseX<=endX) && (mouseY >startY) && (mouseY<endY)) {
  70.       rectClicked = false;
  71.     }
  72.   }
  73.   
  74.   

  75.   
  76.   
  77. }/// end class




  78. Boxen myBoxen;


  79. void setup() {

  80.   size(700,600);
  81.   background(255);
  82.   smooth();
  83.   noStroke();
  84.   myBoxen = new Boxen();
  85.   
  86. }


  87. void draw(){

  88. myBoxen.displayBoxen();
  89. //myBoxen.mouseClicked(42,99,44,99);
  90. myBoxen.moveBoxen();

  91. emptyRect();

  92. }


  93. //////////// other function ////////////

  94.   void emptyRect()
  95.  {
  96.   stroke(#3C3D40);
  97.   fill(255, 15);
  98.   rect(40, 40, 60, 60);
  99.  
  100.  }


I don't see my code in the code you show again, which looks strongly like the previous one.
Ah, I see, you just commented out the myBoxen.mouseClicked(42,99,44,99); which is the first part, but you haven't added the mouseClicked() function calling it.
This function is automatically called by Processing whenever the user clicks on the sketch. You have to transmit the information to the class, via its method.

Note: I wonder why you pass parameters to the mouseClicked() method in Boxen, instead of using the fields of the class.
I did add the mouseClicked function. See line 72 in the code. When I run the program, and click on the blue rect, nothing happens. Could you post how the code should be for this to work? Sometimes if I see the code, I'll understand it better. 
mousePressed() is called by Processing with no parameters. You'll have to remove the parameters from mousePressed() (the global version, not the one in your class) and find another way to get to those values.
Here, an adapted version of your code with some comments. Just to help you understand, the result should not be be what u need, but i think might help you understand better. 

edited: as i forgot to change lines 88 and 90 they still having the hard number (40), though the objects are not behaving as they should. Every one stops at lef,t where the first square is instead of their own position. if you change those hard numbers to startX they should work fine. (untested) 

Copy code

  1. /////////globals //////////



  2. int countClick = 0;

  3. /////////////////////////



  4. class Boxen {



  5.   float x;
  6.   float y;

  7.   int bxW;
  8.   int bxH;
  9.   float speedX;

  10.   float startX; // made float to can interchange with x
  11.                 // i did use your var for another porpouse
  12.                 // store the original position for the frame
  13.   int endX;
  14.   float startY;
  15.   int endY;
  16.   
  17.   boolean rectClicked = false;  // Brought  inside class so each instance has it own
  18.   ///////////////////////////////////
  19.   
  20.   Boxen() { // a constructor with hard number is not much usefull

  21.     x = 40;
  22.     y = 40;
  23.     bxW = 60;
  24.     bxH = 60;
  25.     speedX = 2;
  26.     startX = x;
  27.     startY = y;
  28.   }



  29.   Boxen(float _x, float _y, int _bxW, int _bxH, float _speed) {
  30.             // this accepts inputs for making diferent instances
  31.     x = _x;
  32.     y = _y;
  33.     startX = x;
  34.     startY = y;
  35.     bxW = _bxW;
  36.     bxH = _bxH;
  37.     speedX = _speed;
  38.   }
  39.   ////////////////////////////////////
  40.   void displayBoxen() {
  41.     fill(#7178E5);
  42.     noStroke();
  43.     rect(x, y, bxW, bxH);
  44.     emptyRect();  /// called form inside the clas to share class vars
  45.   }

  46.   void emptyRect()
  47.   {
  48.     stroke(#3C3D40);
  49.     fill(255, 15);
  50.     rect(startX, startY, bxW, bxH); /// startX used here, we ant the frame to stay..
  51.   }




  52.   void moveBoxen() {       // I did not get the porpouse of counClick so ignored it 
  53.     if (rectClicked) {

  54.       countClick = 1;

  55.       x += speedX;

  56.       if (x>= 400) {

  57.         x = 400;
  58.         speedX = - speedX;
  59.       }

  60.       if (x<=40) {

  61.         x = 40;
  62.         speedX = - speedX;
  63.       }
  64.     }
  65.   }
  66.   /////////// mouseClick function ///////////

  67.   void isMouseClicked() {  

  68.     //////////////////
  69.     // First Square X: Parameters: 42, 99
  70.     // 1st Square Y Parameters: 44, 99
  71.     //
  72.     //
  73.     //
  74.     //
  75.     //////////////////// 
  76.     if ((mouseX > startX) && (mouseX< startX + bxW) &&
  77.          (mouseY >startY) && (mouseY < startY  + bxH)) {

  78.       rectClicked = true;
  79.     }

  80.   }
  81. }/// end class




  82. Boxen myBoxen;
  83. Boxen myBoxen2;
  84. Boxen[] several= new Boxen[10]; // illustrate how to handle a lot...


  85. void setup() {

  86.   size(700, 600);
  87.   background(255);
  88.   smooth();
  89.   noStroke();
  90.   myBoxen = new Boxen();
  91.   myBoxen2 = new Boxen(100, 100, 50, 60, 1.89);
  92.   for (int i = 0 ; i < several.length; i++)
  93.   {
  94.     several[i] = new Boxen (100, 200+ i*40 , 40, 40, i+1 );// i helps change parameters..
  95.   }
  96. }


  97. void draw() {
  98.   background(255);// 
  99.   myBoxen.displayBoxen();
  100.   myBoxen.moveBoxen();

  101.   myBoxen2.displayBoxen();
  102.   myBoxen2.moveBoxen();
  103.   
  104.   for (int i = 0 ; i < several.length; i++)
  105.   {
  106.     several[i].displayBoxen();
  107.     several[i].moveBoxen();
  108.   }
  109. }



  110. void mouseClicked()
  111. {
  112.   myBoxen.isMouseClicked();
  113.   myBoxen2.isMouseClicked();
  114.   for (int i = 0 ; i < several.length; i++)
  115.   {
  116.     several[i].isMouseClicked();
  117.   }
  118.   
  119. }

  120. //////////// other function ////////////


" I did add the mouseClicked function. See line 72 in the code."
Ah, I wouldn't have thought you have put it there... With this line, you make a recursive call, ie. an infinite loop...
I have put my mouseClicked() function at the same level than draw(), and without parameters. See the reference about this function.
Yes, this makes sense to me now. Basically had to make my own ismouseClicked function and not call it 'mouseClicked' which is already taken by Processing. Still lots to learn...... 
In Processing / Java, two functions can have the same name if they have different number of parameters, or if they are in different classes (or, in Processing, global names vs. class' method names). So the core of the problem wasn't a name clash, rather how to call your function.