我正在尝试获取要在5x5网格上显示的图像列表。我不知道是什么问题。
这是作业说明: 作业:
创建图像BINGO。以练习14.1(第586页)为基础:编写一个在Bingo卡上随机显示图像的程序。这应该是一个5X5的网格,顶部带有字母B I N G O。
//package
package javafxapplication2;
//import
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
/**
*
* @author travis.dutton
*/
public class JavaFXApplication2 extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(5);
pane.setVgap(5);
ImageView flags[];
flags = new ImageView[25];
flags[0] = new ImageView("images/1.png");
flags[1] = new ImageView("images/2.jpg");
flags[2] = new ImageView("images/3.gif");
flags[3] = new ImageView("images/4.gif");
flags[4] = new ImageView("images/5.jpg");
flags[5] = new ImageView("images/6.png");
flags[6] = new ImageView("images/7.gif");
flags[7] = new ImageView("images/8.jpg");
flags[8] = new ImageView("images/9.png");
flags[9] = new ImageView("images/10.png");
flags[10] = new ImageView("images/11.jpg");
flags[11] = new ImageView("images/12.png");
flags[12] = new ImageView("images/13.png");
flags[13] = new ImageView("images/14.png");
flags[14] = new ImageView("images/15.png");
flags[15] = new ImageView("images/16.png");
flags[16] = new ImageView("images/17.gif");
flags[17] = new ImageView("images/18.gif");
flags[18] = new ImageView("images/19.png");
flags[19] = new ImageView("images/20.jpg");
flags[20] = new ImageView("images/21.png");
flags[21] = new ImageView("images/22.png");
flags[22] = new ImageView("images/23.png");
// flags[23] = new ImageView("images/24.gif");
// flags[24] = new ImageView("images/25.png");
// pane.add(flags[0], 0, 0);
// pane.add(imageView2, 1, 0);
// pane.add(imageView3, 0, 1);
// pane.add(imageView4, 1, 1);
// for(int i = 0; i < flags.length; i++){
//
// GridPane.setRowIndex(flags[i],1);
// GridPane.setColumnIndex(flags[i],i);
// pane.getChildren().add(flags[i]);
// }
for(int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++) {
ImageView image = new ImageView();
image.setColumnIndex(1);
image.getChildren(String.valueOf(flags));
pane.add(image, i, j);
}
}
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("Exercise14_01"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);//launch javafx
}//end main
}//end of class
答案 0 :(得分:0)
如果要将随机图像放置到GridPane
,例如:
您必须执行以下操作:
final ArrayList<ImageView> flags = new ArrayList<>();
flags.add(new ImageView("images/1.png"));
flags.add(new ImageView("images/2.png"));
flags.add(new ImageView("images/3.png"));
// add flags
for (int columnIndex = 0; columnIndex < 5; i++) {
for (int rowIndex = 0; rowIndex < 5; j++) {
if(isNotBingoCell(columnIndex, rowIndex)) { //check that cell is not BINGO cell
final int imageIndex = Objects.equals(flags.size(), 1)
? 0
: ThreadLocalRandom.current().nextInt(0, flags.size() - 1);
pane.add(flags.get(imageIndex), columnIndex, rowIndex);
flags.remove(imageIndex);
flags.trimToSize();
}
}
}
,isNotBingoCell(...)
实施:
private boolean isNotBingoCell(int columnIndex, int rowIndex) {
return !Objects.equals(columnIndex, rowIndex);
}
答案 1 :(得分:0)
ImageView不提供getChildren
方法。 (我不知道JavaFX类提供了一个使用String
参数btw的类)。 ImageView
是节点,应直接使用GridPane
方法放置在GridPane.add(Node, int, int)
中。
您可以使用Collections.shuffle
来生成预设:
String[] images = new String[] {
"images/1.png",
"images/2.jpg",
...
"images/25.png"
};
// using a list with indices here to keep the original index order intact.
// (You could use a list of String and shuffle it otherwise)
List<Integer> permutation = IntStream.range(0, images.length).boxed().collect(Collectors.toCollection(ArrayList::new));
Collections.shuffle(permutation);
for(int i = 0, index = 0; i < 5; i++){
for (int j = 1; j <= 5; j++) {
ImageView image = new ImageView(images[permutation.get(index)]);
pane.add(image, i, j);
index++;
}
}
// bingo accross the top (not sure if this is what was asked for)
pane.add(new Label("Bingo"), 0, 0, 5, 1);