Monday, June 20, 2016

50. Blend

Blend effect can be used to add together colors.


An opaque red rectangle is added to semitransparent green rectangle at top. We also do a similar operation to the lower two rectangles.


package ex50;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Blend;
import javafx.scene.effect.BlendMode;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Ex50 extends Application {
    
    @Override
    public void start(Stage stage) {
        Group root = new Group();
        
        Blend blend = new Blend();
        blend.setMode(BlendMode.ADD);
        
        Rectangle rect1 = new Rectangle();
        rect1.setX(75); rect1.setY(50);
        rect1.setWidth(50); rect1.setHeight(50);
        rect1.setFill(Color.rgb(255, 0, 0, 1)); // Red
        
        Rectangle rect2 = new Rectangle();
        rect2.setX(100); rect2.setY(50);
        rect2.setWidth(50); rect2.setHeight(50);
        rect2.setFill(Color.rgb(0, 255, 0, 0.5)); // Green, 0.5 opacity
        
        rect2.setEffect(blend);
        
        Rectangle rect3 = new Rectangle();
        rect3.setX(75); rect3.setY(150);
        rect3.setWidth(50); rect3.setHeight(50);
        rect3.setFill(Color.rgb(255, 0, 255, 1)); // Magenta
        
        Rectangle rect4 = new Rectangle();
        rect4.setX(100); rect4.setY(150);
        rect4.setWidth(50); rect4.setHeight(50);
        rect4.setFill(Color.rgb(0, 255, 255, 0.5)); // Cyan, 0.5 opacity
        
        rect4.setEffect(blend);
        
        root.getChildren().addAll(rect1, rect2, rect3, rect4);
        
        Scene scene = new Scene(root, 300, 250, Color.LIGHTYELLOW);
        stage.setTitle("Example 50. Blend");
        stage.setScene(scene);
        stage.show();
    }

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

This is the output:


No comments:

Post a Comment