Question

As for now I am using:

import java.awt.MouseInfo;
import java.awt.Point;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;

public class nnn {

public static void main(String[] args) {

    OutputStream os = new ByteArrayOutputStream(); 

    while(true) {

        Point point = MouseInfo.getPointerInfo().getLocation();

        try {

            ByteBuffer byteBuffer = ByteBuffer.allocate(8);

            byteBuffer.putInt(point.x);
            byteBuffer.putInt(point.y);

            byte[] buffer = byteBuffer.array();

            os.write(buffer);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
}

to get mouse position on the screen. It is still the slowest part of the program.

It lasts 191520028ns (0.191) so the frequency is 5 times in a second and i would need something close to movie motion (25 times per second).

I am not using any Swing or AWT components. The app is strictly command line.

Was it helpful?

Solution

The code you posted really hasn't helped (it's not compilable, it has a reference to a class that you didn't post the code to, and it's not demonstrating your problem). However, using the shell of what you posted, I did the following:

public class Test {

    public static void main(final String[] args) {
        new Test().doStuff();
    }

    private void doStuff() {
        long start;
        long end;
        while (true) {
            start = System.nanoTime();
            Point point = MouseInfo.getPointerInfo().getLocation();
            end = System.nanoTime();
            System.out.println("Time taken: " + (end - start));
        }
    }

}

This gave me output as follows:

Time taken: 183564075
Time taken: 24498
Time taken: 16448
Time taken: 16448
Time taken: 16798
Time taken: 16098
Time taken: 16798
Time taken: 21697
Time taken: 16448
Time taken: 17498
Time taken: 26597

So while there is a large value for the very first run, this can easily be explained as a ramp up issue, if you are running this in a tight loop, it quickly drops to a few ms.

If you don't like that, you could always track the mouse yourself, and store the last position using a MouseMotiontListener or a MouseMotitonAdapter

public class Test {

    public static void main(final String[] args) {
        new Test().doStuff();
    }

    private void doStuff() {
        long start;
        long end;
        MouseTracker tracker = new MouseTracker();
        while (true) {
            start = System.nanoTime();
            Point point = tracker.getLocation();
            end = System.nanoTime();
            System.out.println("Time taken: " + (end - start));
        }
    }

    class MouseTracker extends MouseMotionAdapter {

        private Point location;

        @Override
        public void mouseMoved(final MouseEvent e) {
            location = e.getLocationOnScreen();
        }

        public Point getLocation() {
            return location;
        }

    }

}

Using that gives much lower values (even for ramp-up)

Time taken: 3150
Time taken: 350
Time taken: 0
Time taken: 0
Time taken: 0
Time taken: 0
Time taken: 350
Time taken: 0
Time taken: 350

OTHER TIPS

MouseInfo.getPointerInfo().getLocation(); ..is still the slowest part of the program. ..the frequency is 5 times in a second..

I truly doubt that getting the mouse position is taking so long. Here is the evidence to suggest otherwise.

enter image description here

So no, that's not 5 times a second, it is almost 300,000 times a second!

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class SpeedOfGetMousePosition {

    int count;
    int xCount = 0;
    Thread t;

    SpeedOfGetMousePosition() {
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                while (true) {
                    Point point = MouseInfo.getPointerInfo().getLocation();
                    xCount += point.x;
                    count++;
                }
            }
        };
        t = new Thread(r1);
        t.start();

        Runnable r = new Runnable() {

            @Override
            public void run() {
                final JLabel l = new JLabel("Count..");

                ActionListener al = new ActionListener() {

                    int lastCount;

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int temp = count;
                        int num = temp-lastCount;
                        lastCount = temp;
                        l.setText("Count: " + num);
                    }
                };
                Timer timer = new Timer(1000,al);
                timer.start();

                JOptionPane.showMessageDialog(null, l);
            }
        };

        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }

    public static void main(String[] args) {
        new SpeedOfGetMousePosition();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top