Undefined Constructor
in
Programming Questions
•
3 years ago
I get this error all the time when trying to make a class, even when I copy code from a program that is working fine. A few posts mention declaring within setup, but no luck so far. Thanks for any help
MyButton button1, button2, button3;
void setup(){
size(800,800);
String image1, image2, image3;
image1 = "imageBase.JPG";
image2 = "imageHover.JPG";
image3 = "imageClick.JPG";
button1 = new MyButton(image1,image2,image3,100,100,100,100);
button2 = new MyButton(image1,image2,image3,200,200,200,200);
button3 = new MyButton(image1,image2,image3,400,400,300,300);
}
void draw(){
button1.update();
button2.update();
button3.update();
if (button1.getter_hovered()){
println("Button 1 is hovered over.");
}
if (button1.getter_clicked()){
println("Button 1 is clicked.");
}
}
class MyButton{
private String _pathBase, _pathHover, _pathClick;
private PImage imageBase, imageHover, imageClick, imageCurrent;
private int locX, locY, sizeX, sizeY;
private boolean clicked = false, hovered = false;
//constructor
MyButton(String _pathBase, String _pathHover, String _pathClick, int _locX, int _locY, int _sizeX, int _sizeY){
imageBase = loadImage(_pathBase);
imageHover = loadImage(_pathHover);
imageClick = loadImage(_pathClick);
locX = _locX;
locY = _locY;
sizeX = _sizeX;
sizeY = _sizeY;
//resize the images
imageBase.resize(sizeX, sizeY);
imageHover.resize(sizeX, sizeY);
imageClick.resize(sizeX, sizeY);
//assign the base image as the current image
imageCurrent = imageBase;
}
void update(){
//check where the mouse is
//is it over my button?
if (!mousePressed && mouseX>=locX && mouseX<=locX+sizeX && mouseY>=locY && mouseY<=locY+sizeY){
//set the hover image as the current image
imageCurrent = imageHover;
hovered = true;
clicked = false;
}
//is it clicking my button?
else if (mousePressed && mouseX>=locX && mouseX<=locX+sizeX && mouseY>=locY && mouseY<=locY+sizeY){
//set the clicked image to the current Image
imageCurrent = imageClick;
clicked = true;
hovered = false;
}
else{
imageCurrent = imageBase;
clicked = false;
hovered = false;
}
//update the onscreen image
image(imageCurrent,locX,locY);
}
boolean getter_clicked(){
return clicked;
}
boolean getter_hovered(){
return hovered;
}
} //end class
MyButton button1, button2, button3;
void setup(){
size(800,800);
String image1, image2, image3;
image1 = "imageBase.JPG";
image2 = "imageHover.JPG";
image3 = "imageClick.JPG";
button1 = new MyButton(image1,image2,image3,100,100,100,100);
button2 = new MyButton(image1,image2,image3,200,200,200,200);
button3 = new MyButton(image1,image2,image3,400,400,300,300);
}
void draw(){
button1.update();
button2.update();
button3.update();
if (button1.getter_hovered()){
println("Button 1 is hovered over.");
}
if (button1.getter_clicked()){
println("Button 1 is clicked.");
}
}
class MyButton{
private String _pathBase, _pathHover, _pathClick;
private PImage imageBase, imageHover, imageClick, imageCurrent;
private int locX, locY, sizeX, sizeY;
private boolean clicked = false, hovered = false;
//constructor
MyButton(String _pathBase, String _pathHover, String _pathClick, int _locX, int _locY, int _sizeX, int _sizeY){
imageBase = loadImage(_pathBase);
imageHover = loadImage(_pathHover);
imageClick = loadImage(_pathClick);
locX = _locX;
locY = _locY;
sizeX = _sizeX;
sizeY = _sizeY;
//resize the images
imageBase.resize(sizeX, sizeY);
imageHover.resize(sizeX, sizeY);
imageClick.resize(sizeX, sizeY);
//assign the base image as the current image
imageCurrent = imageBase;
}
void update(){
//check where the mouse is
//is it over my button?
if (!mousePressed && mouseX>=locX && mouseX<=locX+sizeX && mouseY>=locY && mouseY<=locY+sizeY){
//set the hover image as the current image
imageCurrent = imageHover;
hovered = true;
clicked = false;
}
//is it clicking my button?
else if (mousePressed && mouseX>=locX && mouseX<=locX+sizeX && mouseY>=locY && mouseY<=locY+sizeY){
//set the clicked image to the current Image
imageCurrent = imageClick;
clicked = true;
hovered = false;
}
else{
imageCurrent = imageBase;
clicked = false;
hovered = false;
}
//update the onscreen image
image(imageCurrent,locX,locY);
}
boolean getter_clicked(){
return clicked;
}
boolean getter_hovered(){
return hovered;
}
} //end class
1