Problem with using a string variable to define a function within a class
in
Programming Questions
•
1 years ago
I am making an app in which I have a selectionTool class from which I can make different shape selectionTool objects which each are related to a small photoshop-like icon displayed on the screen that represents this object's shape.
Currently, in my constructor, I have a String variable which I want to use to either have a rectangular selection tool (in which case the object would be instantiated with "rect") or else an elliptical selection tool (in which case the object would be instantiated with "ellipse").
The variable is called shapeMarquee but it does not work. However, if I substitute that variable (it's in two places right below each other) for either rect or ellipse, I get the selection tool to work fine.
It's probably a syntax problem but I can't see how to make it work. Here is my code:
SelectionTool selectRectangle;
SelectionTool selectEllipse;
PImage iconR, iconE;
void setup() {
size (screen.width, screen.height);
iconR=loadImage("rectangularmarquee.jpg");
iconE=loadImage ("ellipticalmarquee.jpg");
selectRectangle = new SelectionTool (0, 0, 0, 0, "rect", iconR, 0, 0);
// selectEllipse=new SelectionTool (0, 0, 0, 0, "ellipse", iconE, 0, 50);
}
void draw() {
background (204);
selectRectangle.select();
selectRectangle.display();
// selectEllipse.select();
// selectEllipse.display();
}
// mouse and keyboard events
void mousePressed () {
selectRectangle.click (mouseX, mouseY);
// selectEllipse.click (mouseX, mouseY);
}
void mouseDragged() {
}
void mouseReleased () {
selectRectangle.release ();
// selectEllipse.release();
}
void keyPressed () {
}
abstract class InteractiveBoxes {
int boxPosX, boxPosY, boxPosW, boxPosH;
int boxPosXOff, boxPosYOff;
boolean over = false;
boolean pressed = false;
InteractiveBoxes (int tempBoxPosX, int tempBoxPosY, int tempBoxWidth, int tempBoxHeight) {
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
boxPosW=tempBoxWidth;
boxPosH=tempBoxHeight;
}
void rollOver () {
if ((mouseX>=boxPosX) && (mouseX <=boxPosX+boxPosW) && (mouseY>=boxPosY) && (mouseY<=boxPosY+boxPosH)) {
over=true;
}
else {
over=false;
}
}
void click (int mX, int mY) {
boxPosX=mouseX;
boxPosY=mouseY;
boxPosW=mouseX-boxPosX;
boxPosH=mouseY-boxPosY;
}
void press (int mX, int mY) {
if (over==true) {
pressed=true;
boxPosXOff=mX-boxPosX;
boxPosYOff=mY-boxPosY;
}
} // end of the press function
void drag (int mX, int mY) {
if (pressed==true) {
boxPosX=mX-boxPosXOff;
boxPosY=mY-boxPosYOff;
}
} // end of the drag function
void release () {
pressed=false;
} // end of release function
}
class SelectionTool extends InteractiveBoxes {
String shapeMarquee;
PImage icon;
int iconXPos, iconYPos;
int SelectX, SelectY, SelectW, SelectH;
// constructor of the SelectionTool class
SelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, String selectShapeMarquee, PImage iconS, int tempIconXPos, int tempIconYPos) {
super (boxPosX, boxPosY, boxPosW, boxPosH);
iconXPos=tempIconXPos;
iconYPos=tempIconYPos;
shapeMarquee=selectShapeMarquee;
icon=iconS;
} // end of the constructor
// methods of the SelectionTool class
void release () {
pressed=false;
SelectX=boxPosX;
SelectY=boxPosY;
SelectW=mouseX-boxPosX;
SelectH=mouseY-boxPosY;
} // end of release function
void select () {
strokeWeight(1);
stroke(0, 0, 255);
fill (0, 0, 0, 0);
if (mousePressed) {
shapeMarquee (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
}
else {
shapeMarquee (SelectX, SelectY, SelectW, SelectH);
}
stroke(0);
}
void display () {
image (icon, 0, 0);
} // end of the display function
} // end of the SelectionTool class
/* THIS IS THE ALGORITHM FOR THE ELLIPSE MARQUEE TOOL; THE ONLY DIFFERENCE IS
THAT "rect" is substituted for "ellipse" and I added ellipseMode as well
void select () {
strokeWeight(1);
stroke(0, 0, 255);
fill (0, 0, 0, 0);
if (mousePressed) {
ellipseMode(CORNER);
ellipse (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
}
else {
ellipse (SelectX, SelectY, SelectW, SelectH);
}
stroke(0);
}
*/
Currently, in my constructor, I have a String variable which I want to use to either have a rectangular selection tool (in which case the object would be instantiated with "rect") or else an elliptical selection tool (in which case the object would be instantiated with "ellipse").
The variable is called shapeMarquee but it does not work. However, if I substitute that variable (it's in two places right below each other) for either rect or ellipse, I get the selection tool to work fine.
It's probably a syntax problem but I can't see how to make it work. Here is my code:
SelectionTool selectRectangle;
SelectionTool selectEllipse;
PImage iconR, iconE;
void setup() {
size (screen.width, screen.height);
iconR=loadImage("rectangularmarquee.jpg");
iconE=loadImage ("ellipticalmarquee.jpg");
selectRectangle = new SelectionTool (0, 0, 0, 0, "rect", iconR, 0, 0);
// selectEllipse=new SelectionTool (0, 0, 0, 0, "ellipse", iconE, 0, 50);
}
void draw() {
background (204);
selectRectangle.select();
selectRectangle.display();
// selectEllipse.select();
// selectEllipse.display();
}
// mouse and keyboard events
void mousePressed () {
selectRectangle.click (mouseX, mouseY);
// selectEllipse.click (mouseX, mouseY);
}
void mouseDragged() {
}
void mouseReleased () {
selectRectangle.release ();
// selectEllipse.release();
}
void keyPressed () {
}
abstract class InteractiveBoxes {
int boxPosX, boxPosY, boxPosW, boxPosH;
int boxPosXOff, boxPosYOff;
boolean over = false;
boolean pressed = false;
InteractiveBoxes (int tempBoxPosX, int tempBoxPosY, int tempBoxWidth, int tempBoxHeight) {
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
boxPosW=tempBoxWidth;
boxPosH=tempBoxHeight;
}
void rollOver () {
if ((mouseX>=boxPosX) && (mouseX <=boxPosX+boxPosW) && (mouseY>=boxPosY) && (mouseY<=boxPosY+boxPosH)) {
over=true;
}
else {
over=false;
}
}
void click (int mX, int mY) {
boxPosX=mouseX;
boxPosY=mouseY;
boxPosW=mouseX-boxPosX;
boxPosH=mouseY-boxPosY;
}
void press (int mX, int mY) {
if (over==true) {
pressed=true;
boxPosXOff=mX-boxPosX;
boxPosYOff=mY-boxPosY;
}
} // end of the press function
void drag (int mX, int mY) {
if (pressed==true) {
boxPosX=mX-boxPosXOff;
boxPosY=mY-boxPosYOff;
}
} // end of the drag function
void release () {
pressed=false;
} // end of release function
}
class SelectionTool extends InteractiveBoxes {
String shapeMarquee;
PImage icon;
int iconXPos, iconYPos;
int SelectX, SelectY, SelectW, SelectH;
// constructor of the SelectionTool class
SelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, String selectShapeMarquee, PImage iconS, int tempIconXPos, int tempIconYPos) {
super (boxPosX, boxPosY, boxPosW, boxPosH);
iconXPos=tempIconXPos;
iconYPos=tempIconYPos;
shapeMarquee=selectShapeMarquee;
icon=iconS;
} // end of the constructor
// methods of the SelectionTool class
void release () {
pressed=false;
SelectX=boxPosX;
SelectY=boxPosY;
SelectW=mouseX-boxPosX;
SelectH=mouseY-boxPosY;
} // end of release function
void select () {
strokeWeight(1);
stroke(0, 0, 255);
fill (0, 0, 0, 0);
if (mousePressed) {
shapeMarquee (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
}
else {
shapeMarquee (SelectX, SelectY, SelectW, SelectH);
}
stroke(0);
}
void display () {
image (icon, 0, 0);
} // end of the display function
} // end of the SelectionTool class
/* THIS IS THE ALGORITHM FOR THE ELLIPSE MARQUEE TOOL; THE ONLY DIFFERENCE IS
THAT "rect" is substituted for "ellipse" and I added ellipseMode as well
void select () {
strokeWeight(1);
stroke(0, 0, 255);
fill (0, 0, 0, 0);
if (mousePressed) {
ellipseMode(CORNER);
ellipse (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
}
else {
ellipse (SelectX, SelectY, SelectW, SelectH);
}
stroke(0);
}
*/
1