java - 在javaFX中的gridPane单元格内随机显示圆圈
问题描述
我正在创建一个应用程序,它在 gridPane 的每个单元格中随机显示(不同颜色的)圆圈。
我想要做的是创建一个“随机播放”按钮,该按钮在 gridPane 中随机更改每个圆圈的位置。但是,我一直遇到一堆问题。
这是我到目前为止所拥有的。我的两个类(没有添加 XML 文件):
控制器类
public class viewController {
//My two Variables, a gridPane and a button
@FXML
private GridPane matrix;
@FXML
private Button shuffleBut;
//my eventHandler event that should (1) add circles to all the cells, and
(2) shuffle them amongst the cells in the gridPane.
void shuffle(ActionEvent e) {
Random r = new Random ();
int rowShuffle = r.next((4-0)+1);
int colShuffle = r.next((4-0)+1);
Circle newCircle = new Circle ();
matrix.add(newCircle, rowShuffle, colShuffle );
}
主班
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// just load fxml file and display it in the stage:
Parent root = FXMLLoader.Load(getClass().getResource("mainUI.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
// main method to support non-JavaFX-aware environments:
public static void main(String[] args) {
// starts the FX toolkit, instantiates this class,
// and calls start(...) on the FX Application thread:
launch(args);
}
解决方案
这是一个示例,演示如何Circles
在GridPane
. 如果将 添加Circles
到,则可以从ArrayList
中删除。然后你就可以洗牌了。最后,您可以将打乱后的列表添加回. Circles
GridPane
List
GridPane
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.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication314 extends Application
{
Random random = new Random();
int numberOfRows = 25;
int numberOfColumns = 25;
@Override
public void start(Stage primaryStage)
{
List<Circle> circles = new ArrayList();
for (int i = 0; i < numberOfColumns * numberOfRows; i++) {
circles.add(new Circle(10, getRandomColor()));
}
GridPane gridPane = new GridPane();
addCirclesToGridPane(gridPane, circles);
gridPane.setPadding(new Insets(20, 20, 20, 20));
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction((ActionEvent event) -> {
Collections.shuffle(circles);//Shuffle the List of Circles.
for(int i = 0; i < numberOfColumns * numberOfRows; i++)
{
Circle c = circles.get(i);
GridPane.setColumnIndex(c, i % numberOfColumns);
GridPane.setRowIndex(c, i / numberOfColumns);
}
});
VBox vBox = new VBox(gridPane, new StackPane(btn));
vBox.setMaxSize(Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
StackPane root = new StackPane(vBox);
root.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
Scene scene = new Scene(root);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
public void addCirclesToGridPane(GridPane gridPane, List<Circle> circles)
{
for (int i = 0; i < numberOfColumns * numberOfRows; i++) {
gridPane.add(circles.get(i), i % numberOfColumns, i / numberOfColumns);
}
}
public Color getRandomColor()
{
int r = random.nextInt(255);
int g = random.nextInt(255);
int b = random.nextInt(255);
return Color.rgb(r, g, b);
}
}
推荐阅读
- python - 预处理以使用 python opencv 查找两个图像之间的差异
- docker - 几乎`docker pull`命令挂起-重试(5分钟后)-完成
- java - 将一列值转换为键,将另一列值转换为值
- solidity - 我收到了一个像“TypeError:”block.blockhash()“已被弃用,取而代之“blockhash()”之类的错误
- docker - Grails Gradle Docker 插件 bmuschko.github.io/gradle-docker-plugin/ throws 无法设置未知属性“env”
- jquery - 无法将行添加到 jQuery 数据表
- laravel - Laravel Nova - 表单发布前的确认对话框
- sql-server - 在 SSRS 报告中隐藏空值
- docker - docker jupyter notebook 地址从不响应/超时
- kubernetes - 无法更改类型:要键入的 ClusterIP:kubernetes 中 kube-dns 的 LoadBalancer