2016-12-12 7 views
0

以下のコードでは、(変数 'イメージ'に格納された)単一のイメージを複数回印刷しようとしています。どのように私はこれを行うに行くつもりの任意の提案?どんな情報でも役立ちます。JavaFX - imageviewを使用してイメージを複数回印刷する方法はありますか?

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); 
+0

私はそうすることの困難を抱えている、ループ内で同じ 'Image' –

+0

を使用して、それぞれ1つずつ、複数の' ImageView'sを作成し、あなたはループを生成するために含まれている必要が何にとしての提案を与えることができます複数のImageViewの? ImageViewデータ型を配列に変換する必要がありますか? (また、チップのおかげで、少なくとも私はこの時点で開始する場所があります) – asdf

答えて

2

gridpaneに子を追加しませんGridPane.setConstraints(tileset,x,y)方法は、それだけで子ノードのインデックスを設定します。グリッドパネルに追加するには、ループ内で毎回root.getChildren().add(tileset)を呼び出し、新しいImageViewを付けてください。

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); 
    } 
} 
関連する問題