Need help with an exercise about overloading a constructor

Hello, I need some help with an exercise that I'm completely lost at how to do...

Exercise:

Make an overloaded version of the Button constructor, which takes an image as argument instead of dimension and color information. Rewrite the drawButton- and isPositionWithinButton-methods to support the use of an image to visually represent the button.

Create an instance of the Button class using the overloaded constructor and test the drawButton- and isPositionWithinButton-methods.

You can find here the code that I've already done (which needs to be changed into what the exercise asks of me): https://gist.github.com/anonymous/7197991

Answers

  • edited October 2013

    Maybe the color should be placed within the class/constructor rather than in my void setup?

    EDIT:

    Ah, crap... I think they're supposed to be inside the class/constructor...

  • edited October 2013
    • Button(PImage img) {}
    • Button(int ww, int hh, color cc) {}
  • edited October 2013

    I don't understand why this doesn't work :(

    https://gist.github.com/anonymous/7199484

  • edited October 2013

    1 of the constructors expect 4 arguments:

    Button(float x, float y, float sX, float sY) {}
    

    But you're passing 6 instead: :-S

    new Button(50, 50, 50, 50, #FF0000, "Button 1");
    

    The other 1 demands 3:

    Button(color c, String t, PImage image) {}
    
  • Don't I also need to pass the img into the new Button?

    Also I get an error with (float x, float y, float sX, float sY) {}

    I'm guessing it's because of the new Button (x, y, sX, sY, c, t, img?) {}?

  • edited October 2013

    Don't I also need to pass the img into the new Button?

    Perhaps the right question would be: "Does my Button class always demand a PImage object in order to work?" :-/

    In my understanding, your aim is to have a class which either draws a regular box using rect()
    or stamps a PImage to represent 1. Is that right? :-?

    That's why you'd need to have overloaded constructors to decide which modal an instance of it is gonna use.

    For the other guess, I'd say a PImage would be some picture that's gonna be associated w/ a particular Button object then.
    And would show up only when that is either hovered or clicked at.

    In that latter guessed case, you'd have 2 overloaded constructors:

    • 1 demanding a PImage to be associated to.
    • And 1 w/o it, but using a fixed default PImage instead.

    In short, overloaded constructors (and regular methods too) would serve either to assume certain defaults
    or then, to accept different kinds of input and behave accordingly. Or even both! :-B

  • edited October 2013

    Processing got many overloaded functions too. For example background(). It varies for both kind and # of parameters. @-)

    • background(rgb)
    • background(rgb, alpha)

    • background(gray)

    • background(gray, alpha)

    • background(v1, v2, v3)

    • background(v1, v2, v3, alpha)

    • background(image)

    Most interesting 1 is background(image), which demands a PImage in place of color values! 8-X
    So it got 2 modal behaviors -> either clears canvas using specified color parameters or stamps a PImage to do that same work! $-)

  • edited November 2013

    uhm... sorry but that didn't really help me to figure out why it doesn't work :( here is the exercises I'm trying to do:

    -

    Make an overloaded version of the Button constructor, which takes an image as argument instead of dimension and color information. Rewrite the drawButton- and isPositionWithinButton-methods to support the use of an image to visually represent the button.

    Create an instance of the Button class using the overloaded constructor and test the drawButton- and isPositionWithinButton-methods.

    -

    But you're correct regarding the "if it can't find the picture then it should make a rect" but not matter what i do i just keep ending up with a pure white screen :(

  • "not matter what i do"
    Show your last attempt. Paste it here (and select it, use the C button to highlight it) so we can easily see it and post remarks.

  • edited November 2013

    As the fool I'm I save them over each other so... sadly I don't have the 5 different attempts I did at this which all resulted in different errors... the one i posted the link for is the only one that "worked" or at least the only one that didn't get any errors.

    I also had someone else who is better at this to help me, but he couldn't figure out the problem... and this problem seems to show up again in the newer exercises I'm doing, so I'm quite stuck at the moment and can't seem to progress :(

  • I know I'm pretty much asking for someone to do this one for me, because I'm just knocking my head against my desk at the moment...

  • Here is a solution to the problem. Notice the second constructor still expects the x and y position for the button even if it is an image. It also needs the name of the image file.

    The constructor sets x and y then loads the image. To get the isPositionWithinButton to work without change it uses the size of the image as the size of the button.

    I have had to increase the display size so we can see the new button and I have used my own image since I didn't have the Twitter one.

    buttons

    Button myB1;
    Button myB2;
    Button myB3;
    Button myB4;
    
    void setup()
    {
      size(350, 250);
      background(255);
      myB1 = new Button(50, 50, 50, 50, color(255, 0, 0), "Button 1");
      myB2 = new Button(150, 50, 50, 50, color(0, 255, 0), "Button 2");
      myB3 = new Button(250, 50, 50, 50, color(0, 0, 255), "Button 3");
      myB4 = new Button(50, 150, "Red Apple.gif");
    }
    
    void draw() 
    {
      myB1.drawButton();
      myB2.drawButton();
      myB3.drawButton();
      myB4.drawButton();
    
      myB1.isPositionWithinButton(mouseX, mouseY);
      myB2.isPositionWithinButton(mouseX, mouseY);
      myB3.isPositionWithinButton(mouseX, mouseY);
      myB4.isPositionWithinButton(mouseX, mouseY);
    }
    
    
    
    
    
    class Button
    {
      float _x;   //position x
      float _y;   //position y
      float _sX;  //sizeX (width?)
      float _sY;  //sizeY (height?)
      color _c;   //color
      String _t;  //text
      PImage _image; //see drawButton
    
      //this is the constructor, constructors doesn't have a type so they can't be voids
      Button(float x, float y, float sX, float sY, color c, String t)
      {
        _x = x;
        _y = y;
        _sX = sX;
        _sY = sY;
        //this(x, y, sX, sY, t, null);
        _c = c;
        _t = t;
      }
      //
      Button(float x, float y, String imgFilename)
      {
        _x = x;
        _y = y;
        _image = loadImage(imgFilename); //"twitter_icon.png"
        _t = imgFilename;
        _sX = _image.width;
        _sY = _image.height;
      }
    
      void drawButton()
      {
        //you do this to avoid crashing the program if the image isn't there
        if (_image == null)
        {
          // fill(color(_c)); //fill(_red,_green,_blue); instead of color _c;
          fill(_c); //fill(_red,_green,_blue); instead of color _c;
          noStroke();
          text(_t, _x, _y-10);
          rect(_x, _y, _sX, _sY);
        }
        else
        {
          image(_image, _x, _y);
          noFill();
          stroke(0); // black border for button
          strokeWeight(1);
          rect(_x, _y, _sX, _sY);
        }
      }
      //
      boolean isPositionWithinButton(float x, float y)
      {
        if (x > _x && x < _x+_sX && y > _y && y < _y+_sY)
        {
          println(_t);
          return true;
        }
        else
          return false;
      }
      //
    }
    
  • edited November 2013

    Hello again and thanks for the help (again sorry about my late replies)

    But I still can't make this bloody thing work!

    either I get an error with myB4: https://gist.github.com/anonymous/7336272

    or I get it with the String in my Button constructor: https://gist.github.com/anonymous/7336227

  • First snippet, when I run it, I see the error: The constructor xxx.Button(int, int, String) is undefined, and indeed I don't see such constructor. Just create it!

    In the second snippet, I see the line:
    Button(float x, float y, PImage "twitter_icon.png") //this doesn't make any sense...
    and indeed, I must agree with the comment...

    You have some difficulties with understanding constructors, parameter passing, overloading, or some other concept, apparently...

    When you call: myB4 = new Button(300,150, "twitter_icon.png");
    what do you expect the parameters to be?
    I will guess the first two are the position of the button, and the last one is the name of an image, to display in the button.

    So, make a constructor taking two ints and one String:
    Button(int x, int y, String fileName)

    Actually, I see already this constructor in quark's answer (except there are floats instead of int, which is OK too). Have you tried to understand this code? If you don't understand something, just ask more information!

  • Yeah, I'm having some difficulties understanding what I'm doing which is most likely why I find this so hard... I'm working on a list I hope will be able to help me out when I code in processing, you can see it here but it isn't done yet: https://docs.google.com/document/d/12QyuTY_FT6_WUkPSkCOQ5Mkw1SFrCSGjtaUlrbqAQsA/

    What parameter? The ehm... PImage image, so it'll show a picture like it does for quark.

    But I did another assignment where I didn't have any problems, but I didn't use an image/String for that one... so I dunno if I'm missing something...

Sign In or Register to comment.