2017-07-01 57 views
6

Şimdiye kadar, bazı dikdörtgenlerin etrafında hareket ettiği bir JavaFX uygulaması yazdım. Şimdi, pencerede bir dikdörtgenin hala görünüp görünmeyeceğini veya daha önceden taşınmış olup olmadığını kontrol etmek için bir yöntem oluşturmak istiyorum. Benim kod benziyor:Dikdörtgen düğümün pencerede olup olmadığını nasıl kontrol edebilirim

import javafx.animation.AnimationTimer; 
import javafx.application.Application; 
import javafx.geometry.Point2D; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.layout.Pane; 
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 

public class Test extends Application { 
    private Pane root = new Pane(); 
    private Rectangle rect = new Rectangle(150,150,15,15); 
    private Point2D velocity = new Point2D(2,1); 

    private Pane createContent(){ 
     root.setPrefSize(500,500); 
     root.getChildren().add(rect); 

     AnimationTimer timer = new AnimationTimer() { 
      @Override 
      public void handle(long now) { 
       update(); 
      } 
     }; 
     timer.start(); 

     return root; 
    } 

    private void update(){ 
     if (outOfWindow(rect)) { 
      System.out.println("out of window...\n"); 

     }else { 
      System.out.println("in window...\n"); 
     } 

     rect.setTranslateX(rect.getTranslateX() + velocity.getX()); 
     rect.setTranslateY(rect.getTranslateY() + velocity.getY()); 
    } 

    private boolean outOfWindow(Node node) { 
     if (node.getBoundsInParent().intersects(node.getBoundsInParent().getWidth(), node.getBoundsInParent().getHeight(), 
      root.getPrefWidth() - node.getBoundsInParent().getWidth() * 2, 
      root.getPrefHeight() - node.getBoundsInParent().getHeight() * 2)){ 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     primaryStage.setScene(new Scene(createContent())); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

outOfWindow() yöntem dikdörtgenin konumu penceresinde hala olup olmadığını kontrol etmek benim girişimidir. İşe yarıyor. Ama dikdörtgenin hangi pencere kenarını geçtiğini tespit etmenin daha iyi bir yolu ya da yolu var mı? bir dikdörtgen başka birinin dışında

+0

Bu size yardımcı olabilir - bir 'ScrollPane' daha fazla odaklanan olmasına rağmen: https://stackoverflow.com/questions/28701208/javafx-ask-wether-a-node-is-inside -viewport-veya-olmayan – Chris

cevap

2

Eğer iki anlama gelir:

  • tümüyle küçük bir

Yani içermiyor

  • büyük bir kesişmeyen senin İşte

    private boolean inTheWindow(Rectangle rect) { 
        return rect.getBoundsInParent().intersects(root.getLayoutBounds()) 
         || root.getLayoutBounds().contains(rect.getBoundsInParent()); 
    } 
    

    demonst için MCVE geçerli: yöntemi sonraki yol bakabilirsiniz hızı:

    public class FXBounds extends Application { 
    
        private Pane root; 
    
        @Override 
        public void start(Stage primaryStage) { 
    
         root = new Pane(); 
    
         Scene scene = new Scene(root, 300, 250); 
    
         primaryStage.setTitle("Hello World!"); 
         primaryStage.setScene(scene); 
         primaryStage.show(); 
    
         check(1,1,50,50, true); 
         check(100,100,50,50, true); 
         check(-5,-5,30,30, true); 
         check(-50,0,30,30, false); 
        } 
    
        private void check(int x, int y, int width, int height, boolean in) { 
         Rectangle rect = new Rectangle(x, y, width, height); 
         root.getChildren().add(rect); 
         System.out.println(in == inTheWindow(rect)); 
        } 
    
        private boolean inTheWindow(Rectangle rect) { 
         return rect.getBoundsInParent().intersects(root.getLayoutBounds()) || root.getLayoutBounds().contains(rect.getBoundsInParent()); 
        } 
    }