JavaFX resize

After the previous posts, this is the first one that uncovers an issue, where a custom solution is required.

Adding a red rectangle in an anchor pane on the overlay will result in the following effect, when resizing the window:

The background image will always be centered when enlarging or decreasing the size of the window. The red rectangle will maintain its position from the top and left side when enlarging. When decreasing the window size the padding to the top and left will also be reduced.


What is required however is a layout that will resize the image in one or two directions when resized. For the background this would mean that the image always fills the whole window. For the red rectangle this means that the padding scales as the window is scaled and that the area is also scaled, meaning in this case that the rectangle goes all the way to the right and the bottom.

package javafxtest;

import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

 * @author andi
public class OverlayLayoutResizeTest extends Application {

    private Random rnd = new Random(System.nanoTime());

    public void start(Stage stage) throws FileNotFoundException {
        final StackPane root = new StackPane();
        File f = new File("/home/andi/Pictures/Adam.jpg");
        Image image = new Image(new FileInputStream(f));
        final double width = image.getWidth();
        final double height = image.getHeight();
        Canvas background = new Canvas(width, height);
        GraphicsContext context = background.getGraphicsContext2D();
        context.drawImage(image, 0, 0);


        AnchorPane animationPane = new AnchorPane();
        int rndX = 54;
        int rndY = 23;
        final double cWidth = width- rndX;
        final double cHeight = height - rndY;
        Canvas red = new Canvas(cWidth, cHeight);
        AnchorPane.setTopAnchor(red, (double) rndY);
        AnchorPane.setLeftAnchor(red, (double) rndX);
        AnchorPane.setBottomAnchor(red, 0.0);
        AnchorPane.setRightAnchor(red, 0.0);
        GraphicsContext context2 = red.getGraphicsContext2D();
        final Paint paintColor = Paint.valueOf(Color.RED.toString());
        context2.fillRect(rndX, rndY, cWidth, cHeight);
        System.out.println("Draw red rectangle "+rndX+", "+rndY+", "+cWidth+", "+cHeight);

        Scene scene = new Scene(root, width, height);

        stage.setTitle("Resize Overlay Test");

     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     * @param args the command line arguments
    public static void main(String[] args) {

Schreibe einen Kommentar