import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

/**
 * The simplest Pane, like the Pane graphic: A, B and C are placed by hand (B overlaps A),
 * and D is a circle. The pane places nothing for you: each child stays where you put it,
 * and resizing the window moves nothing.
 *
 *   relocate / setLayoutX / setLayoutY   position of a child's top-left corner in the pane
 *   setPrefSize                          the size the pane gives a Label, Button or other Region
 *   setCenterX / setCenterY              a Circle positions itself by its center
 *   setTranslateX / setTranslateY        shift a node away from its layout position
 *   toFront / toBack                     which node is drawn on top
 *
 * The small red dots mark each label's layout position (layoutX, layoutY).
 * A Pane has no spacing, margin or alignment: there is no rule to apply them to.
 * Edit a value, run again, resize the window.
 */

public class SimplePaneDemo extends Application {

	// A colored label of the size you give it.
	private Label label ( String text, String color, double width,
	                      double height ) {
		Label label = new Label(text);
		label.setPrefSize(width,height);
		label.setAlignment(Pos.CENTER); // the text inside the label
		label.setStyle("-fx-background-color: " + color
		    + "; -fx-border-color: #333;");
		return label;
	}

	// A small red dot at a point, to show where a layout position is.
	private Circle marker ( double x, double y ) {
		return new Circle(x,y,4,Color.CRIMSON);
	}

	@Override
	public void start ( Stage stage ) {
		Label a = label("A","#cfe8ff",114,66);
		Label b = label("B","#c8f0c8",120,75);
		Label c = label("C","#ffe0b3",84,54);

		Circle circle = new Circle(33,Color.web("#f5c0c0"));
		circle.setStroke(Color.web("#b03a3a"));
		Label d = new Label("D"); // a plain label on top of the circle, for its
		                          // letter
		d.setPrefSize(66,66);
		d.setAlignment(Pos.CENTER);

		Pane pane = new Pane(a,b,c,circle,d);
		pane.setStyle("-fx-background-color: #eeeeee;");

		// ---- position: top-left corner, measured from the pane's top-left corner
		// (0, 0) ----
		a.relocate(42,57); // same as setLayoutX(42) + setLayoutY(57)
		b.setLayoutX(138); // B overlaps A; B is added later, so it is on top
		b.setLayoutY(99);
		c.relocate(222,39);
		// try: b.relocate(250, 150);

		// ---- a Circle positions itself by its center, not its corner ----
		circle.setCenterX(75);
		circle.setCenterY(189);
		d.relocate(75 - 33,189 - 33); // the label's corner: the circle's center
		                              // minus its radius

		// ---- the red dots: the same numbers as the relocate calls above ----
		pane.getChildren().addAll(marker(42,57),marker(138,99),marker(222,39));

		// ---- padding does NOT move children in a Pane ----
		pane.setPadding(new Insets(15)); // positions are still measured from the
		                                 // corner
		// try: pane.setPadding(new Insets(60)); and see that nothing moves

		// ---- translate: a shift on top of the layout position ----
		// try: c.setTranslateX(30); // C is drawn 30 to the right of where it was
		// placed
		// try: c.setTranslateY(60);

		// ---- drawing order ----
		// try: a.toFront(); // A is now drawn on top of B
		// try: circle.toBack();

		stage.setScene(new Scene(pane,400,280));
		stage.setTitle("Pane");
		stage.show();
	}

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