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 & HelpSyntax Questions › Object(class) question, using mouseDragged(), etc
Page Index Toggle Pages: 1
Object(class) question, using mouseDragged(), etc (Read 851 times)
Object(class) question, using mouseDragged(), etc
Jun 11th, 2009, 11:24am
 
Hi,
Simple question,

Can a class (eventually an object) be extended with interactivity used by processing? With this i mean mousePressed(), mouseDragged(), etc. Or can these functions only be declared in the far most outer scope of processing (where void() and setup() is declared).

Well tnx for any help Smiley

Re: Object(class) question, using mouseDragged(), etc
Reply #1 - Jun 11th, 2009, 11:28am
 
Nevermind, already found it. Just plain Java.
Re: Object(class) question, using mouseDragged(), etc
Reply #2 - Jun 11th, 2009, 12:11pm
 
Just to throw in an answer here for others who find this. The way you would use mouseDragged(), mousePressed(), keyPressed(), etc., with an object is to simply call an object function from those methods in the outer sketch. So you would just do this in your main sketch body:

Code:

// assuming you've already defined a class and created an object

void keyPressed(){
  yourOject.yourMethod();
}


That way, you keep the two things separate, but one can access the other. System variables, on the other hand, are always accessible inside a class. So you can call things like mouseX and mouseY and frameCount from anywhere inside your class.
Re: Object(class) question, using mouseDragged(), etc
Reply #3 - Jun 11th, 2009, 1:18pm
 
True, but the main problem is the event. Every instance of an Event is bound to an Object. This is the nature of an event. I do not see the point in having alot of instances of some class being controlled by one function for checking which event occured on which object.

That is at least your solution for the problem. I'm still working on the code, will post it soon Smiley
Re: Object(class) question, using mouseDragged(), etc
Reply #4 - Jun 11th, 2009, 2:17pm
 
So when you are dragging your mouse on the screen, you want each of your objects to be aware of whether or not you are dragging them each specifically. Is that right? I think that could still be accomplished with minimal code. When the mouse is dragged, I would just pass in the mouseX and mouseY coordinates to each of your objects, then you can have them each test to see if they are being dragged. Something like this:

Code:

/* You would first initialize all your objects through an ArrayList or something similar. Then when the mouseDragged() method is called, you just do the following... */

void mouseDragged(){
    for(int 0; i<myObjects.size(); i++){
         (Object)object = (Object)myObjects.get(i);
         object.testForDrag(mouseX, mouseY);
    }
}


Then inside your class you have a method which cross references the mouse location to the location of your object, and determines whether or not it is being dragged. So anyway, just thinking aloud. If you have a better way then go for it...this is probably just the line of thinking I would take.  Smiley
Re: Object(class) question, using mouseDragged(), etc
Reply #5 - Jun 11th, 2009, 2:30pm
 
There is an easy way for you to design a class where each object has its own mouse handling routine and even its own draw routine and get Processing to call these methods for us. Cheesy

This sketch creates 10 balls and allows us to drag them to new positions with the mouse.

Code:
Ball[] balls = new Ball[10];

int tx, ty;
Ball selected;

void setup(){
 size(600,600);
 for(int i = 0; i < 10; i++){
   balls[i] = new Ball();
   registerDraw(balls[i]);
   registerMouseEvent(balls[i]);
 }
}

void draw(){
 background(0);
}


public class Ball{
 int x,y, radius, diameter;
 int fillCol, strokeCol;
 
 Ball(){
   int r,g,b;
   r = 128+(int)random(127);
   g = 128+(int)random(127);
   b = 128+(int)random(127);
   fillCol = color(r,g,b);
   strokeCol = color(r/2,g/2,b/2);
   x = 50 + (int)random(400);
   y = 50 + (int)random(400);
   diameter = 50 + (int)random(100);
   radius = diameter/2;
 }
 
 boolean isOver(int mx, int my){
   return (mx-x)*(mx-x) + (my-y)*(my-y) < radius*radius;
 }
 
 // This method is called automatically every loop
 // becasue we have registerDraw(this object)
 void draw(){
   ellipseMode(CENTER);
   fill(fillCol);
   stroke(strokeCol);
   strokeWeight(2);
   ellipse(x,y,diameter,diameter);
 }
 
 // This method is called automatically every loop
 // becasue we have registerMouseEvent(this object)
 void mouseEvent(MouseEvent event){
   if((selected == null && isOver(mouseX, mouseY))
                                     || selected == this){
     switch(event.getID()){
       case MouseEvent.MOUSE_PRESSED:
         selected = this;
         break;
       case MouseEvent.MOUSE_DRAGGED:
         x = x + mouseX - pmouseX;
         y = y + mouseY - pmouseY;
         break;
       case MouseEvent.MOUSE_RELEASED:
         selected = null;
         break;
     }
   }
 }
}


More info can be found here

http://dev.processing.org/libraries/basics.html
Re: Object(class) question, using mouseDragged(), etc
Reply #6 - Jun 11th, 2009, 2:32pm
 
ps the question might be simple but the answer may not be. Grin

Hope the previous post helps.
Re: Object(class) question, using mouseDragged(), etc
Reply #7 - Jun 11th, 2009, 3:28pm
 
Yes, that's in the right direction. Your sourcecode also clears everything up, was already on the page some minutes ago though Cheesy.

NoChinDeluxe, hope you see the difference in approach.

Tnx the both of you Smiley
Page Index Toggle Pages: 1