2017-07-01 6 views
6

これまでのところ、いくつかの四角形が移動するJavaFXアプリケーションをコーディングしました。今は、矩形がウィンドウ内にまだ表示されているかどうか、または既に移動しているかどうかをチェックするメソッドを作成したいと思います。私のコードは以下のようになります。長方形のノードがウィンドウ内にあるかどうかを確認する方法

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()方法は、四角形の位置は、ウィンドウ内に残っているかどうかを確認する私の試みです。できます。しかし、より良い方法や、ウィンドウの境界線がどの矩形を横切るかを検出する方法はありますか?

+0

これはあなたを助けるかもしれない - それは 'ScrollPane'に、より焦点ですが:https://stackoverflow.com/questions/28701208/javafx-ask-wether-a-node-is-inside -viewport-or-not – Chris

答えて

2

1つの長方形は、それは2つのことを意味し、別の1の外にある場合:

  • 彼らは
  • 大きな1は完全にあなただから、小さい1

が含まれていない交差していませんこの方法は、次の方法を見ることができます:ここでは

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

はdemonstにMCVEです料金:

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()); 
    } 
} 
関連する問題