2016-01-07 43 views
0

我想做一個程序,從mongoDB的數據庫中找到座標值x和y。之後,減去這些值以便每次查找它們的各種值並搜索新座標的位置。如何獲得結果作爲值

結果的結構,例如:屏幕外(計數):{「X」:「1248」,「Y」:「698」}

我做了什麼,但我不知道如何給出(x2,x1,y2,y1)的值或者它有更好的解決方案。

感謝您的幫助..

  int x_and_y = 1; 

      while (cursorEvents.hasNext()) { 
      DBObject documentInEventCollection = cursorEvents.next(); 

      if("mouseMove".equals(documentInEventCollection.get("type"))){ 

      System.out.println("offscreen(" + x_and_y + "): " + documentInEventCollection.get("offScreen").toString()); 
      x_and_y++;          
      } 

      if("mouseMove".equals(documentInEventCollection.get("type"))){ 

       if((x2 - x1) < 0){ 

        System.out.println("Right"); 

       }else { 

        System.out.println("Left"); 
       } 
       if((y2 - y1) > 0){ 

        System.out.println("Down"); 

       }else{ 

        System.out.println("Up");      
       } 
      } 

回答

1

你可以嘗試,而這個替換您的循環:

while (cursorEvents.hasNext()) 
    { 
     DBObject doc1 = cursorEvents.next(); 
     if(cursorEvents.hasNext()) 
     { 
      DBObject doc2 = cursorEvents.next(); 
     } 

     if("mouseMove".equals(documentInEventCollection.get("type"))){ 

     System.out.println("offscreen(" + x_and_y + "): " + doc1.get("offScreen").toString()+","+doc2.get("offScreen").toString()); 
     x_and_y++;          
     } 
+0

謝謝您的回答。是的,我做到了,以後我將如何給(x2,x1,y2,y1)(offScreen)的值。 – William

+0

如果您想設置x offScreen的值,例如x2。你可以這樣做:'DBObject temp = doc1.get(「offScreen」);'然後'int x2 =(int)temp.get(「x」);' –

+0

aaaa ok非常感謝! – William