Click Button to Add an Object -- OOP
in
Programming Questions
•
1 year ago
Hi,
I have written a small program that allows you to manipulate objects with the mouse and some key strokes. I want to start with a blank canvas with a button. When you click on the button a new ShapeMove is added to the canvas. I feel like this should be pretty simple, but I'm stumped.
Here's my code so far. Sorry it's so long. The button class is at the very bottom. I'm using PImages but in this example it's all rectangles.
Thanks
- int x; // shape x
- int y; // shape y
- int h; // shape height
- int w; // shape width
- int b = 0; //counts button clicks
- boolean captured;
- boolean isOverSomething;
- ShapeMove[] shapeMove = new ShapeMove[3];
- Button button;
-
- void setup()
- {
- size(800, 500);
- for (int i = 0; i <= 2; i++)
- {
- shapeMove[i] = new ShapeMove(0, 0, 200, 200);
- }
- button = new Button(200, 300);
- }
-
- void draw()
- {
- background(255);
- button.display();
- if (b > 0)
- {
- fill(120, 250,0);
- shapeMove[b-1].update();
- shapeMove[b-1].display();
- }
- }
-
- //////////////////////////////////////////////////////////////////////////
- class ShapeMove
- {
- int x;
- int y;
- int h;
- int w;
- int origx;
- int origy;
- int deltax;
- int deltay;
- int posXdiff;
- int posYdiff;
- int xdragdelta;
- int ydragdelta;
- boolean over;
- boolean locked = false;
- boolean imCaptured = false;
- boolean overMe = false;
-
-
- ShapeMove(int _x, int _y, int _h, int _w)
- {
- x = _x;
- y = _y;
- h = _h;
- w = _w;
- }
-
- void isover()
- {
- if (mouseX >= x && mouseX <=x+w &&
- mouseY >= y && mouseY <= y+h)
- {
- over = true;
- isOverSomething = true;
- }
- else
- {
- over = false;
- isOverSomething = false;
- }
- }
- void overme()
- {
- if(isOverSomething) // over something
- {
- if(overMe) // over me!
- {
- if(!over)
- {
- overMe = false;
- isOverSomething = false;
- }
- }
- }
- else // not over anything at the moment
- {
- if(over) // over me!
- {
- overMe = true;
- isOverSomething = true;
- }
- }
- }
-
- void update()
- {
- if (over && mousePressed && !imCaptured && !captured)
- {
- deltax = x - mouseX;
- deltay = y - mouseY;
- origx = x;
- origy = y;
- captured = true;
- imCaptured = true;
- }
- if (!mousePressed && imCaptured)
- {
- captured = false;
- imCaptured = false;
- }
- if (imCaptured)
- {
- xdragdelta = x - (mouseX + posXdiff);
- ydragdelta = y - (mouseY + posYdiff);
- x = mouseX + deltax;
- y = mouseY + deltay;
- }
- }
-
- void changesize()
- {
- if (over && keyPressed)
- {
- if (key == 'b')
- {
- h += 2;
- w += 2;
- }
- if (key == 's')
- {
- h -= 2;
- w -= 2;
- }
- }
- }
- void display()
- {
- rect(x, y, h, w);
- changesize();
- }
- }
- /////////////////////////////////////////////////////////////////////////
- class Button
- {
- int rectX, rectY; // Position of square button
- int rectSize = 50; // Diameter of rect
- color rectColor;
- color rectHighlight;
- boolean rectOver = false;
- Button(int _rectX, int _rectY)
- {
- smooth();
- rectColor = color(0);
- rectHighlight = color(51);
- rectX = _rectX;
- rectY = _rectY;
- }
- void display()
- {
- update(mouseX, mouseY);
- if(rectOver) {
- fill(rectHighlight);
- } else {
- fill(rectColor);
- }
- stroke(255);
- rect(rectX, rectY, rectSize, rectSize);
- }
- void update(int x, int y)
- {
- if ( overRect(rectX, rectY, rectSize, rectSize) )
- {
- rectOver = true;
- } else {
- rectOver = false;
- }
- }
- void mousePressed()
- {
- if(rectOver)
- {
- b++;
- }
- }
- boolean overRect(int x, int y, int width, int height)
- {
- if (mouseX >= x && mouseX <= x+width &&
- mouseY >= y && mouseY <= y+height) {
- return true;
- } else {
- return false;
- }
- }
- }
1