在javaFX中的gridPane单元格中随机显示圆圈 [英] randomly displaying circles within gridPane cells in javaFX

查看:298
本文介绍了在javaFX中的gridPane单元格中随机显示圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建应用程序,它在gridPane的每个单元格中随机显示圆圈(不同颜色)。

I am creating am application that displays circles (of different colors) randomly within each cell of a gridPane.

我想要做的是创建一个随机按钮,在gridPane中随机改变每个圆圈的位置。但是,我一直遇到问题。

What i want to do is create a "shuffle" button that changes the position of each circle randomly within the gridPane. However, I keep running into a slurry of problems.

这是我到目前为止的情况。我的两个类(尚未添加XML文件):

Here is what i have so far. My two classes (have not added XML file):

控制器类

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 中随机播放 Circles 。如果将 Circles 添加到 ArrayList ,则可以删除 Circles GridPane 的$ c>。然后你可以随机播放 List 。最后,您可以将混洗列表添加回 GridPane

Here is an example that demos how to shuffle Circles around in a GridPane. If you add the Circles to an ArrayList, you can remove the Circles from the GridPane. Then you can shuffle the List. Finally, you can add the shuffled list back to the 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);
    }
}

这篇关于在javaFX中的gridPane单元格中随机显示圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