# PLEASE DELETE THIS POST: Problems with constructor of Class

**URL:** <https://discourse.processing.org/t/please-delete-this-post-problems-with-constructor-of-class/26796>\
**Category:** Coding Questions\
**Created:** [January 5, 2021, 3:16pm UTC](https://discourse.processing.org/t/please-delete-this-post-problems-with-constructor-of-class/26796 "2021-01-05T15:16:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beto](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/beto/32/11677_2.png) [@Beto](https://discourse.processing.org/u/Beto)\
**Post date:** [January 5, 2021, 3:16pm UTC](https://discourse.processing.org/t/please-delete-this-post-problems-with-constructor-of-class/26796/1 "2021-01-05T15:16:40Z")

</div>

My code will be uploaded later in a new topic. If an admin comes across this topic, please delete.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 5, 2021, 3:47pm UTC](https://discourse.processing.org/t/please-delete-this-post-problems-with-constructor-of-class/26796/2 "2021-01-05T15:47:25Z")

</div>

The number of parameters must match

Compare it where you define the constructor:  
` RED(int x, int y, int w, int h, String t, int g, int b)` // int r is missing (2 params after the String t)  
with where you call it:  
`RED = new RED(100, 100, 100, 50, “RED”, 0, 200, 200); // 3 params after the String "RED"`

as you can see, where you define the constructor there is one parameter less than in the code line where you instantiate the object (besides, instead of r,g, you could just pass one color variable)

**Remark**

also, the object should be called red or redObj

` RED RED;` is not good

by convention:

- class name: BigWithCaptial
- constant names: BIG\_WITH\_UNDERSCORES
- object names: smallWithBigLettersInside

**Hey, and welcome to the forum! Great to have you here!**

Chrisir

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 5, 2021, 3:57pm UTC](https://discourse.processing.org/t/please-delete-this-post-problems-with-constructor-of-class/26796/3 "2021-01-05T15:57:48Z")

</div>

1. 

there was also a misplaced `}` bracket, after the class’ constructor, instead of after the class’ methods.

1. 

Also we have `RED ButtonRed;` now, not `RED RED;` anymore

1. 

Also, I placed this in draw():

```auto
  ButtonRed.update();
  ButtonRed.render();

```

* * *

**Here is the entire Sketch :**

```auto

class RED
{
  PVector Pos = new PVector(0, 0);
  float Width = 0;
  float Height = 0;
  color Color;
  String Text;
  Boolean Pressed = false;
  Boolean Clicked = false;

  // constructor to create Button
  RED(int x, int y, 
    int w, int h, 
    String t, 
    int r, int g, int b) // 3 int vars 
  {
    Pos.x = x;
    Pos.y = y;
    Width = w;
    Height = h;
    Color = color(r, g, b);
    Text = t;
  }

  void update() //must be placed in void draw() to work
  {
    if (mousePressed == true && mouseButton == LEFT && Pressed == false)
    {
      Pressed = true;
      if (mouseX>= Pos.x && 
        mouseX <= Pos.x+Width &&
        mouseY >= Pos.y && 
        mouseY <= Pos.y+Height)
        //mouse x is greater or equal to our position x and mouse x is lesser og equal to our position inclusive the width. 
        // AND mouseY has to be greater og equal to the position y and the mouseY has to be smaller og equal to position y inclusive the height
      {
        Clicked = true;
      }
    } else
    {
      Clicked = false;
      Pressed = false;
    }
  }

  //Now we move on to rendering out ButtonRed
  void render() 
  //must be placed in void draw() to render the button to the screen
  {
    fill(Color);
    rect(Pos.x, Pos.y, Width, Height);

    fill(0); //black
    //now we want to center the text
    textAlign(CENTER, CENTER); //it makes everything align with out position
    text(Text, Pos.x+(Width/2), Pos.y+(Height/2)); // halfway of where our width was added to it. We wanna ass half the height to ensure that the text is in the middle
  }

  boolean isClicked() 
  //making a function
  {
    return Clicked; // this is the function that we use. It has to be in a an if statement to check if the button has been clicked
  }
}//class

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

//Code where class should be integrated in:

import processing.serial.*;

Serial myPort;
String serialInterface = "/dev/cu.usbmodem1411 (Arduino Uno)";
int lastX = -1;
int lastY = -1;
int x;
int y;
String nextXY;
RED ButtonRed;

void setup() {
  size(512, 512); // size of canva
  background(255); //colour of background, white
  // myPort = new Serial(this, Serial.list()[1], 9600); //finding my port in arduino
  // myPort.bufferUntil('\n'); //it stores the the memory until before SerialEvent
  ButtonRed = new RED(100, 100, 100, 50, "RED", 0, 200, 200); //integrating my class

  //variables for color
  // color backg = color(0, 0, 0);
  //color lines = color(348, 90, 90);
}

void draw() {
  /*
  String[] parts = splitTokens(nextXY);
   
   if (parts.length >= 2) {
   x = int(parts[0])/2;
   y = int(parts[1])/2;
   
   if (lastX >= 0 && lastY >= 0) {
   line(x, y, lastX, lastY);
   }
   lastX = x;
   lastY = y;
   }
   
   */
  ButtonRed.update();
  ButtonRed.render();
}

void mouseClicked() {
  background(255);
  if (ButtonRed.isClicked())
  {
    println("Here");
  }
}

void serialEvent(Serial p) {
  nextXY = p.readString();
}
//

```

---

<div class="post-metadata">

**Author:** ![Beto](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/beto/32/11677_2.png) [@Beto](https://discourse.processing.org/u/Beto)\
**Post date:** [January 5, 2021, 4:15pm UTC](https://discourse.processing.org/t/please-delete-this-post-problems-with-constructor-of-class/26796/4 "2021-01-05T16:15:37Z")

</div>

Hi Chrisir 🙂

Thank you so much for your help and thank you for helping a rookie, this is much appreciated.

I am really happy right now

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 5, 2021, 5:05pm UTC](https://discourse.processing.org/t/please-delete-this-post-problems-with-constructor-of-class/26796/5 "2021-01-05T17:05:21Z")

</div>

You’re welcome!

Chrisir
