2014-12-07 111 views
0

我正在嘗試使用兩個for循環來自動將ImageView節點添加到每個位置。當使用for循環時,我收到一個錯誤。當我僅用一條語句添加ImageView節點來註釋for循環代碼時,代碼似乎可以工作,您可以使用for循環來填充GridPane?如果是這樣我做錯了什麼?如果沒有什麼可以用作解決方案?JavaFX - 在GridPane中使用for循環?

我的班級:

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.GridPane; 
import javafx.stage.Stage; 

public class GridCreation extends Application { 
@Override 
public void start(Stage gameStage) throws Exception { 
    GridPane grid = new GridPane(); 
    Image backOfCardsImg = new Image("images/naruto_shipuden_logo.png"); 
    ImageView backOfCards = new ImageView(backOfCardsImg); 
     for (int i = 0; i < 4; i++) { 
      for (int j = 0; j < 4; j++) { 
       grid.add(backOfCards, i, j); 

      } 
     } 

    //grid.add(backOfCards, 1,1); 
    Scene scene = new Scene(grid); 
    gameStage.setTitle("MemoryGame"); 
    gameStage.setScene(scene); 
    gameStage.show(); 
} 

public static void main(String[] args) { 
    launch(args); 
} 
} 

編輯:爲代碼:

grid.add(backOfCards, i, j); 

我改變的代碼行到

grid.add(new ImageView(backOfCardImg), i, j); 

這似乎解決了這個問題,但任何人都可以向我解釋爲什麼第一個選項不起作用?

+0

'ImageView'是一個'Node',你不能重用它,否則你會得到這個異常:'Children:duplicate children added'。第二種解決方案的工作原理是爲網格上的每個單元格創建一個新的圖像視圖。 – 2014-12-07 02:01:09

+0

我想重新使用圖像並將其添加到每個gridPane是他們的任何解決方案嗎? – leftlopez 2014-12-07 03:26:52

+0

由於FX節點只能在父節點上進行,並且只允許向該父節點添加一次,因此每次要在網格中掃描該圖像時都必須創建一個新的ImageView()。它不會複製eimage本身,只複製視圖的JavaFX容器/控件。 – 2014-12-07 10:34:54

回答

0

這可能是fx內存遊戲可用的開始。它使用ImageView自己的擴展來完成轉彎和焦點動畫,並處理通用的背面圖像。它唯一的圖形,沒有遊戲邏輯。

public class MemoryGame extends Application { 
    final int rows = 4; 
    final int columns = 4; 
    CardView views[][] = new CardView[rows][]; 

    public static class CardView extends ImageView { 
     static final double scale = 0.95; 
     static DropShadow shadowhoover = new DropShadow(5, 4, 4, Color.rgb(50, 60, 50)); 
     static DropShadow shadowdown = new DropShadow(2, 2, 2, Color.rgb(50, 60, 50)); 
     static Image backside = null; 
     public static void setbackside(Image image) { backside = image; } 

     public CardView(Image image) { 
      super(backside); 
      setRotationAxis(new Point3D(0, 200,0)); 
      setScaleX(scale); 
      setScaleY(scale); 
      setEffect(shadowdown); 
      setOnMouseEntered(m -> { 
       setEffect(shadowhoover); 
       setScaleX(scale*1.01); 
       setScaleY(scale*1.01); 
      }); 
      setOnMouseExited(m -> { 
       setEffect(shadowdown); 
       setScaleX(scale); 
       setScaleY(scale); 
      }); 
      setOnMouseClicked(m -> {  
       RotateTransition r1 = new RotateTransition(Duration.millis(300), this); 
       r1.setByAngle(90); 
       r1.setOnFinished(e -> setImage(image)); 
       RotateTransition r2 = new RotateTransition(Duration.millis(300), this); 
       r2.setByAngle(-90); 

       RotateTransition r3 = new RotateTransition(Duration.millis(300), this); 
       r3.setByAngle(90); 
       r3.setOnFinished(e -> setImage(backside)); 
       RotateTransition r4 = new RotateTransition(Duration.millis(300), this); 
       r4.setByAngle(-90); 

       new SequentialTransition(r1, r2, new PauseTransition(Duration.millis(1000)), r3, r4).play(); 
      }); 
     } 
    } 

    @Override 
    public void start(Stage gameStage) throws Exception { 
     GridPane grid = new GridPane(); 
     grid.setBackground(new Background(new BackgroundFill(Color.rgb(140, 200, 140), new CornerRadii(0), new Insets(0)))); 
     grid.setHgap(5); 
     grid.setVgap(5); 
     Image back = new Image(MemoryGame.class.getResource("card-back.png").toExternalForm(), 140, 200, true, true); 
     Image front = new Image(MemoryGame.class.getResource("card-1.png").toExternalForm(), 140, 200, true, true); 
     CardView.setbackside(back); 
     for (int r = 0; r < rows; r++) { 
      views[r] = new CardView[columns]; 
      for (int c = 0; c < columns; c++) { 
       CardView view = new CardView(front); // different front images of course... 
       views[r][c] = view; 

       HBox box = new HBox(5); 
       box.getChildren().add(views[r][c]); 
       grid.add(box, c, r); 

      } 
     } 

     //grid.add(backOfCards, 1,1); 
     Scene scene = new Scene(grid); 
     gameStage.setTitle("MemoryGame"); 
     gameStage.setScene(scene); 
     gameStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
}