Hello , Today I’m going to show the small demo of masking. In javafx masking is known as clipping. Every Node Object has

public final void setClip(Node value)

You can use the setClip to any Node.The setClip is used to mask for specific dimension of the existing Node.Let’s see the demo application of this masking in Detective Glass Application.

Source Code (DetectiveGlass.java):

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

/**
 *
 * @author Narayan G.M
 */
public class DetectiveGlass extends Application {

    
    public static void main(String[] args) {
        Application.launch(DetectiveGlass.class, args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Detective Glass");
        Group root = new Group();
        Scene scene = new Scene(root, 800, 470,Color.BLACK);

        //ImageView 
        ImageView maskView = new ImageView();    
        maskView.setCursor(Cursor.NONE);
        //Image is loaded 
        Image image = new Image(DetectiveGlass.class.getResource("oldimg.jpg").toExternalForm());
        maskView.setImage(image);
        //Mask Shape 
        final Circle glass = new Circle(100,100,100);
        maskView.setClip(glass);
        
        //adding to the root's children
        root.getChildren().add(maskView);
        
        //MouseMoved Event on Scene
        scene.setOnMouseMoved(new EventHandler(){

            public void handle(MouseEvent event) {                
                //Setting X and Y position of mask shape
                glass.setCenterX(event.getX());
                glass.setCenterY(event.getY());
            }
            
        });

        primaryStage.setScene(scene);  
        primaryStage.setVisible(true);
    }
}

Detective Glass (Masking)

A worked out example : Detective Glass

 

Feel free to comment.

Have a :) good day.