We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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...
I don't understand why this doesn't work :(
https://gist.github.com/anonymous/7199484
1 of the constructors expect 4 arguments:
But you're passing 6 instead: :-S
The other 1 demands 3:
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?) {}?
Still can't make this work...
https://gist.github.com/anonymous/7211726
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:
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
Processing got many overloaded functions too. For example background(). It varies for both kind and # of parameters. @-)
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-XSo it got 2 modal behaviors -> either clears canvas using specified color parameters or stamps a PImage to do that same work! $-)
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.
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.
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...