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 › Extending MouseListener in own class
Page Index Toggle Pages: 1
Extending MouseListener in own class (Read 823 times)
Extending MouseListener in own class
Aug 30th, 2009, 9:10am
 
I am writing a small visualisation that displays nodes for videos within a system.  Below, I have a Video node class that is used to display the data.  The problem I am having is that for each video I want to have mouse events, so they can be dragged around a bit and clicked on for more information.

At the moment, with the code below I seem to be getting a weird result that no matter where I click on the screen, I get the mousePressed event on every video, rather than only working when I click on a video object and only get it's results.

Can anyone help at all?

Thanks!

Code:
class Video implements MouseListener {
 
 int id;
 PVector position;
 PVector acceleration;
 float radius;
 float diameter;
 String title;
 color off_color;
 color on_color;
 boolean selected;
 boolean dragging;
 
 public Video(int new_id, PVector new_position, PVector new_acceleration,
             float new_radius, String new_title)
 {
   addMouseListener(this);
   id = new_id;
   position = new_position;
   acceleration = new_acceleration;
   radius = new_radius;
   title = new_title;
   
   diameter = radius / 2;
   off_color = color(int(random(255)), int(random(255)), int(random(255)));
   on_color = color(int(random(255)), int(random(255)), int(random(255)));
   
   selected = false;
   dragging = false;
 }
 
 public void mouseDragged(){
   
 }
 
 public void mouseReleased(MouseEvent e){
 
 }
 
 public void mousePressed(MouseEvent e) {
   print (e);
   print("Video " + id + "\n");
 }
 
 public void mouseClicked(MouseEvent e) {
   
 }
 
 public void mouseEntered(MouseEvent e) {
   
 }
 
 public void mouseExited(MouseEvent e) {
   
 }
 
 public void draw()
 {
   // If the cursor is within 2x the radius of current circle...
   if( dist(position.x, position.y, mouseX, mouseY) < radius ) {
     // Change fill color to green.
     fill(on_color, 100);
     // Remember user has circle "selected"  
     selected = true;
     
     // If user has mouse down and is moving...
     if(dragging){
       // Move circle to circle position
       position.x = mouseX;
       position.y = mouseY;
     }
   
   } else {
     // Keep fill color blue
     fill(off_color, 100);
     // User has nothing "selected"
     selected = false;
   }
   
   // Draw circle
   ellipse(position.x, position.y, radius, radius);
   
   // Move circle
   position.x += acceleration.x;
   position.y += acceleration.y;
   
   /* Wrap edges of canvas so circles leave the top
      and re-enter the bottom, etc...*/
   if( position.x < - diameter ){ position.x = width + diameter;  }
   if( position.x > width + diameter ){ position.x -= diameter;       }
   if( position.y < 0 - diameter     ){ position.y = height  + diameter; }
   if( position.y > height + diameter){ position.y -= diameter;       }
   
   // If current circle is selected...
   if(selected){
     // Set fill color of center dot to white..
     fill(255, 255, 255, 255);
     // ..and set stroke color of line to green.
     stroke(128, 255 ,0, 100);
   } else {            
     // otherwise set center dot color to black..
     fill(0, 0, 0, 255);
     // and set line color to turquoise.
     stroke(64, 128 ,128, 255);
   }
   
   // Turn off stroke/border
   noStroke();      
   
   // Draw dot in center of circle
   ellipse(position.x, position.y, 10, 10);
 }
}


At the moment, I am just displaying these randomly for testing, and I have a class that creates the field, and then my main code that displays it:
Code:
class VideoField {
 Video videos[];
 int count;
 
 public VideoField( int count)
 {
   this.count = count;
   videos = new Video[count];
   for (int i = 0; i < count; i++) {
     
     if (i == 1) {
       print ("Creating Central Video");
       videos[i] = new Video(i,
                           new PVector((width/2), (height/2)),
                           new PVector(0, 0),
                           30.0,
                           "Video " + i);
       
     } else {
       videos[i] = new Video(i,
                           new PVector(random(width), random(height)),
                           new PVector(random(-1, 1), random(-1, 1)),
                           30.0,
                           "Video " + i);      
     }
   }
 }
 
 public void draw() {
   for (int i = 0; i < videos.length ; i++) {
     videos[i].draw();
   }
 }
 
}

Code:
VideoField videofield;

void setup() {
 size(640,480);
 videofield = new VideoField( 10 );
 frameRate( 25 );
 smooth();
}
void draw() {
 background(255);
 videofield.draw();
}


Re: Extending MouseListener in own class
Reply #1 - Aug 30th, 2009, 11:04pm
 
No wonder. The mouse listener acts for a given AWT (or Swing) component. In the case of Processing, that's the whole sketch surface (a Frame).
You have to do the good old Processing way: listen for the event, and see if it happens inside the surface of your video.
Page Index Toggle Pages: 1