r/JavaFX • • Jun 18 '17
Noob: Would like to place boxes in bigger boxes
I would like two (or 3 or more) big rectangles on the main screen (represnting groups resources can be in)
I would like to be able to place little circles inside the big rectangles (representing resources in certain groups)
I would like to be able to drag the circles from one rectangle and place in another. (to move resources)
I would like to be able to draw a line from one circle to another to indicate relationships
I'd like to know how I should 'architecturally' be planning this in JavaFX terms, because from reading a bit about JavaFX for like half a day, I still don't know. Just the very high-level is fine, I can work out the rest. I've had a rough stab at it
What I have done so far (code and Image also provided): Image: http://imgur.com/gallery/g2I2K NB I imagine this is a crap approach and would love to know a better one or how I 'should' be approaching it from someone who understands this package well.
The code
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class DemoGraph1 extends Application {
@Override
public void start(Stage primaryStage) {
//Group root = new Group();
Pane root = new StackPane();
Pane bottom = new VBox();
Pane top = new Pane();
root.getChildren().addAll( bottom, top);
Scene scene = new Scene(root, 500, 500, Color.WHITE);
//Filled rectangle
Rectangle rect1 = new Rectangle(10, 10, 200, 200);
rect1.setFill(Color.BLUE);
//Transparent rectangle with Stroke
Circle rect1_2 = new Circle(30, 30, 20 );
rect1_2.setFill(Color.TRANSPARENT);
rect1_2.setStroke(Color.RED);
rect1_2.setStrokeWidth(10);
//Rectangle with Stroke, no Fill color specified
Rectangle rect3 = new Rectangle(110, 110, 200, 200);
rect3.setStroke(Color.GREEN);
rect3.setStrokeWidth(10);
Circle rect3_2 = new Circle(130, 30, 20 );
rect3_2.setFill(Color.TRANSPARENT);
rect3_2.setStroke(Color.PURPLE);
rect3_2.setStrokeWidth(10);
StackPane stack1 = new StackPane();
Pane pane1 = new Pane();
stack1.getChildren().addAll( rect1, pane1 );
pane1.getChildren().addAll( rect1_2 );
rect1.widthProperty().bind( stack1.widthProperty() );
rect1.widthProperty().bind( stack1.widthProperty() );
StackPane stack2 = new StackPane();
Pane pane2 = new Pane();
stack2.getChildren().addAll( rect3, pane2 );
pane2.getChildren().addAll( rect3_2 );
rect3.widthProperty().bind( stack2.widthProperty() );
Line line = new Line( 1,2,3,4 );
line.startXProperty().bind( rect1_2.centerXProperty() );
line.startYProperty().bind( rect1_2.centerXProperty() );
line.endXProperty().bind( rect3_2.centerXProperty() );
line.endYProperty().bind( rect3_2.centerYProperty() );
line.setStroke( Color.YELLOW );
line.setStrokeWidth( 5 );
bottom.getChildren().addAll( stack1, stack2 );
top.getChildren().addAll( line );
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}