Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX Canvas draw image with transparency

Tags:

java

javafx

With a JavaFX Canvas, you can use drawImage(). However, is there anyway to draw the image with transparency (draw it with only 50% of opacity) or tint it with color?

like image 498
Voldemort Avatar asked Jan 22 '15 22:01

Voldemort


People also ask

Does JavaFX support PNG?

JavaFX supports the image formats like Bmp, Gif, Jpeg, Png.

What is GraphicsContext?

This class is used to issue draw calls to a Canvas using a buffer. Each call pushes the necessary parameters onto the buffer where they will be later rendered onto the image of the Canvas node by the rendering thread at the end of a pulse.

What is GraphicsContext in Java?

A graphics context is an object belonging to the class, Graphics. Instance methods are provided in this class for drawing shapes, text, and images. Any given Graphics object can draw to only one location. In this chapter, that location will always be one of Java's GUI components, such as an applet.

What is JavaFX canvas?

Canvas class is a part of JavaFX. Canvas class basically creates an image that can be drawn on using a set of graphics commands provided by a GraphicsContext. Canvas has a specified height and width and all the drawing operations are clipped to the bounds of the canvas.


1 Answers

Methods to Control Canvas Draw Operations

There are methods to control the attributes of canvas drawing operations:

  • setGlobalAlpha() controls opacity.
  • setGlobalBlendMode() controls how items blend with items they are drawn over.
  • setEffect() sets the effect to be applied after the next draw call.

Sample Usage

batsignal

The source image used by the program is:

bat signal

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";
}
like image 162
jewelsea Avatar answered Oct 26 '22 19:10

jewelsea