感谢您花时间帮助我。我正在制作一个游戏板,我希望将图像的 ArrayList 添加到 hbox 中,然后添加到您认为更好的 gridpane 或 tilepane 中。我认为是瓷砖。我正在尝试使用循环遍历 arraylist 来制作图像视图。但是你可以看到我没有得到任何结果。请帮助再次感谢您的帮助。我试图开始工作的方法是 addGridPane。初级阶段也有我的主菜单,类型为边框窗格。我的网格窗格在边框窗格内居中。
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.media.AudioClip;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.media.AudioClip;
public class Runner extends Application {
static ArrayList<PlayerMove> moves = new ArrayList<PlayerMove>();
static ArrayList<Image> tileImages = new ArrayList<>();
static HBox currentMoves = getHBoxCurrentMoves("PLAYER MOVES:", moves, 0, 0);
static VBox playerBox = getVBoxTextList();//red
static VBox playerBox1 = getVBoxTextList1();//black
static HBox imageTiles = setTileImages(tileImages);
public static void main(String[] args) {
launch(args);
}
public static VBox getVBoxTextList2(String TitleName) {
VBox vbox = new VBox();
vbox.setPadding(new Insets(50, 50, 50, 100));
vbox.setSpacing(8);
vbox.setPrefHeight(100);
vbox.setPrefWidth(100);
Text title = new Text(TitleName);
title.setFill(Color.rgb(178, 0, 0));
title.setFont(Font.font("Arial", FontWeight.BOLD, 20));
vbox.getChildren().add(title);
vbox.setStyle("-fx-border-color: #b20000;" + "-fx-background-color: black;");
return vbox;
}
public static VBox getVBoxTextList() {
VBox vbox = new VBox();
vbox.setPadding(new Insets(10));
vbox.setSpacing(8);
vbox.setPrefHeight(100);
//vbox.setStyle("-fx-border-color: #b20000;" + "-fx-background-color: #8b5a2b;");
//vbox.setStyle("-fx-background-image: url('redplayer.png')");
vbox.setStyle("-fx-background-image: url('redplayer.png')");
Image image = new Image("redplayer.png");
ImageView redplayer = new ImageView();
redplayer.setImage(image);
vbox.getChildren().addAll(redplayer);
vbox.setStyle("-fx-background-color: red;");
return vbox;
}
public static VBox getVBoxTextList1() {
VBox vbox = new VBox();
vbox.setPadding(new Insets(10));
vbox.setSpacing(8);
vbox.setPrefHeight(100);
//vbox.setStyle("-fx-border-color: #b20000;" + "-fx-background-color: black;");
//vbox.setStyle("-fx-background-image: url('blackplayer.png')");
Image image = new Image("blackplayer.png");
ImageView blackplayer = new ImageView();
blackplayer.setImage(image);
vbox.getChildren().addAll(blackplayer);
vbox.setStyle("-fx-background-color: black;");
return vbox;
}
public static HBox setTileImages(ArrayList<Image> myImages){
HBox hbox = new HBox();
ArrayList<Image> temp = myImages;
for(int i = 0; i < temp.size(); i++){
ImageView boardImage = new ImageView(temp.get(i));
}
return hbox;
}
public GridPane addGridPane(ArrayList<PlayerMove> myMoves, ArrayList<Image> myImages) {
GridPane grid = new GridPane();
grid.setGridLinesVisible(true);
grid.setStyle("-fx-background-color: pink;");
grid.setHgap(20);
grid.setVgap(20);
grid.setPadding(new Insets(30, 300, 30, 300));
HBox hbox = new HBox();
ArrayList<PlayerMove> temp = myMoves;
int col = 0;
int row = 0;
//I would like to have the arraylist of images added in to an hbox
//then into a gridpane making it get the string of everyimage to compare
ArrayList<Image> tempImage = myImages;
for(int m = 0; m < tempImage.size(); m++){
Image image = new Image(tempImage.get(m).toString());
ImageView picture = new ImageView();
picture.setFitHeight(145);
picture.setFitWidth(145);
picture.setImage(image);
grid.add(picture,col,row);
hbox.getChildren().add(picture);
}
return grid;
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Niya Game Board");
BorderPane border = new BorderPane();
border.setStyle("-fx-background-color: #999C9E;");
GridPane layout = addGridPane(moves, tileImages);
//ArrayList<Image> tileImages = new ArrayList<>();
tileImages.add(new Image ("bird&cloud.jpg"));
tileImages.add(new Image ("bird&computer.jpg"));
tileImages.add(new Image ("bird&dragon.jpg"));
tileImages.add(new Image ("bird&flower.jpg"));
tileImages.add(new Image ("bird&leaf.jpg"));
tileImages.add(new Image ("bird&sun.jpg"));
tileImages.add(new Image ("bird&tiger.jpg"));
tileImages.add(new Image ("cloud&computer.jpg"));
tileImages.add(new Image ("cloud&dragon.jpg"));
tileImages.add(new Image ("cloud&flower.jpg"));
tileImages.add(new Image ("cloud&leaf.jpg"));
tileImages.add(new Image ("cloud&sun.jpg"));
tileImages.add(new Image ("cloud&tiger.jpg"));
tileImages.add(new Image ("computer&dragon.jpg"));
tileImages.add(new Image ("computer&flower.jpg"));
tileImages.add(new Image ("computer&leaf.jpg"));
tileImages.add(new Image ("computer&sun.jpg"));
tileImages.add(new Image ("computer&tiger.jpg"));
tileImages.add(new Image ("dragon&flower.jpg"));
tileImages.add(new Image ("dragon&leaf.jpg"));
tileImages.add(new Image ("dragon&sun.jpg"));
tileImages.add(new Image ("dragon&tiger.jpg"));
tileImages.add(new Image ("flower&leaf.jpg"));
tileImages.add(new Image ("flower&sun.jpg"));
tileImages.add(new Image ("flower&tiger.jpg"));
tileImages.add(new Image ("leaf&sun.jpg"));
tileImages.add(new Image ("leaf&tiger.jpg"));
tileImages.add(new Image ("sun&tiger.jpg"));
ArrayList<HBox> hboxList = new ArrayList<>();
VBox menu = getVBoxTextList2("Welcome To The Niya Game Board");
StackPane border2 = new StackPane();
BorderPane border1 = new BorderPane();
border1.setTop(menu);
border2.setStyle("-fx-background-color: black;");
border2.setPrefSize(800, 800);
Image img = new Image("main.jpg");
ImageView background = new ImageView(img);
border1.setCenter(background);
HBox hbox = new HBox();
hbox.setPadding(new Insets(20, 55, 20, 55));
hbox.setSpacing(10);
hbox.setStyle("-fx-background-color: black;");
Button button1 = new Button("PLAY GAME");
button1.setStyle("-fx-font: 22 Arial; FontWeight.BOLD; -fx-base: #b6e7c9; -fx-text-fill: white;");
button1.setPrefSize(200, 70);
button1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
Scene sc = new Scene(border);
primaryStage.setScene(sc);
primaryStage.show();
}
});
Button button2 = new Button("SAVED GAMES");
button2.setStyle("-fx-font: 22 Arial; FontWeight.BOLD; -fx-base: #b6e7c9; -fx-text-fill: white;");
button2.setPrefSize(200, 70);
button2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
System.out.println("Loading Saved Games...");
}
});
button2.setOnAction(e -> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
File file = fileChooser.showOpenDialog(primaryStage);
if (file != null) {
Path path = Paths.get(file.toURI());
Charset charset = Charset.forName("US-ASCII");
List<String> lines = new ArrayList<String>();
try (BufferedReader reader = Files.newBufferedReader(path, charset)) {
lines = Files.readAllLines(path, charset);
} catch (IOException x) {
return;
}
for (String s : lines) {
System.out.println(s);
}
}
});
hbox.getChildren().addAll(button1, button2);
border1.setLeft(hbox);
border.setCenter(layout);
border.setLeft(playerBox);
border.setRight(playerBox1);
border.setBottom(currentMoves);
Scene sc1 = new Scene(border1);
primaryStage.setScene(sc1);
primaryStage.show();
}
}