访问创建当前类的类中的方法

huangapple 未分类评论50阅读模式
英文:

Accessing a method in the class that created the class you're currently in

问题

以下是翻译好的部分:

代码部分不要翻译,只返回翻译好的内容。

有一点困扰。我有两个类:一个 GUIBuilder 和一个模拟康威生命游戏(Conway's Game of Life)的类,使用 2D 数组。GUI 创建一个方格网格,网格的维度由用户选择,然后允许用户选择哪些方格是“存活”的,哪些是“死亡”的,然后他们指定模拟将运行多少代,并点击“开始模拟”按钮。方格网格将更新以反映新的边界,以及每一代哪些方格现在是“存活”的,哪些是“死亡”的。

最初,我设置了康威生命游戏类,以输出它为每一代创建的 2D 数组,但现在我想调用 GUI Builder 中的类来更新 GUI 方格网格,其中每个方格都是不同的颜色,以显示其状态,而不是这些输出。在 GUI 中按下一个按钮以调用 Conway 类,并为模拟传递规格,然后 Conway 类运行模拟,在每一代结束时暂停,以更新 GUI 以反映模拟的新状态。

每次在 GUI 构建器类中调用更新方法时,都会暂停以展示模拟的进展。

我在使用 JavaFX 创建 GUI。

这是“开始模拟”按钮事件的代码。它将调用 Conway 中的方法,传递所有起始宇宙的 2D 数组。

这是我希望从 Conway 类调用的更新 GUI 方法。

最后,这是 Conway 类中我想要更新 GUI 的地方。

如果需要更多帮助,请告诉我。

英文:

Having a bit of trouble here. So I have two classes: A GUIBuilder and a Class that simulates Conway's Game of Life using 2D arrays. The GUI creates a grid of squares to a user's chosen dimensions and then allows them to select which squares are "alive" and "dead" to start off, they then specify how many generations the simulation will run for and hit the start simulation button. The grid of squares will update to reflect the new bounds as well as which squares are now "living" and which are "dead" for each generation.

Originally I had set up Conway's game of life class to print out the 2D array it creates for each generation as an output but now I want to call a class in GUI Builder to update a GUI grid of squares, with each square being a different colour to show its status, in place of these print outputs. A button is pressed in the GUI to call the Conway class with the specifications for the simulation then the Conway class runs the simulation pausing at the end of each generation to update the GUI to reflect the new state of the simulation.

[![Class Interaction I'm Thinking Of][1]][1]

Each time the update method is called in the GUI builder class there is a pause to show off the progression of the simulation.

I'm using JavaFX to create the GUI as well

So here is the code for the Start Simulation button event. It'll call the method in Conway passing the 2D array of all the starting universe

startSimulationButton.setOnAction(new EventHandler<ActionEvent>()
	{
		public void handle(ActionEvent event)
		{
			if(validateTextBox(genTextField))
			{
				int[][] universe = convertTo2DArray(simulationPane);
				int generations = Integer.parseInt(genTextField.getText());
				ConwayLife simStart = new ConwayLife();
				simStart.getGeneration(universe, generations);
			}
			else
			{
				instructionLabel.setText("Didn't put numbers in ya silly billy");
			}
		}
	});

Here is the update GUI method I want to be called from the Conway class

private void updateSimulationPane(int[][] universe, int xUpdate, int yUpdate) throws InterruptedException
{
	clearSimulationGrid(simulationPane);
	int[] flatUniverse = Stream.of(universe).flatMapToInt(IntStream::of).toArray();
	for(int counter = 0; counter < (xUpdate*yUpdate); counter++)
	{
		Rectangle rect = new Rectangle(0, 0, squareDimensions, squareDimensions);
		if(flatUniverse
0
+
网站访问量
== 1) { rect.setFill(Color.DARKGREEN); } else { rect.setFill(Color.GREY); } simulationPane.add(rect, (counter) % xInput, (counter) / xInput); } double leftAnchor = (xWindow / 2) - ((squareDimensions * xInput) / 2); gridAnchorPane.setTopAnchor(simulationPane, 100d); gridAnchorPane.setLeftAnchor(simulationPane, leftAnchor); Thread.sleep(5000); }

and finally here is the point in the Conway class that I would like to update the GUI

	if(deadCells == i * j) //if there are no more alive cells
		{
			int[][] emptyArray = new int[0][0];
			/*
			//******Last Generation plus Empty set output*********
			System.out.println("Generation: " + counter);
			for(int elcounto = 0; elcounto < oldGeneration.length; elcounto++)
			{
				System.out.println(Arrays.toString(oldGeneration[elcounto]));
			}
			System.out.println("Generation: " + (counter + 1) + "\nSociety died out");
			System.out.println(Arrays.deepToString(emptyArray));
			//****************************************************
			*/
			
			//call updateSimulationPane here 
			
			return emptyArray; //return an empty set to show that the society died out
			
			
		}
		/*
		//******Generation Check Output*********
		System.out.println("Generation: " + counter);
		for(int elcounto = 0; elcounto < oldGeneration.length; elcounto++)
		{
			System.out.println(Arrays.toString(oldGeneration[elcounto]));
		}
		System.out.println();
		//**************************************
		*/
		
		//call updateSimulationPane here 
		
		oldGeneration = newGeneration; //make the new generation the old generation to show passing of a generation
		newGeneration = new int[oldGeneration.length + bounds][oldGeneration[0].length + bounds]; //em
	}
	return trim(oldGeneration);

The Conway side is pretty rough the now but so long as I can call back and update the GUI I'll be able to work out all the kinks on that end. Basically just looking to fix the class interaction. I hope that's enough information to understand what I'm trying to do.
[1]: https://i.stack.imgur.com/RoZBb.png

huangapple
  • 本文由 发表于 2020年4月8日 21:31:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/61101917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定