अब तक मैंने जावाएफएक्स एप्लिकेशन को कोड किया है जिसमें कुछ आयताकार घूमते हैं। अब मैं यह जांचने के लिए एक विधि बनाना चाहता हूं कि खिड़की में एक आयताकार अभी भी दिखाई दे रहा है या इससे पहले से बाहर निकल गया है। मेरे कोड इस तरह दिखता है:कैसे जांचें कि आयताकार नोड विंडो में है
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()
विधि आयत की स्थिति खिड़की में अब भी है, तो जाँच करने के लिए मेरे प्रयास है। यह काम करता हैं। लेकिन क्या एक बेहतर तरीका है या यह पता लगाने का एक तरीका है कि आयताकार किनारों की सीमा पार हो गई है?
इससे आपको यह पता मदद कर सकता है - हालांकि इस पर एक 'ScrollPane' और अधिक ध्यान केंद्रित है: https://stackoverflow.com/questions/28701208/javafx-ask-wether-a-node-is-inside -viewport या नहीं – Chris