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.
IndexDiscussionExhibition › Multiple instances of a class....maybe
Page Index Toggle Pages: 1
Multiple instances of a class....maybe (Read 903 times)
Multiple instances of a class....maybe
Nov 23rd, 2009, 7:20pm
 
I am writing a sketch that displays a grid of circles that interact with the mouse cursor to change the size of the circles depending on the location of the mouse x and y. I have two classes: one for the circles and one for the force that manipulates the circles. Right now it does what I want it to do with a single force and then the up and down arrows control the range of the force. But I want to be able to use multiple forces at once. So I want to be able to click the mouse and lock the force in its current position and then get a new force that can be put anywhere in the field of circles and locked in its position with a mouse click and then get a new force and so on.

I am new to Processing and I finding it difficult to understand how this would be structured exactly. I have tried numerous ways using mousePressed() (within void draw, within the Force class, as void mousePressed, etc.) but I am not sure if I am doing it correctly or if I am even writing the sketch in a way that will make it do what I want.

So here is the sketch and any help would be greatly appreciated. You will see mousePressed the sketch but it is not doing anything at the moment.

Circle [] cir = new Circle [900];
Force fr;

void setup() {
 size(870 ,870);
 smooth();
 for(int i = 0; i < cir.length; i++) {
 cir[i] = new Circle((i % 30) * 30, (i / 30) * 30, 30);
 fr = new Force(2,2);
 }
}

void draw() {
 background(220);
   for(int i = 0; i < cir.length; i++){
 cir[i].display();
 fr.force();
 if (mousePressed) {
   fr.force();
 }
   }
}


class Circle {
 float x, y, diameter;
 float d = 10;
 
 Circle(float xpos, float ypos, float dia) {
   x = xpos;
   y = ypos;
   diameter = dia;
 }
   void display() {
   fill(255);
   stroke(1);
   ellipse(x,y,diameter,diameter);
   diameter = (dist(fr.a, fr.b, x, y)/d);
   if (keyPressed) {
     if (keyCode == UP) {
       d+=1;
     } else if (keyCode == DOWN) {
       d-=1;
     } if (d < 1) {
       d = 1;
     }
   }
 }
}

class Force {
 float a, b;
 
 Force(float apos, float bpos) {
   a = apos;
   b = bpos;
 }
 
 void force() {
   fill (255,0,0);
    noStroke();
     ellipse (a, b, 10, 10);
   
   a = mouseX;
   b = mouseY;
 }
}

Re: Multiple instances of a class....maybe
Reply #1 - Nov 23rd, 2009, 10:26pm
 
One thing i can tell you about your code is that you drew the force 900 times cause you put it in the circle loop. works much better if you dont do that...
Re: Multiple instances of a class....maybe
Reply #2 - Dec 7th, 2009, 11:58am
 
Thank you. It does run with a little less lag now.
Re: Multiple instances of a class....maybe
Reply #3 - Dec 7th, 2009, 12:52pm
 
In the future, you should ask for help in the Questions section. Thanks.
Page Index Toggle Pages: 1