簡體   English   中英

在javaFX中的gridPane單元內隨機顯示圓圈

[英]randomly displaying circles within gridPane cells in javaFX

我正在創建一個應用程序,該應用程序在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); 
}

這是演示如何在GridPane隨機播放CirclesGridPane 如果將Circles添加到ArrayList ,則可以從GridPane刪除Circles 然后,您可以隨機播放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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM