Tuesday, June 21, 2016

61. AnchorPane

AnchorPane layout can fix nodes to certain distances from the four corners.


Here we set 4 rectangles (squares) and circles near each of the four coners.


package ex61;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Ex61 extends Application {

    @Override
    public void start(Stage stage) {
                
        Rectangle square1 = new Rectangle(25,25,Color.RED);
        AnchorPane.setTopAnchor(square1, 10.0);
        AnchorPane.setLeftAnchor(square1, 10.0);
        Circle circle1 = new Circle(25);
        circle1.setFill(Color.RED);
        AnchorPane.setTopAnchor(circle1, 50.0);
        AnchorPane.setLeftAnchor(circle1, 50.0);
        
        Rectangle square2 = new Rectangle(25,25,Color.BLUE);
        AnchorPane.setBottomAnchor(square2, 10.0);
        AnchorPane.setLeftAnchor(square2, 10.0);
        Circle circle2 = new Circle(25);
        circle2.setFill(Color.BLUE);
        AnchorPane.setBottomAnchor(circle2, 50.0);
        AnchorPane.setLeftAnchor(circle2, 50.0);
        
        Rectangle square3 = new Rectangle(25,25,Color.YELLOW);
        AnchorPane.setTopAnchor(square3, 10.0);
        AnchorPane.setRightAnchor(square3, 10.0);
        Circle circle3 = new Circle(25);
        circle3.setFill(Color.YELLOW);
        AnchorPane.setTopAnchor(circle3, 50.0);
        AnchorPane.setRightAnchor(circle3, 50.0);
        
        Rectangle square4 = new Rectangle(25,25,Color.GREEN);
        AnchorPane.setBottomAnchor(square4, 10.0);
        AnchorPane.setRightAnchor(square4, 10.0);
        Circle circle4 = new Circle(25);
        circle4.setFill(Color.GREEN);
        AnchorPane.setBottomAnchor(circle4, 50.0);
        AnchorPane.setRightAnchor(circle4, 50.0);
        
        AnchorPane anchorPane = new AnchorPane(square1, square2,
                square3, square4,
                circle1, circle2,
                circle3, circle4);
        
        Scene scene = new Scene(anchorPane, 300, 300, Color.LIGHTGREEN);
        stage.setTitle("Example 61. AnchorPane");
        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch();
    }
}

This is the output after the window is resized:


No comments:

Post a Comment