Wednesday, June 22, 2016

65. RadioButton

A set of 3 RadioButtons, part of a ToggleGroup, can select between triangle, square or pentagon shape.


The visibility of the shape node is dependent on the state of button's selectedProperty through a unidirectional bind. For the Polygon shapes, we construct the double array using getDouble method which requires the number of sides (which is also equal to the number of vertices of points in our array).


package ex65;

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;

public class Ex65 extends Application {

    final int RADIUS = 100;
    final Point2D CENTER = new Point2D(200, 200);
    
    @Override
    public void start(Stage stage) {
                        
        Group root = new Group();
        
        RadioButton triangleButton = new RadioButton("Triangle");
        triangleButton.setLayoutX(10); triangleButton.setLayoutY(10);
        RadioButton squareButton = new RadioButton("Square");
        squareButton.setLayoutX(10); squareButton.setLayoutY(60);
        RadioButton pentagonButton = new RadioButton("Pentagon");
        pentagonButton.setLayoutX(10); pentagonButton.setLayoutY(110);
        
        Polygon triangle = new Polygon(getDouble(3));
        triangle.setFill(Color.RED);
        
        Polygon square = new Polygon(getDouble(4));
        square.setFill(Color.BLUE);
        
        Polygon pentagon = new Polygon(getDouble(5));
        pentagon.setFill(Color.GREEN);
        
        triangle.visibleProperty().bind(
                triangleButton.selectedProperty());
        square.visibleProperty().bind(
                squareButton.selectedProperty());
        pentagon.visibleProperty().bind(
                pentagonButton.selectedProperty());
        
        root.getChildren().addAll(triangleButton,
                squareButton, pentagonButton,
                triangle, square, pentagon);
        
        ToggleGroup toggleGroup = new ToggleGroup();
        toggleGroup.getToggles().addAll(triangleButton,
                squareButton, pentagonButton);
        
        Scene scene = new Scene(root, 400, 400, Color.CORNSILK);
        stage.setTitle("Example 65. RadioButton");
        
        stage.setScene(scene);
        stage.show();
    }
    
    private double[] getDouble (int n) {
        double[] array = new double[2*n];
        for (int i = 0; i<n; i++){
            array[2*i] = RADIUS*Math.cos(2*Math.PI/n*i)+CENTER.getX();
            array[2*i+1]=RADIUS*Math.sin(2*Math.PI/n*i)+CENTER.getY();
        }
        return array;
    }
    
    public static void main(String[] args) {
        launch();
    }
}

This is the output when the pentagon button is selected:


64. RotateTransition

With RotateTransition we can rotate a node.


Here the button is rotated for 20 seconds, from 0 to 45 degree, and another 20 seconds, going back.


package ex64;

