Drag and Release
in
Programming Questions
•
1 year ago
Hi.
Well I'm stuck again. All I'm trying to do is be able to click the mouse and drag the circle into any quadrant and have the ball 'snap' to the center of the quadrant upon a mouse release. When the mouse is released the color of the ball should change too - nothing occurs on a mouse press.
How can I drag and release the 'ball' at this point and also make it change color upon a release?
Thanks.
Well I'm stuck again. All I'm trying to do is be able to click the mouse and drag the circle into any quadrant and have the ball 'snap' to the center of the quadrant upon a mouse release. When the mouse is released the color of the ball should change too - nothing occurs on a mouse press.
How can I drag and release the 'ball' at this point and also make it change color upon a release?
Thanks.
- Ball all;
void setup() {
size(400, 400);
smooth();
all = new Ball();
}
void draw() {
background(255);
lines();
all.mouseClicked();
all.positions();
all.display();
}
void lines() {
line(width/2, 0, width/2, height);
line(0, height/2, width, height/2);
}
class Ball {
int posX;
int posY;
final int dia;
color col;
Ball() {
posX = mouseX;
posY = mouseY;
dia = 100;
col = color(random(0, 255), random(0, 255), random(0, 255));
}
void display() {
fill(col);
noStroke();
ellipse(posX, posY, dia, dia);
strokeWeight(3);
stroke(0);
}
void positions() {
if (posX <= width/2 && posY <=height/2) {
posX=width/4;
posY=height/4;
}
if (posX >= width/2 && posY <=height/2) {
posX = 3*width/4;
posY = height/4;
}
if (posX <= width/2 && posY >= height/2) {
posX = width/4;
posY = 3*height/4;
}
if (posX >= width/2 && posY >= height/2) {
posX = 3*width/4;
posY = 3*height/4;
}
}
void mouseClicked() {
posX = mouseX;
posY = mouseY;
}
}
1