【问题标题】:JavaFX - Is there a method to print one image multiple times using imageview?JavaFX - 有没有一种方法可以使用 imageview 多次打印一个图像?
【发布时间】:2016-12-12 03:13:51
【问题描述】:

鉴于下面的代码,我正在尝试多次打印出单个图像(存储在变量“图像”中)。关于我将如何做这件事的任何建议?任何信息都会很有用。

    Image image = new Image("tileset.png");
    ImageView tileset = new ImageView();
    tileset.setImage(image);

    Rectangle2D viewport1 = new Rectangle2D(0,16,16,16); //(selected pixels)
    tileset.setViewport(viewport1);
    int length = 40, width= 40;  // declare size of array (print 40x40)

    // loop through grid, fill every tile with image 'image'. 
    // currently only fills position (40,40) with the image. 
    for(int y = 0; y < length; y++)
    {
        for(int x = 0; x < width; x++)
        {
            GridPane.setConstraints(tileset,x,y);
        }
    }


    root.getChildren().add(tileset);

【问题讨论】:

  • 在循环中创建多个ImageViews,每个都使用相同的Image
  • 我在这样做时遇到了困难,您能否就循环需要包含什么内容来生成多个 ImageView 提出建议? ImageView 数据类型是否需要转换为数组? (另外,谢谢你的提示,至少我现在有地方开始)

标签: java javafx imageview tile gridpane


【解决方案1】:

GridPane.setConstraints(tileset,x,y) 方法不会将子节点添加到网格窗格,它只是设置子节点的索引。为了将其添加到网格窗格中,您应该在每次循环中使用新的 ImageView 调用 root.getChildren().add(tileset)

Image image = new Image("tileset.png");

Rectangle2D viewport1 = new Rectangle2D(0,16,16,16); //(selected pixels)
int length = 40, width= 40;  // declare size of array (print 40x40)

// loop through grid, fill every tile with image 'image'. 
// currently only fills position (40,40) with the image. 
for(int y = 0; y < length; y++)
{
    for(int x = 0; x < width; x++)
    {
        ImageView tileset = new ImageView(image);
        tileset.setViewport(viewport1);

        GridPane.setConstraints(tileset,x,y);
        root.getChildren().add(tileset);
    }
}

【讨论】:

    猜你喜欢
    • 2022-06-11
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多