PLEASE DELETE THIS POST: Problems with constructor of Class

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

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

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

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

Also, I placed this in draw():

  ButtonRed.update();
  ButtonRed.render();

Here is the entire Sketch :



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();
}
//

1 Like

Hi Chrisir :slight_smile:

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

I am really happy right now

1 Like

You’re welcome!

Chrisir

1 Like