如何在javaFX中混合两个图像

时间:2014-02-27 21:13:40

标签: javafx

我有两个关于存储在两个单独图像中的数据的图。我需要将它们放在一个图像中,这样我才能看出它的不同之处。如何在javaFX中完成这个?

1 个答案:

答案 0 :(得分:13)

<强>解决方案

将两张图片放在Group中,然后通过设置BlendMode来应用blendMode of the topmost Node

ImageView bottom = new ImageView(coke);
ImageView top    = new ImageView(pepsi);
top.setBlendMode(BlendMode.DIFFERENCE);

Group blend = new Group(
        bottom,
        top
);

可执行样本

接受百事可乐的挑战?你能“发现”差异吗?

PepsiChallenge

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.BlendMode;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/** Blend a coke can and a pepsi can to find the difference. */
public class PepsiChallenge extends Application {
    @Override
    public void start(Stage stage) {
        Image coke = new Image(
            "http://icons.iconarchive.com/icons/michael/coke-pepsi/256/Coca-Cola-Can-icon.png"
        );

        Image pepsi = new Image(
            "http://icons.iconarchive.com/icons/michael/coke-pepsi/256/Pepsi-Can-icon.png"
        );

        ImageView bottom = new ImageView(coke);
        ImageView top = new ImageView(pepsi);
        top.setBlendMode(BlendMode.DIFFERENCE);

        Group blend = new Group(
                bottom,
                top
        );

        HBox layout = new HBox(10);
        layout.getChildren().addAll(
                new ImageView(coke),
                blend,
                new ImageView(pepsi)
        );
        layout.setPadding(new Insets(10));
        stage.setScene(new Scene(layout));
        stage.show();
    }

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