We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Mouse Press on many Circles
Page Index Toggle Pages: 1
Mouse Press on many Circles (Read 1384 times)
Mouse Press on many Circles
Mar 19th, 2010, 1:46pm
 
hi, i'm new to processing and i have been messing with the MouseFunction example from the website where a circle is created and you can mouse press it and drag it to a different position.

I used some example code i got elsewhere to create the array of circles and have them bounce around the screen, each having different colours.

What the code does is it combines both of the examples and for the MouseFunction example, when i hover the mouse cursor over the circle it changes to different colours but on mouse press it changes to one solid colour, and it can be dragged around the screen:

Code:
 
float bx;
float by;
int bs = 20;
boolean bover = false;
boolean locked = false;
float bdifx = 0.0;
float bdify = 0.0;
ani_Circle[] circles = new ani_Circle[15];

void setup()
{
 size(500, 500);
 bx = width/2.0;
 by = height/2.0;
 noStroke();
 ellipseMode(RADIUS);  
 
 for(int i=0; i <15; i++)
{
  circles[i] = new ani_Circle (random(0),random(0), random(7), random(7), random(255),random(255),random(255));
 }
}

void draw()
{
 background(0);  
 for(int i=0; i<15; i++)
 {
  circles[i].update();
 }

  if (mouseX > bx-bs && mouseX < bx+bs &&
     mouseY > by-bs && mouseY < by+bs) {
      bover = true;  
      if(!locked) {
      stroke(255);
      fill(random(255),random(255),random(255));
   }
  }
  else {
   stroke(153);
   fill(153);
   bover = false;
  }

 ellipse(bx, by, bs, bs);
}


void mousePressed() {
 if(bover) {
   locked = true;
   fill(random(255), random(255), random(255));
 } else {
   locked = false;
 }
 bdifx = mouseX-bx;
 bdify = mouseY-by;
}

void mouseDragged() {
 if(locked) {
   bx = mouseX-bdifx;
   by = mouseY-bdify;
 }
}

void mouseReleased() {
 locked = false;
}



class ani_Circle
{
 float x,y,r,g,b;
 float xMove,yMove;
 public ani_Circle(float newx, float newy, float newxMove, float newyMove, float r1, float g1,float b1)
 { // constructor
   x = newx;
   y = newy;
   xMove = newxMove;
   yMove = newyMove;
   r = r1;
   g = g1;
   b = b1;
 }
 
 void update()
 {
   fill(r,g,b);
   noStroke();
   ellipse(x, y, 20,20);
   x+= xMove;
   y+= yMove;
   
   if(x<0)
   {
    xMove = -xMove;
   }
   if(y<0) {yMove = -yMove;}
   if(x>width) {xMove = -xMove;}
   if(y>height){yMove = -yMove;}
 }
}



I was wondering if is it possible to mouse press on any of those bouncing circles, have it change to many different colours and have it appear in a different colour when mouse press is false (it would carry on moving when there is no mouse activity).

Thanks for any feedback or suggestions =)
Re: Mouse Press on many Circles
Reply #1 - Mar 19th, 2010, 5:46pm
 
Like that?

Code:
ani_Circle locked; // Locked is an object instead of boolean.
// shows which object is selected
float bdifx = 0.0;
float bdify = 0.0;
ani_Circle[] circles = new ani_Circle[15];

void setup()
{
size(500, 500);
noStroke();
ellipseMode(RADIUS);

for(int i=0; i <15; i++)
{
circles[i] = new ani_Circle (random(0),random(0), random(2), random(2), random(255),random(255),random(255));
}
}

void draw()
{
background(0);
for(int i=0; i<15; i++)
{
circles[i].update();
}
}


void mousePressed() {
//Check if cursor is inside any circle and set it as the locked object
for (int i=0,j=circles.length;i<j;i++) {
ani_Circle c=circles[i];
if (mouseX > c.x-c.rad && mouseX < c.x+c.rad &&
mouseY > c.y-c.rad && mouseY < c.y+c.rad) {
locked=c;
bdifx = mouseX-c.x;
bdify = mouseY-c.y;
}
}
}

void mouseDragged() {
// If there is a locked object, drag it.
if(locked!=null) {
locked.x = mouseX-bdifx;
locked.y = mouseY-bdify;
}
}

void mouseReleased() {
// Release any locked objects.
locked = null;
}



class ani_Circle
{
float x,y,r,g,b;
float rad=20;
float xMove,yMove;
public ani_Circle(float newx, float newy, float newxMove, float newyMove, float r1, float g1,float b1)
{ // constructor
x = newx;
y = newy;
xMove = newxMove;
yMove = newyMove;
r = r1;
g = g1;
b = b1;
}

void update()
{
fill(r,g,b);
noStroke();
ellipse(x, y, rad,rad);
// If this object is not locked, move it.
if (locked!=this) {
x+= xMove;
y+= yMove;
}
// Otherwise, do nothing but randomize colors.
else {
r=random(255);
g=random(255);
b=random(255);
}
if(x<0)
{
xMove = -xMove;
}
if(y<0) {
yMove = -yMove;
}
if(x>width) {
xMove = -xMove;
}
if(y>height){
yMove = -yMove;
}
}
}

Re: Mouse Press on many Circles
Reply #2 - Mar 19th, 2010, 6:45pm
 
yes thank you very much =D

i forgot to ask but in the MouseFunction example theres the variables:

float bdifx
float bdify

what are they used for? i changed the values and nothing changed when i clicked on a circle and dragged them across the screen.

also what variables would i need to change for how far the circles can bounce around the screen, as i would like to test on adding some text at the bottom of the screen with some empty space to make it readable e.g. 1/3 of the screen cannot be reached by the circles so it just bounces around the remaining 2/3 of the screen

Thanks for the suggestions and feedback =D
Re: Mouse Press on many Circles
Reply #3 - Mar 20th, 2010, 5:52pm
 
Quote:
i forgot to ask but in the MouseFunction example theres the variables:

float bdifx
float bdify

what are they used for?


These hold the offset of the cursor at selection time from the center of the circle; essentially they allow you to drag it from and edge etc., otherwise everytime you selected sth, it's center would immediately move to the cursor's pos.

Quote:
also what variables would i need to change for how far the circles can bounce around the screen


You can just change the width and height that you are comparing the circle's position against with something you want.
Re: Mouse Press on many Circles
Reply #4 - Mar 21st, 2010, 6:34am
 
thanks for the reply yconst, really appreciate your help =D
Page Index Toggle Pages: 1