patternjavaMinor
Game Of Life optimization in Java
Viewed 0 times
lifeoptimizationgamejava
Problem
I've been working on a version of Conway's Game of Life for about a week now. It has loads of features like drawing onto the screen and saving/loading images. The problem is that ever since I added mouse support, it seems to start running pretty slowly. I don't know if it is due to the mouse or if it's something else. I'm a pretty newbie coder so I'd really like some help in optimizing this.
Just noting: I didn't include any imports due to the character limit.
Main Class
```
public class MainGame extends JFrame{
static int size = 721;
static JFrame frame = new JFrame("Game of Life");
static Life life = new Life(size);
Random r = new Random();
JLabel lblCellCount = new JLabel("Cell Count");
JLabel lblGeneration = new JLabel("Generation");
JSlider slider = new JSlider();
JButton play = new JButton("Pause");
JLabel lblSpeed = new JLabel("Speed: (10) ups");
public SaveLoad sl = new SaveLoad();
String[] patterns = sl.loadImages(new File(".\\Patterns\\"));
final JFileChooser fc = new JFileChooser();
final DefaultComboBoxModel model;
final JComboBox cmboPatterns;
Point start;
public void runGameLoop(){
life.running = true;
Thread loop = new Thread() {
public void run(){
gameLoop();
}
};
loop.start();
}
public void gameLoop(){
System.out.println("Started");
final int MAX_UPDATES_BEFORE_RENDER = 5;
double lastUpdateTime = System.nanoTime();
double lastRenderTime = System.nanoTime();
int lastSecondTime = (int) (lastUpdateTime / 1000000000);
while(life.running){
final double GAME_HERTZ = (double) slider.getValue();
final double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;
final double TARGET_FPS = (int) GAME_HERTZ;
final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;
double now = System.nanoTime();
Just noting: I didn't include any imports due to the character limit.
Main Class
```
public class MainGame extends JFrame{
static int size = 721;
static JFrame frame = new JFrame("Game of Life");
static Life life = new Life(size);
Random r = new Random();
JLabel lblCellCount = new JLabel("Cell Count");
JLabel lblGeneration = new JLabel("Generation");
JSlider slider = new JSlider();
JButton play = new JButton("Pause");
JLabel lblSpeed = new JLabel("Speed: (10) ups");
public SaveLoad sl = new SaveLoad();
String[] patterns = sl.loadImages(new File(".\\Patterns\\"));
final JFileChooser fc = new JFileChooser();
final DefaultComboBoxModel model;
final JComboBox cmboPatterns;
Point start;
public void runGameLoop(){
life.running = true;
Thread loop = new Thread() {
public void run(){
gameLoop();
}
};
loop.start();
}
public void gameLoop(){
System.out.println("Started");
final int MAX_UPDATES_BEFORE_RENDER = 5;
double lastUpdateTime = System.nanoTime();
double lastRenderTime = System.nanoTime();
int lastSecondTime = (int) (lastUpdateTime / 1000000000);
while(life.running){
final double GAME_HERTZ = (double) slider.getValue();
final double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;
final double TARGET_FPS = (int) GAME_HERTZ;
final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;
double now = System.nanoTime();
Solution
As @ChrisW mentioned: if you comment out the mouse support code, does it run quickly again? If this is the case, then changing from using a MouseMotionListener to a simple MouseListener, and check for a click instead of a drag.
Another thing that might help is if you move complicated code out of the body of the listener method. Just making a separate method to do the task and having a call to this method will make the code easier to read, but if you are still seeing problems with the code running slowly you may want to have the listener method spawn a thread to do the updating.
Another possibility is to have two modes of running the simulation. Add a pause button which stops the generations and enables the mouse listener. After the user makes changes, she can press a resume button which disables the mouse listener. This makes the logic easier as well, since you don't have to worry about cells changing state while you are trying to calculate the next generation.
Another thing that might help is if you move complicated code out of the body of the listener method. Just making a separate method to do the task and having a call to this method will make the code easier to read, but if you are still seeing problems with the code running slowly you may want to have the listener method spawn a thread to do the updating.
Another possibility is to have two modes of running the simulation. Add a pause button which stops the generations and enables the mouse listener. After the user makes changes, she can press a resume button which disables the mouse listener. This makes the logic easier as well, since you don't have to worry about cells changing state while you are trying to calculate the next generation.
Context
StackExchange Code Review Q#40008, answer score: 4
Revisions (0)
No revisions yet.