2014-03-28 81 views
0

佈置我有各種容器(ScrollPane中,組和HBox中垂直框)畫線在HBox中和垂直框

我想從0,0畫一條線到圓的中心內部的圓。

我認爲下面的代碼顯示了這個問題。有一些功能未知,我應該去 //這裏的一些代碼工作,在哪裏圓是使它工作

我有一個示例應用程序,我寫了簡化我的問題。

import java.util.Random; 

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.ScrollPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.scene.shape.Circle; 
import javafx.scene.shape.Line; 
import javafx.stage.Stage; 

public class TestAppForCoords extends Application { 

    //Scene Graph: 
    //ScrollPane - scroll 
    // - Group - root 
    // - HBox - hbox - with random padding 
    //  - VBox 
    //  - VBox - vbox - with random padding 
    //  - Circle - circle 
    //  - VBox 
    // - Group - lines 
    //  - Line - line 


    @Override 
    public void start(final Stage primaryStage) throws Exception { 
     Random rand = new Random(); 
     int randomNum1 = rand.nextInt((100 - 0) + 1) + 0; 
     int randomNum2 = rand.nextInt((100 - 0) + 1) + 0; 
     System.out.println(randomNum1); 
     System.out.println(randomNum2); 


     ScrollPane scroll = new ScrollPane(); 
     scroll.setPrefSize(500, 300); 
     Scene scene = new Scene(scroll); 

     primaryStage.setScene(scene); 


     Group root = new Group(); 

     HBox hbox = new HBox(); 

     hbox.setPadding(new Insets(randomNum1)); 

     VBox vbox = new VBox(); 
     hbox.getChildren().add(new VBox()); 

     vbox.setPadding(new Insets(randomNum2)); 
     hbox.getChildren().add(vbox); 
     hbox.getChildren().add(new VBox()); 

     Circle circle = new Circle(); 
     circle.setRadius(10); 
     vbox.getChildren().add(circle); 


     Group lines = new Group(); 
     root.getChildren().add(hbox); 
     root.getChildren().add(lines); 

     root.autosize(); 


     Line line = new Line(); 
     line.setStartX(0); 
     line.setStartY(0); 

     //SOME CODE HERE TO WORK OUT WHERE THE CIRCLE IS 
     line.setEndX(123); 
     line.setEndY(123); 

     lines.getChildren().add(line); 


     scroll.setContent(root); 

     primaryStage.show(); 
    } 



    public static void main(String[] args) { 
     System.out.println("Start JavaFXTestApp"); 
     launch(args); 
     System.out.println("End JavaFXTestApp"); 
    } 
} 

回答

1

最後我讓它工作。我可以添加一個setOnShown處理程序,它被調用來糾正這一行。

所以我發現,localToScene和sceneToLocal只有正在顯示的窗口之後

primaryStage.setOnShown(new EventHandler<WindowEvent>() { 
     @Override 
     public void handle(WindowEvent arg0) { 
      Point2D p = circle.localToScene(circle.getCenterX(),circle.getCenterY()); 
      p = line.sceneToLocal(p); 
      line.setEndX(p.getX()); 
      line.setEndY(p.getY()); 
     } 
    }); 

工作,所以最終的工作程序是:

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.Random; 

import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.geometry.Point2D; 
import javafx.scene.Group; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ScrollPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.scene.shape.Circle; 
import javafx.scene.shape.Line; 
import javafx.stage.Stage; 
import javafx.stage.WindowEvent; 

public class TestAppForCoords extends Application { 

    //Scene Graph: 
    //ScrollPane - scroll 
    // - Group - root 
    // - VBox - vboxEXTRA - with random padding 
    //  - HBox - hbox - with random padding 
    //  - VBox 
    //  - VBox - vbox - with random padding 
    //   - Circle - circle 
    //  - VBox 
    //  - Group - lines 
    //  - Line - line 

    Line line = new Line(); 
    Circle circle = new Circle(); 

    @Override 
    public void start(final Stage primaryStage) throws Exception { 
     Random rand = new Random(); 
     int randomNum1 = rand.nextInt((100 - 0) + 1) + 0; 
     int randomNum2 = rand.nextInt((100 - 0) + 1) + 0; 
     int randomNum3 = rand.nextInt((100 - 0) + 1) + 0; 
     System.out.println(randomNum1); 
     System.out.println(randomNum2); 
     System.out.println(randomNum3); 


     ScrollPane scroll = new ScrollPane(); 
     scroll.setPrefSize(500, 300); 
     Scene scene = new Scene(scroll); 

     primaryStage.setScene(scene); 

     Group root = new Group(); 

     HBox hbox = new HBox(); 

     hbox.setPadding(new Insets(randomNum1)); 

     VBox vbox = new VBox(); 
     hbox.getChildren().add(new VBox()); 

     vbox.setPadding(new Insets(randomNum2)); 
     hbox.getChildren().add(vbox); 
     hbox.getChildren().add(new VBox()); 

     circle.setRadius(10); 
     vbox.getChildren().add(circle); 

     Group lines = new Group(); 
     root.getChildren().add(hbox); 
     root.getChildren().add(lines); 

     root.autosize(); 
     root.requestLayout(); 



     lines.getChildren().add(line); 


     VBox vboxEXTRA = new VBox(); 
     vboxEXTRA.setPadding(new Insets(randomNum3)); 
     vboxEXTRA.getChildren().add(root); 


     scroll.setContent(vboxEXTRA); 

     line.setStartX(0); 
     line.setStartY(0); 

     //This dosen't work prob because we aren't drawing yet 
     Point2D p = circle.localToScene(circle.getCenterX(),circle.getCenterY()); 
     p = line.sceneToLocal(p); 
     line.setEndX(p.getX()); 
     line.setEndY(p.getY()); 


     primaryStage.setOnShown(new EventHandler<WindowEvent>() { 
      @Override 
      public void handle(WindowEvent arg0) { 
       Point2D p = circle.localToScene(circle.getCenterX(),circle.getCenterY()); 
       p = line.sceneToLocal(p); 
       line.setEndX(p.getX()); 
       line.setEndY(p.getY()); 
      } 
     }); 

     primaryStage.show(); 
    } 



    public static void main(String[] args) { 
     System.out.println("Start JavaFXTestApp"); 
     launch(args); 
     System.out.println("End JavaFXTestApp"); 
    } 
} 
+0

請編輯您的第一個合併的答案。快速瀏覽一下StackOverflow網站的工作原理。 –

+0

...你可以隨時接受你自己的口水。 –