Skip to content
Advertisement

Java predator-prey simulation with GUI can’t run simulation properly

I’m trying to add a GUI to the predator-prey simulation. It can allow users to choose which simulation(species involved) they want to do, set the simulation field size they want, and show the progress of the simulation and result.

I asked a question before, and I kinda figure it out what caused the problem.

But now I encounter another question:

How do I let the simulation buttons use the same object method which generate button use, but also don’t generate the field again?

As the code shown below,

(1)after I generate the field and press “Reset” button, it will generate the field again then reset simulation(show up a new field window with different data);

(2)after I generate the field and press “Next step” button, it will generate the field again then show next step simulation(show up a new field window and don’t inherit the initial data I generate);

(3)after I generate the field and press “long run step” button, it will generate the field again then show next hundred step simulation result(show up a new field window and don’t inherit the initial data I generate and don’t show the progress);

private class ButtonHandler implements ActionListener{//Generate field
    public void actionPerformed(ActionEvent e){
        String depthstr = depthtxt.getText();
        String widthstr = widthtxt.getText();
        int depth = Integer.parseInt(depthstr);
        int width = Integer.parseInt(widthstr);
        switch(version) {//Different specices simulation choosed by user
            case 0:
                Simulator set = new Simulator();
                set.Simulator(depth,width);
                break;
            case 1:
                SimulatorRF setRF = new SimulatorRF();
                setRF.SimulatorRF(depth,width);
                break;
            case 2:
                SimulatorRW setRW = new SimulatorRW();
                setRW.SimulatorRW(depth,width);
                break;
            ...
            case 25:
                SimulatorFWBH setFWBH = new SimulatorFWBH();
                setFWBH.SimulatorFWBH(depth,width);
                break;
        }  
        reset_butt.setEnabled(true);
        nextstep_butt.setEnabled(true);
        longstep_butt.setEnabled(true);
    }
}      
private class SimulateOption implements ActionListener{
    public void actionPerformed(ActionEvent e){
        switch(version) {
            case 0:
                Simulator set = new Simulator();
                set.Simulator(depth,width);
                if(e.getSource() == reset_butt){
                    set.reset();
                }
                else if(e.getSource() == nextstep_butt){
                    set.simulateOneStep();
                }
                else if(e.getSource() == longstep_butt){
                    set.runLongSimulation();
                }
                break;
            case 1:
                SimulatorRF setRF = new SimulatorRF();
                setRF.SimulatorRF(depth,width);
                if(e.getSource() == reset_butt){
                    setRF.reset();
                }
                else if(e.getSource() == nextstep_butt){
                    setRF.simulateOneStep();
                }
                else if(e.getSource() == longstep_butt){
                    setRF.runLongSimulation();
                }
                break;
            case 2:
                SimulatorRW setRW = new SimulatorRW();
                setRW.SimulatorRW(depth,width);
                if(e.getSource() == reset_butt){
                    setRW.reset();
                }
                else if(e.getSource() == nextstep_butt){
                    setRW.simulateOneStep();
                }
                else if(e.getSource() == longstep_butt){
                    setRW.runLongSimulation();
                }
                break;
            ...
            case 25:
                SimulatorFWBH setFWBH = new SimulatorFWBH();
                setFWBH.SimulatorFWBH(depth,width);
                if(e.getSource() == reset_butt){
                    setFWBH.reset();
                }
                else if(e.getSource() == nextstep_butt){
                    setFWBH.simulateOneStep();
                }
                else if(e.getSource() == longstep_butt){
                    setFWBH.runLongSimulation();
                }
                break;
        }  
        reset_butt.setEnabled(true);
        nextstep_butt.setEnabled(true);
        longstep_butt.setEnabled(true);
    }
 }      
}

Is there a way to fix my code to solve the problem?

I hope that

(1)after I click the “reset” button, it reset the field I generated (don’t show up a new field window)

(2)after I click the “next step” button, it simulate next step of the field I generated (inherit the initial data I generate and don’t show up a new field window)

(3)after I click the “next hundred step” button, it simulate next hundred step of the field I generated and show the process (inherit the initial data I generate and don’t show up a new field window)

Here are my full codes if you are intrested:

https://github.com/KasmaJC/predator-prey-simulation-with-GUI *There is a BlueJ project rar. at button

Advertisement

Answer

Well I figure it out myself again… I put all of my Classes into a package(name as PredatorPreySimulation), and change all simulator classes’ method into static method.

By doing so, I don’t need to create an instance of the class to call a static method, and I can let the simulation buttons use the same method which generate button use, but also don’t generate the field again.

In this case, I can use the code below:

import PredatorPreySimulation.Simulation;
import PredatorPreySimulation.SimulationRF;
...

and

public class ButtonHandler implements ActionListener{//Generate field
        public void actionPerformed(ActionEvent e){
            String depthstr = depthtxt.getText();
            String widthstr = widthtxt.getText();
            int depth = Integer.parseInt(depthstr);
            int width = Integer.parseInt(widthstr);
            switch(version) {//Different specices simulation choosed by user
                case 0:
                    Simulator.Simulator(depth,width);
                    break;
                case 1:
                    SimulatorRF.SimulatorRF(depth,width);
                    break;
                ...
                case 25:
                    SimulatorFWBH.SimulatorFWBH(depth,width);
                    break;
            }  
            reset_butt.setEnabled(true);
            nextstep_butt.setEnabled(true);
            longstep_butt.setEnabled(true);
        }
    }      
    private class SimulateOption implements ActionListener{
        public void actionPerformed(ActionEvent e){
            switch(version) {
                case 0:
                    if(e.getSource() == reset_butt){
                        Simulator.reset();
                    }
                    else if(e.getSource() == nextstep_butt){
                        Simulator.simulateOneStep();
                    }
                    else if(e.getSource() == longstep_butt){
                        Simulator.runLongSimulation();
                    }
                    break;
                case 1:
                    if(e.getSource() == reset_butt){
                        SimulatorRF.reset();
                    }
                    else if(e.getSource() == nextstep_butt){
                        SimulatorRF.simulateOneStep();
                    }
                    else if(e.getSource() == longstep_butt){
                        SimulatorRF.runLongSimulation();
                    }
                    break;
                ...
                case 25:
                    if(e.getSource() == reset_butt){
                        SimulatorFWBH.reset();
                    }
                    else if(e.getSource() == nextstep_butt){
                        SimulatorFWBH.simulateOneStep();
                    }
                    else if(e.getSource() == longstep_butt){
                        SimulatorFWBH.runLongSimulation();
                    }
                    break;
            }  
            reset_butt.setEnabled(true);
            nextstep_butt.setEnabled(true);
            longstep_butt.setEnabled(true);
        }
     }      
    }

I might be bad at expressing questions… I’ll try to improve my asking skill …

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement