ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JavaFX 8带Alpha的画布快照

2019-10-11 01:03:07  阅读:661  来源: 互联网

标签:java javafx canvas



我目前正在开发一个绘画程序(类似于Gimp和Photoshop),为了做到这一点,我将需要图层.我创建了一个名为JImage的类,该类具有ArrayList< Canvas>.层和一些方法.

public Image toImage(){ //Returns the final image which is all its layers combined into one canvas and snapshotted.
    Canvas c = new Canvas(width, height); //width and height are determined in the constructor
    for(int i=layers.size()-1;i>=0;i--){
        Canvas currLayer = layers.get(i);
        c.getGraphicsContext2D().drawImage(currLayer.snapshot(new SnapshotParameters(), new WritableImage(width,height)));
    }
    return c.snapshot(new SnapshotParameters(), new WritableImage(width,height));
}

我的问题是,当您执行canvas.snapshot(SnapshotParameters,WritableImage)时,不包括alpha层,并且背景始终为白色.这样可以防止我将其发送到没有丑陋白色背景的文件中.有什么办法可以从具有alpha层的多个画布中获取图像?我希望将JavaFX用于此解决方案,因此请在JavaFX的范围内提供解决方案.

解决方法:

快照之前,将SnapshotParameters的Set the fill设置为Color.TRANSPARENT.

SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT);
Image snapshot = currLayer.snapshot(params, null);

从javadoc:

Sets the fill to the specified value. This is used to fill the entire image being rendered prior to rendering the node. A value of null indicates that the color white should be used for the fill. The default value is null.



标签:java,javafx,canvas

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有