title: Calling a method from JFrame from another class
body: <p>I am currently making a terrain generator, everything works fine in one class but I am going to be expanding my application.</p>
  
  <p>Currently I have a JFrame class which holds everything, generating the terrain, painting the terrain, finding locations etc.</p>
  
  <p>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.</p>
  
  <pre><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
  }
  
  
  }
  }
  </code></pre>
  
  <p>That works fine in its own class but when I create a class for example:</p>
  
  <pre><code>public class TerrainGenerator 
  {
  Simulator sim = new Simulator();
  }
  </code></pre>
  
  <p>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 </p>
  
  <p>For example I want to be able to call a method from the 'TerrainGenerator' class and call i.e. terrainGenerator.generateTerrain</p>