将图像分割为相等的图像大小并将其设置为窗格背景

时间:2018-03-15 19:12:33

标签: java image javafx-8

使用JAVA Fx

不能使用javax.awt图像,所以如何使用它,无需写入文件然后重新启动它并循环过程

    private class Tile extends StackPane {

        public Tile(double rectangularDimension, Image imgAWT) {

            Rectangle border = new Rectangle(rectangularDimension, rectangularDimension);
            border.setFill(null);
            border.setStroke(Color.BLACK);


            Image image = new Image(imgAWT, rectangularDimension, rectangularDimension, false, true, true);
            BackgroundImage myBI= new BackgroundImage(new Image("fd",this.getWidth(),this.getHeight(),false,true),
                    BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT,
                    BackgroundSize.DEFAULT);
            //then you set to your node
            this.setBackground(new Background(myBI));

            setAlignment(Pos.CENTER);
            getChildren().addAll(border);

            setOnMouseClicked(this::handleMouseClick);
        }
    }

我使用Sub Image分割图像 然后缩放它以适应瓷砖,我不知道如何将其设置为背景,如果我将其转换为图像视图然后添加到瓷砖作为节点(仅出现点) 所以我需要它作为背景,所以我可以伸展它

private Parent createContent() throws IOException {
    Pane root = new Pane();
    root.setPrefSize(ROOT_SIZE, ROOT_SIZE);

    String imagePath = "D:\\secret.jpg";
    BufferedImage image=ImageIO.read(new File(imagePath));

    int w = image.getWidth();
    int h = image.getHeight();

    board = new Tile[SPAN][SPAN];
    for (int i = 0; i < SPAN; i++) {
        for (int j = 0; j < SPAN; j++) {

            BufferedImage wim=image.getSubimage(i*w/SPAN,j*h/SPAN, w/SPAN, h/SPAN);
            Image sc=wim.getScaledInstance((ROOT_SIZE/SPAN)/SPAN,
                    (ROOT_SIZE/SPAN)/SPAN, Image.SCALE_AREA_AVERAGING);

            Tile tile = new Tile(ROOT_SIZE/SPAN, sc);
            tile.setTranslateX(j * ROOT_SIZE/SPAN);
            tile.setTranslateY(i * ROOT_SIZE/SPAN);

            root.getChildren().addAll(tile);

            board[j][i] = tile;
        }
    }
    return root;
}

0 个答案:

没有答案