使图像看起来圆润

时间:2014-09-26 16:59:22

标签: java image javafx rounded-corners

我想让滚动窗格上的图像视图看起来呈圆形。我将图像视图和按钮添加到Vbox。然后将vbox添加到gridpane.Gridpane添加到滚动窗格。

我也使用了这个

Border-Radius and Shadow on ImageView

这是一个屏幕截图:enter image description here

这是我的代码:

 File file = new File("D:\\SERVER\\Server Content\\Apps\\icons");
        File[] filelist1 = file.listFiles();
        ArrayList<File> filelist2 = new ArrayList<>();

        for (File file1 : filelist1) {
            filelist2.add(file1);

        }
        btnar = new ArrayList<>();
        for (int i = 0; i < filelist2.size(); i++) {
            downloadbtn = new Button("Download");
            btnar.add(downloadbtn);
        }

        System.out.println(filelist2.size());
        gridpane.setAlignment(Pos.CENTER);
        gridpane.setPadding(new Insets(20, 20, 20,20));

        gridpane.setHgap(20);
        gridpane.setVgap(20);

        ColumnConstraints columnConstraints = new ColumnConstraints();
        columnConstraints.setFillWidth(true);
        columnConstraints.setHgrow(Priority.ALWAYS);
        gridpane.getColumnConstraints().add(columnConstraints);

        int imageCol = 0;
        int imageRow = 0;

        for (int i = 0; i < filelist2.size(); i++) {
            System.out.println(filelist2.get(i).getName());

            image = new Image(filelist2.get(i).toURI().toString());

            pic = new ImageView();
            pic.setFitWidth(130);
            pic.setFitHeight(130);

                clip=new Rectangle(pic.getFitWidth(), pic.getFitWidth());
                clip.setArcHeight(20);
                clip.setArcWidth(20);
                pic.setClip(clip);

                SnapshotParameters parameters=new SnapshotParameters();
                parameters.setFill(Color.TRANSPARENT);
                WritableImage image=pic.snapshot(parameters, null);

                pic.setClip(null);
                pic.setEffect(new DropShadow(20,Color.BLACK));

            pic.setImage(image);
            vb = new VBox();
            vb.getChildren().addAll(pic, (Button)btnar.get(i));

            gridpane.add(vb, imageCol, imageRow);
            GridPane.setMargin(pic, new Insets(2, 2, 2, 2));
            imageCol++;

            // To check if all the 3 images of a row are completed
            if (imageCol > 2) {
                // Reset Column
                imageCol = 0;
                // Next Row
                imageRow++;

            }

0 个答案:

没有答案