简体   繁体   中英

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.

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.

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

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...)

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).

更严格的答案是正确的,此外,我认为您可以看看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;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM