JavaFX Canvas 绘制透明图像
JavaFX Canvas draw image with transparency
使用 JavaFX Canvas,您可以使用 drawImage()
。然而,有没有办法用透明度绘制图像(只用 50% 的不透明度绘制)或用颜色着色?
控制方法Canvas绘制操作
有方法可以控制canvas绘图操作的属性:
- setGlobalAlpha() 控制不透明度。
- setGlobalBlendMode() 控制项目如何与被绘制的项目混合。
- setEffect() sets the effect 在下一次绘制调用后应用。
示例用法
程序使用的源图片为:
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.scene.effect.*;
import javafx.scene.image.Image;
import javafx.scene.paint.*;
import javafx.stage.Stage;
public class CanvasEffects extends Application {
@Override
public void start(Stage stage) {
final Image image = new Image(IMAGE_LOC);
final int NUM_IMGS = 5;
final double W = image.getWidth();
final double H = image.getHeight();
final Canvas canvas = new Canvas(W * NUM_IMGS, H);
final GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.GOLD);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setGlobalBlendMode(BlendMode.SCREEN);
for (int i = 0 ; i < NUM_IMGS; i++) {
final double opacity = 1 - ((double) i) / NUM_IMGS;
System.out.println(opacity);
gc.setGlobalAlpha(opacity);
gc.setEffect(new BoxBlur(i * 2, i * 2, 3));
gc.drawImage(image, i * W, 0);
}
stage.setScene(new Scene(new Group(canvas)));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
// icon license: Linkware (Backlink to http://uiconstock.com required) commercial usage not allowed.
private static final String IMAGE_LOC = "http://icons.iconarchive.com/icons/uiconstock/flat-halloween/128/Halloween-Bat-icon.png";
}
使用 JavaFX Canvas,您可以使用 drawImage()
。然而,有没有办法用透明度绘制图像(只用 50% 的不透明度绘制)或用颜色着色?
控制方法Canvas绘制操作
有方法可以控制canvas绘图操作的属性:
- setGlobalAlpha() 控制不透明度。
- setGlobalBlendMode() 控制项目如何与被绘制的项目混合。
- setEffect() sets the effect 在下一次绘制调用后应用。
示例用法
程序使用的源图片为:
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.scene.effect.*;
import javafx.scene.image.Image;
import javafx.scene.paint.*;
import javafx.stage.Stage;
public class CanvasEffects extends Application {
@Override
public void start(Stage stage) {
final Image image = new Image(IMAGE_LOC);
final int NUM_IMGS = 5;
final double W = image.getWidth();
final double H = image.getHeight();
final Canvas canvas = new Canvas(W * NUM_IMGS, H);
final GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.GOLD);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setGlobalBlendMode(BlendMode.SCREEN);
for (int i = 0 ; i < NUM_IMGS; i++) {
final double opacity = 1 - ((double) i) / NUM_IMGS;
System.out.println(opacity);
gc.setGlobalAlpha(opacity);
gc.setEffect(new BoxBlur(i * 2, i * 2, 3));
gc.drawImage(image, i * W, 0);
}
stage.setScene(new Scene(new Group(canvas)));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
// icon license: Linkware (Backlink to http://uiconstock.com required) commercial usage not allowed.
private static final String IMAGE_LOC = "http://icons.iconarchive.com/icons/uiconstock/flat-halloween/128/Halloween-Bat-icon.png";
}