Calling a method from JFrame from another class

Discussion in 'Java Forums' started by AssPunk, Apr 9, 2013.

  1. AssPunk

    AssPunk New Member

    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.\[code\]public class Simulator extends Applet{//fieldspublic 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\]That works fine in its own class but when I create a class for example:\[code\]public class TerrainGenerator {Simulator sim = new Simulator();}\[/code\]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 i.e. terrainGenerator.generateTerrain
     

Share This Page