JavaFX effect on background

前端 未结 4 670
独厮守ぢ
独厮守ぢ 2020-11-30 01:05

I\'m using this to make a iOS-themed JavaFX2 (Java7) application with a frosted glass effect. The problem is that this code uses its effect on an ImageView. I\'d like it to

4条回答
  •  囚心锁ツ
    2020-11-30 01:12

    To expand on Jewlsea's answer .. And using the above example with JavaFX ONLY ..

    While the classes are not public API, it does avoid the AWT stack completely. Here is a non public example :

    // copy a background node to be frozen over.
        private Image copyBackground(Stage stage) {
            final int X = (int) stage.getX();
            final int Y = (int) stage.getY();
            final int W = (int) stage.getWidth();
            final int H = (int) stage.getHeight();
            final Screen screen = Screen.getPrimary();
            try {
    
                Robot rbt = com.sun.glass.ui.Application.GetApplication().createRobot();
                Pixels p = rbt.getScreenCapture(
                    (int)screen.getBounds().getMinX(),
                    (int)screen.getBounds().getMinY(), 
                    (int)screen.getBounds().getWidth(), 
                    (int)screen.getBounds().getHeight(), 
                    true
                );
    
                WritableImage dskTop = new WritableImage((int)screen.getBounds().getWidth(), (int)screen.getBounds().getHeight());
                dskTop.getPixelWriter().setPixels(
                    (int)screen.getBounds().getMinX(),
                    (int)screen.getBounds().getMinY(),
                    (int)screen.getBounds().getWidth(),
                    (int)screen.getBounds().getHeight(),
                    PixelFormat.getByteBgraPreInstance(),
                    p.asByteBuffer(), 
                    (int)(screen.getBounds().getWidth() * 4)
                );
    
                WritableImage image = new WritableImage(W,H);
                image.getPixelWriter().setPixels(0, 0, W, H, dskTop.getPixelReader(), X, Y);
    
                return image;
            } catch (Exception e) {
                System.out.println("The robot of doom strikes!");
                e.printStackTrace();
    
                return null;
            }
        }
    

    Results with a small dropshadow added:

        DropShadow shdw = new DropShadow();
        shdw.setBlurType(BlurType.GAUSSIAN);
        shdw.setColor(Color.GAINSBORO);
        shdw.setRadius(10);
        shdw.setSpread(0.12);
        shdw.setHeight(10);
        shdw.setWidth(10);
        layout.setEffect(shdw);
    

    fxScreenCapture

提交回复
热议问题