
package usecases;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.BoxBlur;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;

public class LiveResize extends Application
{
    private static final int WIDTH = 500;
    private static final int HEIGHT = 500;
    private static final double DIVISIONS = 10;
    
    private Scene scene = null;
    private Rectangle background = null;
    private Pane items = null;
    
    class ResizablePane extends Pane
    {
        final LiveResize app;
        
        public ResizablePane(final LiveResize app)
        {
            super();
            
            this.app = app;
        }

        @Override
        protected void layoutChildren()
        {
            super.layoutChildren();
            
            this.app.resize(getWidth(), getHeight());
        }
    }
    
    protected void resize(double width, double height)
    {
        //System.err.println(">>>>>>>>>>>>>> JFX resize");
        //System.err.println("                 "+width+"x"+height);
        double tickY = (double)height/(double)DIVISIONS;
        double offsetY = tickY / 10;
        double sizeY  = tickY - (offsetY+offsetY);
        
        double tickX = (double)width/(double)DIVISIONS;
        double offsetX = tickX / 10;
        double sizeX = tickX - (offsetX+offsetX);
        
        int i = 0;
        for (int y=0; y<DIVISIONS; y++)
        {
            double posY = (y*tickY) + offsetY;
            
            for (int x=0; x<DIVISIONS; x++)
            {
                double posX = (x*tickX) + offsetX;
                
                Ellipse item = (Ellipse)this.items.getChildren().get(i++);
                item.setRadiusX(sizeX/2.0);
                item.setRadiusY(sizeY/2.0);
                item.setCenterX(posX+(sizeX/2.0));
                item.setCenterY(posY+(sizeY/2.0));
            }
        }
        
        this.background.setWidth(width);
        this.background.setHeight(height);
    }
    
    private void init(Stage stage)
    {
        stage.setResizable(true);
        stage.setTitle("JFX Live Resize (usecase)");
        
        Pane root = new LiveResize.ResizablePane(this);
        root.setPrefSize(WIDTH, HEIGHT);
        root.setMinSize(100, 100);
        root.setMaxSize(10000, 10000);
        
        this.scene = new Scene(root, 1, 1);
        this.scene.setFill(Color.WHITE); // to highlight the resize problem
        
        stage.setScene(this.scene);
        
        this.items = new Pane();
        for (int y=0; y<DIVISIONS; y++)
        {
            for (int x=0; x<DIVISIONS; x++)
            {
                Ellipse item = new Ellipse(1, 1);
                item.setFill(Color.GRAY);
                this.items.getChildren().add(item);
            }
        }
        this.items.setEffect(new BoxBlur(5, 5, 1));
        this.background = new Rectangle(1, 1, Color.BLACK);
        root.getChildren().addAll(this.background, this.items);
        
        resize(WIDTH, HEIGHT);
    }
    
    @Override public void start(Stage primaryStage) throws Exception
    {
        init(primaryStage);
        
        primaryStage.setWidth(WIDTH);
        primaryStage.setHeight(HEIGHT);
        primaryStage.centerOnScreen();
        primaryStage.show();
    }
    
    public static void main(String[] args)
    {
        launch(args);
    }
}
