[英]Calling a method from JFrame from another class
I am currently making a terrain generator, everything works fine in one class but I am going to be expanding my application. 我目前正在制作地形生成器,在一堂课上一切正常,但是我将扩展我的应用程序。
Currently I have a JFrame class which holds everything, generating the terrain, painting the terrain, finding locations etc. 目前,我有一个JFrame类,其中包含所有内容,生成地形,绘制地形,查找位置等。
I want to add another class that will generate the terrain but when I create this class I need to access fields from the main JFrame class and when I do I get a stack overflow error - here is my code. 我想添加另一个将生成地形的类,但是当我创建此类时,我需要访问JFrame主类中的字段,并且当我收到堆栈溢出错误时,这是我的代码。
public class Simulator extends Applet
{
//fields
public Simulator()
{
grid = new int[100][100];
inhabGrid = new boolean[grid.length][grid.length];
gridSize = grid.length - 1;
dist = grid.length;
TerrainGenerator gen = new TerrainGenerator();
setSize(dist,dist);
seedGrid();
findInhabLocation();
printGridToConsole();
}
public void paint(Graphics g)
{
//panting the grid
}
public void seedGrid()
{
//seeding
}
public boolean generateTerrain(int x1,int y1, int x2, int y2)
{
//terrain generator
}
public boolean mouseUp(Event evt, int x, int y)
{
seedGrid(); //Create a new map
findInhabLocation();
repaint();
printGridToConsole();
return true;
}
public boolean keyEvents(Event evt, int x, int y)
{
seedGrid(); //Create a new map
findInhabLocation();
repaint();
printGridToConsole();
return true;
}
public void findInhabLocation()
{
//find best inhabitant location
}
public int locateWater(int x, int y)
{
//finding closest water
}
public int locateJungle(int x, int y)
{
//finding closest jungle
}
}
}
That works fine in its own class but when I create a class for example: 在自己的类中可以正常工作,但是例如当我创建一个类时:
public class TerrainGenerator
{
Simulator sim = new Simulator();
}
I know this has something to do with the constructor and it's something silly I am doing, what would be the best way of splitting up this app into classes, for example terrain generator, inhabitants etc 我知道这与构造函数有关,并且我在做些愚蠢的事情,这是将这个应用拆分为类的最佳方法,例如地形生成器,居民等
For example I want to be able to call a method from the 'TerrainGenerator' class and call ie terrainGenerator.generateTerrain 例如,我希望能够从“ TerrainGenerator”类中调用一个方法,然后调用ieterrainGenerator.generateTerrain
Your TerrainGenerator
creates a Simulator
object and vice versa, hence you'll end up with infinitely many objects (but at some point the stack is full and a stack overflow exception is thrown instead...) TerrainGenerator
创建一个Simulator
对象,反之亦然,因此您最终将获得无限多个对象(但是在某些时候堆栈已满,并且抛出了堆栈溢出异常...)
Instead of creating a new Simulator
in your TerrainGenerator
, you should pass a reference to your current Simulator
(well, actually, that is not a great design either, but I'm not gonna confuse you with the problems of circular references). 而不是在
TerrainGenerator
中创建新的Simulator
,您应该传递对当前Simulator
的引用(嗯,实际上,这也不是一个好的设计,但是我不会将您与循环引用的问题混为一谈)。
更严格的答案是正确的,此外,我认为您可以看看MVC来帮助您组织课程。
Depending which should be the parent, you can pass in the instantiated class to the other, ie; 根据哪个应该是父类,您可以将实例化的类传递给另一个,即;
private final TerrainGenerator gen; //if you need to save this.
public Simulator(TerrainGenerator terrainGenerator)
{
this.gen = terrainGenerator;
....etc
}
public class TerrainGenerator
{
Simulator sim = new Simulator(this);
}
or 要么
private final TerrainGenerator gen; //if you need to save this.
public Simulator()
{
this.gen = new TerrainGenerator(this);
....etc
}
private final Simulator sim; //If you need to save it.
public class TerrainGenerator
{
public TerrainGenerator(Simulator simulator) {
this.sim = simulator;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.