import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Ex64 extends Application {

    @Override
    public void start(Stage stage) {
       
        Group root = new Group();
        
        Button button = new Button("RotateTransition");
        button.setPrefSize(250,250);
        button.setLayoutX(100); button.setLayoutY(100);
        
        RotateTransition rt = new RotateTransition(
                Duration.seconds(20), button);
        rt.setToAngle(45);
        rt.setFromAngle(0);
        rt.setAutoReverse(true);
        rt.setCycleCount(2);
        
        root.getChildren().add(button);
        
        button.setOnAction(e -> rt.play());
        
        Scene scene = new Scene(root, 500, 500, Color.PAPAYAWHIP);
        stage.setTitle("Example 64. RotateTransition");
        stage.setScene(scene);
        stage.show();
    }

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


This is the output during the animation:


63. DisplacementMap

With DisplacementMap effect, we can move pixel in x or y.


Here motion is based on absolute function on the y-axis There is no x-displacement.


package ex63;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.effect.DisplacementMap;
import javafx.scene.effect.FloatMap;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Ex63 extends Application {

    @Override
    public void start(Stage stage) {
        Group root = new Group();

        FloatMap map = new FloatMap(250, 100);
        for (int i = 0; i < map.getWidth(); i++) {
            double value = -Math.abs(.002*(i-125));
            for (int j = 0; j < map.getHeight(); j++)
                map.setSamples(i, j, 0f, (float)value);
        }
        DisplacementMap displacementMap = new DisplacementMap(map);
        
        Label label = new Label("Displacement");
        label.setFont(Font.font("Georgia", 50));
        label.setPrefHeight(100);
        label.setLayoutX(55); label.setLayoutY(30);
        label.setEffect(displacementMap);
        
        root.getChildren().add(label);

        Scene scene = new Scene(root, 450, 200, Color.LIGHTYELLOW);
        stage.setTitle("Example 63. DisplacementMap");
        stage.setScene(scene);
        stage.show();
    }

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

This is the output:


Tuesday, June 21, 2016

62. TextFlow

A TextFlow layout can be used to align text and other nodes.


We also apply a DropShadow effect.


package ex62;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class Ex62 extends Application {

    @Override
    public void start(Stage stage) {

        DropShadow dropShadow = new DropShadow();
        dropShadow.setOffsetX(4);
        dropShadow.setOffsetY(6);
        dropShadow.setColor(Color.PURPLE);
        
        Text text = new Text("A TextFlow Layout ");
        text.setFont(Font.font("Georgia", 64));
        text.setFill(Color.BLUE);
        text.setEffect(dropShadow);
        
        TextFlow root = new TextFlow(text);
        root.setTextAlignment(TextAlignment.CENTER);
        root.setLineSpacing(10);
        
        Scene scene = new Scene(root, 300, 300, Color.LIGHTGREEN);
        stage.setTitle("Example 62. TextFlow");
        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch();
    }
}

This is the output:


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:


60. TilePane

A TilePane organizes nodes in a table kind of view, with the height and width set by the biggest element in a row or column.


Here 12 different sized and colored rectangles are drawn and put in a new slot.


package ex60;

import java.util.ArrayList;
import java.util.Random;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Ex60 extends Application {

    final int NUMCOLORS = 12;
    @Override
    public void start(Stage stage) {
       
        ArrayList<Color> colors = new ArrayList<>();
        
        Random rand = new Random();
        
        int i, r, g, b;
        
        for (i = 0; i<NUMCOLORS; i++) {
            r = rand.nextInt(256);
            b = rand.nextInt(256);
            g = rand.nextInt(256);
            colors.add(Color.rgb(r, g, b));
        }
        
        TilePane tilePane = new TilePane();
        tilePane.setPadding(new Insets(50, 50, 50, 50));
        tilePane.setHgap(25);
        tilePane.setVgap(25);
        
        for (i = 0; i < colors.size(); i++) {
            int w = 50+rand.nextInt(51); // 50 to 100
            int h = 50+rand.nextInt(51); // 50 to 100
            Rectangle rect = new Rectangle(w, h, colors.get(i));
            rect.setArcWidth(25);
            rect.setArcHeight(25);
            tilePane.getChildren().add(rect);
        }

        Scene scene = new Scene(tilePane, Color.GRAY);
        stage.setTitle("Example 60. TilePane");
        stage.setScene(scene);
        stage.show();
    }

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

This is the output:


59. SepiaTone

The SepiaTone effect can be applied to a node, usually an image. Here we chained an ImageInput with the SepiaTone effect.


The SepiaTone effect is used to create image looking older and more monochrome.


package ex59;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.ImageInput;
import javafx.scene.effect.SepiaTone;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class Ex59 extends Application {

    @Override
    public void start(Stage stage) {
        Group root = new Group();
        
        String hillary = "Hillary";
        String clinton = "Clinton";

        SepiaTone sepiaTone = new SepiaTone();
        sepiaTone.setLevel(.5);
        
        String fileName = hillary + clinton + ".png";

        Image image = new Image(fileName);
        ImageInput imageInput = new ImageInput(image);
        sepiaTone.setInput(imageInput);
        root.setEffect(sepiaTone);
 
        Scene scene = new Scene(root);
        stage.setTitle("Example 59. SepiaTone");
        stage.setScene(scene);
        stage.show();
    }

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

This is the output: