Still trying to toggle...simply toggle...
in
Programming Questions
•
1 years ago
After a week of wrestling, I am still trying to toggle a selection tool from rectangular marquee to elliptical marquee by clicking on their respective small icon BEFORE I begin my selection. Both these selection tools are classes inherited from an abstract class, itself inherited from an abstract class.
On the advice of a forum respondent, I am trying to use a toggle in the form of a variable called shapeSel whereby it is worth 1 for the ellipticalTool and 2 for the rectangular one. I assume that the problem is in my syntax but also how I lay it out in the main part of my program. I left the PictureGallery class in the following code but it is not directly related to the problem.
PictureGallery artWorks;
RectSelectionTool selectRectangle;
EllipseSelectionTool selectEllipse;
void setup() {
size (screen.width, screen.height);
background (225, 225, 255);
artWorks=new PictureGallery (50, 140, 330, 360, color (100));
selectRectangle = new RectSelectionTool (0, 0, 0, 0, color (0, 0, 255), 50, 50);
selectEllipse=new EllipseSelectionTool (0, 0, 0, 0, color (0, 0, 255), 50, 50);
}
void draw() {
background (225, 225, 255);
artWorks.rollOver();
artWorks.display();
artWorks.getIconX();
artWorks.getIconY();
selectRectangle.display();
selectEllipse.display();
}
// mouse and keyboard events
void mousePressed () {
artWorks.press(mouseX, mouseY);
if (selectRectangle.setToggle(2)) {
selectRectangle.click (mouseX, mouseY);
}
else if
(selectEllipse.setToggle(1))
{
selectEllipse.click (mouseX, mouseY);
}
}
void mouseDragged() {
artWorks.drag (mouseX, mouseY);
}
void mouseReleased () {
artWorks.release();
}
void keyPressed () {
artWorks.flipThrough();
if (key == CODED) {
if (keyCode == KeyEvent.VK_CONTROL && mousePressed)
{
selectRectangle.select();
selectRectangle.release ();
}
}
if (key == CODED) {
if (keyCode == KeyEvent.VK_CONTROL && mousePressed)
{
selectEllipse.select();
selectEllipse.release();
}
}
}
abstract class InteractiveBoxes {
int boxPosX, boxPosY, boxPosW, boxPosH;
color boxClr;
PFont fontA; // declare a variable that refers to the default font for all the subclasses
int fontHeight;
int marginSpacing;
int boxPosXOff, boxPosYOff;
boolean over = false;
boolean pressed = false;
InteractiveBoxes (int tempBoxPosX, int tempBoxPosY, int tempBoxWidth, int tempBoxHeight, color tempboxClr) {
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
boxPosW=tempBoxWidth;
boxPosH=tempBoxHeight;
boxClr=tempboxClr;
fontA = loadFont("ArialMT-48.vlw"); // Load the default font that the subclasses will be able to use
fontHeight=48; // set a default value for the font height (determined by the font size limit)
marginSpacing=5;
}
// methods of the abstract InteractiveBoxes class
void rollOver () {
if ((mouseX>=boxPosX) && (mouseX <=boxPosX+boxPosW) && (mouseY>=boxPosY) && (mouseY<=boxPosY+boxPosH)) {
over=true;
}
else {
over=false;
}
} // end of the rollOver function
void click (int mX, int mY) {
boxPosX=mouseX;
boxPosY=mouseY;
boxPosW=mouseX-boxPosX;
boxPosH=mouseY-boxPosY;
} // end the click function
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
abstract void display();
}
class PictureGallery extends InteractiveBoxes {
// ArrayList pictures;
PImage[] images=new PImage[10];
int imageIndex;
// constructor of the Picture Gallery class
PictureGallery (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr);
imageIndex=0;
for (int i = 0; i < images.length; i ++ ) {
images[i] = loadImage( "picture" + i + ".jpg" );
}
} // end of the constructor
// methods of the PictureGallery class
int getIconX () {
return boxPosX;
}
int getIconY () {
return (boxPosY+boxPosH);
}
PImage getImage () {
return images[imageIndex];
}
void flipThrough() {
if (key == CODED) {
if (keyCode == KeyEvent.VK_F1)
{
imageIndex++;
if (imageIndex>images.length-1) {
imageIndex=0;
}
}
}
}
}
abstract class SelectionTool extends InteractiveBoxes {
PImage icon;
int iconXPos, iconYPos, iconW, iconH;
int SelectX, SelectY, SelectW, SelectH;
int shapeSel;
// constructor of the abstract SelectionTool class
SelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr, int tempIconW, int tempIconH) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr);
iconW=tempIconW;
iconH=tempIconH;
shapeSel=0;
} // end of the constructor
// methods of the abstract SelectionTool class
boolean rollOver (boolean over) {
if (mousePressed && (mouseX>=iconXPos) && (mouseX <=iconXPos+iconW) && (mouseY>=iconYPos) && (mouseY<=iconYPos+iconH))
{
over=true;
}
else {
over=false;
}
return over;
} // end of the rollOver function
void click (int mX, int mY) {
if (over==true) {
}
else {
boxPosX=mouseX;
boxPosY=mouseY;
boxPosW=mouseX-boxPosX;
boxPosH=mouseY-boxPosY;
}
} // end the click function
void release () {
pressed=false;
SelectX=boxPosX;
SelectY=boxPosY;
SelectW=mouseX-boxPosX;
SelectH=mouseY-boxPosY;
} // end of release function
abstract void select ();
abstract void display ();
} // end of the SelectionTool class
class RectSelectionTool extends SelectionTool {
// constructor of the RectSelectionTool class
RectSelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr, int iconW, int iconH) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr, iconW, iconH);
icon=loadImage("rectangularmarquee.jpg");
} // end of the constructor
// methods of the RectSelectionTool class
void select () {
strokeWeight(1);
stroke(boxClr);
fill (0, 0, 0, 0);
rect (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
stroke(0);
}
int setToggle (int shapeSel) {
shapeSel=2;
return shapeSel;
}
void release () {
rect (SelectX, SelectY, SelectW, SelectH);
stroke(0);
}
void display () {
iconXPos=artWorks.getIconX();
iconYPos=iconH+artWorks.getIconY();
image (icon, iconXPos+iconW, iconYPos);
} // end of the display function
} // end of the RectSelectionTool class
class EllipseSelectionTool extends SelectionTool {
// constructor of the EllipseSelectionTool class
EllipseSelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr, int iconW, int iconH) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr, iconW, iconH);
icon=loadImage ("ellipticalmarquee.jpg");
} // end of the constructor
// methods of the EllipseSelectionTool class
void select () {
strokeWeight(1);
stroke(boxClr);
fill (0, 0, 0, 0);
ellipseMode(CORNER);
ellipse (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
stroke(0);
}
int setToggle (int shapeSel) {
shapeSel=1;
return shapeSel;
}
void release () {
ellipseMode(CORNER);
ellipse (SelectX, SelectY, SelectW, SelectH);
stroke(0);
}
void display () {
iconXPos=artWorks.getIconX();
iconYPos=iconH+artWorks.getIconY();
image (icon, iconXPos, iconYPos);
} // end of the display function
} // end of the EllipseSelectionTool class
On the advice of a forum respondent, I am trying to use a toggle in the form of a variable called shapeSel whereby it is worth 1 for the ellipticalTool and 2 for the rectangular one. I assume that the problem is in my syntax but also how I lay it out in the main part of my program. I left the PictureGallery class in the following code but it is not directly related to the problem.
PictureGallery artWorks;
RectSelectionTool selectRectangle;
EllipseSelectionTool selectEllipse;
void setup() {
size (screen.width, screen.height);
background (225, 225, 255);
artWorks=new PictureGallery (50, 140, 330, 360, color (100));
selectRectangle = new RectSelectionTool (0, 0, 0, 0, color (0, 0, 255), 50, 50);
selectEllipse=new EllipseSelectionTool (0, 0, 0, 0, color (0, 0, 255), 50, 50);
}
void draw() {
background (225, 225, 255);
artWorks.rollOver();
artWorks.display();
artWorks.getIconX();
artWorks.getIconY();
selectRectangle.display();
selectEllipse.display();
}
// mouse and keyboard events
void mousePressed () {
artWorks.press(mouseX, mouseY);
if (selectRectangle.setToggle(2)) {
selectRectangle.click (mouseX, mouseY);
}
else if
(selectEllipse.setToggle(1))
{
selectEllipse.click (mouseX, mouseY);
}
}
void mouseDragged() {
artWorks.drag (mouseX, mouseY);
}
void mouseReleased () {
artWorks.release();
}
void keyPressed () {
artWorks.flipThrough();
if (key == CODED) {
if (keyCode == KeyEvent.VK_CONTROL && mousePressed)
{
selectRectangle.select();
selectRectangle.release ();
}
}
if (key == CODED) {
if (keyCode == KeyEvent.VK_CONTROL && mousePressed)
{
selectEllipse.select();
selectEllipse.release();
}
}
}
abstract class InteractiveBoxes {
int boxPosX, boxPosY, boxPosW, boxPosH;
color boxClr;
PFont fontA; // declare a variable that refers to the default font for all the subclasses
int fontHeight;
int marginSpacing;
int boxPosXOff, boxPosYOff;
boolean over = false;
boolean pressed = false;
InteractiveBoxes (int tempBoxPosX, int tempBoxPosY, int tempBoxWidth, int tempBoxHeight, color tempboxClr) {
boxPosX=tempBoxPosX;
boxPosY=tempBoxPosY;
boxPosW=tempBoxWidth;
boxPosH=tempBoxHeight;
boxClr=tempboxClr;
fontA = loadFont("ArialMT-48.vlw"); // Load the default font that the subclasses will be able to use
fontHeight=48; // set a default value for the font height (determined by the font size limit)
marginSpacing=5;
}
// methods of the abstract InteractiveBoxes class
void rollOver () {
if ((mouseX>=boxPosX) && (mouseX <=boxPosX+boxPosW) && (mouseY>=boxPosY) && (mouseY<=boxPosY+boxPosH)) {
over=true;
}
else {
over=false;
}
} // end of the rollOver function
void click (int mX, int mY) {
boxPosX=mouseX;
boxPosY=mouseY;
boxPosW=mouseX-boxPosX;
boxPosH=mouseY-boxPosY;
} // end the click function
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
abstract void display();
}
class PictureGallery extends InteractiveBoxes {
// ArrayList pictures;
PImage[] images=new PImage[10];
int imageIndex;
// constructor of the Picture Gallery class
PictureGallery (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr);
imageIndex=0;
for (int i = 0; i < images.length; i ++ ) {
images[i] = loadImage( "picture" + i + ".jpg" );
}
} // end of the constructor
// methods of the PictureGallery class
int getIconX () {
return boxPosX;
}
int getIconY () {
return (boxPosY+boxPosH);
}
PImage getImage () {
return images[imageIndex];
}
void flipThrough() {
if (key == CODED) {
if (keyCode == KeyEvent.VK_F1)
{
imageIndex++;
if (imageIndex>images.length-1) {
imageIndex=0;
}
}
}
}
}
abstract class SelectionTool extends InteractiveBoxes {
PImage icon;
int iconXPos, iconYPos, iconW, iconH;
int SelectX, SelectY, SelectW, SelectH;
int shapeSel;
// constructor of the abstract SelectionTool class
SelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr, int tempIconW, int tempIconH) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr);
iconW=tempIconW;
iconH=tempIconH;
shapeSel=0;
} // end of the constructor
// methods of the abstract SelectionTool class
boolean rollOver (boolean over) {
if (mousePressed && (mouseX>=iconXPos) && (mouseX <=iconXPos+iconW) && (mouseY>=iconYPos) && (mouseY<=iconYPos+iconH))
{
over=true;
}
else {
over=false;
}
return over;
} // end of the rollOver function
void click (int mX, int mY) {
if (over==true) {
}
else {
boxPosX=mouseX;
boxPosY=mouseY;
boxPosW=mouseX-boxPosX;
boxPosH=mouseY-boxPosY;
}
} // end the click function
void release () {
pressed=false;
SelectX=boxPosX;
SelectY=boxPosY;
SelectW=mouseX-boxPosX;
SelectH=mouseY-boxPosY;
} // end of release function
abstract void select ();
abstract void display ();
} // end of the SelectionTool class
class RectSelectionTool extends SelectionTool {
// constructor of the RectSelectionTool class
RectSelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr, int iconW, int iconH) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr, iconW, iconH);
icon=loadImage("rectangularmarquee.jpg");
} // end of the constructor
// methods of the RectSelectionTool class
void select () {
strokeWeight(1);
stroke(boxClr);
fill (0, 0, 0, 0);
rect (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
stroke(0);
}
int setToggle (int shapeSel) {
shapeSel=2;
return shapeSel;
}
void release () {
rect (SelectX, SelectY, SelectW, SelectH);
stroke(0);
}
void display () {
iconXPos=artWorks.getIconX();
iconYPos=iconH+artWorks.getIconY();
image (icon, iconXPos+iconW, iconYPos);
} // end of the display function
} // end of the RectSelectionTool class
class EllipseSelectionTool extends SelectionTool {
// constructor of the EllipseSelectionTool class
EllipseSelectionTool (int boxPosX, int boxPosY, int boxPosW, int boxPosH, color boxClr, int iconW, int iconH) {
super (boxPosX, boxPosY, boxPosW, boxPosH, boxClr, iconW, iconH);
icon=loadImage ("ellipticalmarquee.jpg");
} // end of the constructor
// methods of the EllipseSelectionTool class
void select () {
strokeWeight(1);
stroke(boxClr);
fill (0, 0, 0, 0);
ellipseMode(CORNER);
ellipse (boxPosX, boxPosY, mouseX-boxPosX, mouseY-boxPosY);
stroke(0);
}
int setToggle (int shapeSel) {
shapeSel=1;
return shapeSel;
}
void release () {
ellipseMode(CORNER);
ellipse (SelectX, SelectY, SelectW, SelectH);
stroke(0);
}
void display () {
iconXPos=artWorks.getIconX();
iconYPos=iconH+artWorks.getIconY();
image (icon, iconXPos, iconYPos);
} // end of the display function
} // end of the EllipseSelectionTool class
1