2014-02-06 88 views
2

至於我現在用更快的方法:用於獲取鼠標位置

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(); 
     } 

    } 

} 
} 

讓屏幕上的鼠標位置。它仍然是該計劃中最慢的部分。

它持續191520028ns(0.191),所以頻率是每秒5次,我需要的東西接近電影運動(每秒25次)。我不使用任何Swing或AWT組件。該應用程序嚴格命令行。

+1

爲了更快地獲得更好的幫助,請發佈[MCTaRE](http://stackoverflow.com/help/mcve)(最小完整測試和可讀示例)。我真的懷疑,獲得鼠標位置需要很長時間。 –

+0

@AndrewThompson,加... –

+1

失敗!請*閱讀* MCTaRE上的文檔,而不是隨意猜測它的含義。這當然不是*'不可編譯的代碼片段'。 –

回答

3

您發佈的代碼真的沒有幫助(這不是編譯的,它有,你沒有張貼代碼一類的引用,它沒有表現出你的問題)。然而,使用你貼什麼樣的殼,我做了以下內容:

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)); 
     } 
    } 

} 

這給了我輸出如下:

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 

因此,儘管對於非常第一次運行一個較大的值,這可以很容易地解釋爲一個斜坡問題,如果你在一個緊密的循環中運行它,它會迅速下降到幾個毫秒。

如果你不喜歡,你可以隨時跟蹤自己的鼠標,使用MouseMotiontListenerMouseMotitonAdapter

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; 
     } 

    } 

} 

使用,讓值低得多(即使是斜升存儲的最後一個位置)

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 
1

MouseInfo.getPointerInfo().getLocation(); ..仍然是程序中最慢的部分。 ..頻率是在一秒鐘5次..

我真的懷疑,獲取鼠標位置需要這麼長時間。這是另有建議的證據。

enter image description here

所以,不,這不是第二個5次,這幾乎是30萬次第二

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(); 
    } 
} 
+1

> 100k +++++ :-) – mKorbel

+1

我希望我沒有運行:(在我的機器上它只有100k/s以上的速度)傻傻的工作機器 – Goibniu

+2

這不是一個答案,這是一條評論 –