0

How can I calculate the average of 12 coordinates that are received from an mouse IR event in JAVA?

What I mean is: Every movement is sended in X and Y coordinates to me. Buffer these coordinates in 12 coordinates Calculate the average of these 12 coordinates

I know how to get the average of an array, but how does it work when the X is a coordinate? The X is not defined yet, because it change, so how can I do this?

Something like this does the job:

  int count = 0;
     double buffer = 0;
     while(true){ // loop waarin inputs binnen komen
     if(true){ // stel dat je een input binnenkrijgt
           count++;
           buffer += oX;
              if( count == 12 ){ // als je er 12 gekregen hebt
                 //send_output( buffer/12 ); // verzend
                 // reset buffer en count
                 System.out.println(buffer/12);
                         buffer = 0;
                     count = 0;

                  }
            }
        }

@edit Maybe to understand it better:

public void onIrEvent(IREvent arg0) {       
    int oX;
    int oY;

    oX = arg0.getAx()/10;
    oY = arg0.getAy()/10;

The oX and oY have to be putted in a buffer where there can be in 12 coordinates. Then calculate the average of them.

thanks in advance

kind regards

Pascal

  • You want a moving average of your mouse positions: http://stackoverflow.com/questions/3793400/is-there-a-function-in-java-to-get-moving-average – Vincent van der Weele Apr 10 '13 at 08:49
  • But I dont really know how this method or function is called. The IRevent of the mouse is giving me X and Y, then these have to be buffered and the average of these 12 will return. What makes the x and Y coordinate from the mouse diffrent from a list of arrays? – Pascal Jacobs Apr 10 '13 at 08:54

1 Answers1

0

Make bufferX , and bufferY two variables public ; and set them in this method

int i =0;
public void onIrEvent(IREvent arg0) {       
    int oX;
    int oY;

    oX = arg0.getAx()/10;
    oY = arg0.getAy()/10;

/////////////here set the buffer
  if(i<12){
     bufferX += oX;
     bufferY +=oY;
 }
 i++;
}

Try this Example:

public class MouseMotionEventDemo extends JPanel implements MouseMotionListener {

private int mX, mY;
int bufferX = 0;
int bufferY = 0;
int count = 0;

public MouseMotionEventDemo() {
    addMouseMotionListener(this);
    setVisible(true);
}

public void mouseMoved(MouseEvent me) {
    mX = (int) me.getPoint().getX();
    mY = (int) me.getPoint().getY();
    bufferX += mX;
    bufferY += mY;
    System.out.println("X: "+mX+"  Y:"+mY);
    count++;
    if (count == 12) { 
        System.out.println("X average =" +bufferX / 12);
        System.out.println("Y average =" +bufferY / 12);
        count = 0;

    }
}

public void mouseDragged(MouseEvent me) {
     }

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.getContentPane().add(new MouseMotionEventDemo());
    f.setSize(200, 200);
    f.show();


}

}

Alya'a Gamal
  • 5,624
  • 19
  • 34