[solved] while loop keeps going too long
I'm trying to make a Game of Life program but something isn't working right. I have a play/pause button but after it is pressed the main loop keeps going for several iterations before it stops. The button's text changes which shows the paused variable has already changed though. I can't figure out why it keeps going like that.
The code, I tried to take out all the non relevant bits to make it easier to read:
(the while loop is the one in runGame() that says while(!paused), in the real code heavyLifting() is the bit that changes the cells from one iteration to the next)
Code :
package Games;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Checking
{
private JFrame frame;
private LPanel canvas;
private int elapsedtime;
private int timepertick;
private long lasttime;
private boolean paused;
private int iteration;
public Checking()
{
//constructor
elapsedtime = 0;
timepertick = 16; //milliseconds
iteration = 0;
lasttime = 0;
paused = true; //start out paused
createAndShowGUI();
}
private void createAndShowGUI()
{
frame = new JFrame("just checking");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas = new LPanel(this);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
}
private long getElapsedTime()
{
long thistime = System.currentTimeMillis();
long time = thistime - lasttime;
lasttime = thistime;
return time;
}
private void runGame()
{
while(true)
{
while(!paused)
{
elapsedtime = elapsedtime + (int) getElapsedTime();
while(elapsedtime>timepertick)
{
heavyLifting();
iteration++;
elapsedtime = elapsedtime - timepertick;
canvas.repaint();
}
}
}
}
private void heavyLifting()
{
try
{
Thread.sleep(100);
}
catch(InterruptedException ie)
{
System.out.println("bleh");
}
}
public int getIteration()
{
return iteration;
}
public void togglePause()
{
if(!paused)
{
paused = true;
}
else
{
lasttime = System.currentTimeMillis();;
paused = false;
}
}
public boolean isPaused()
{
return paused;
}
public static void main(String[] args)
{
Checking c = new Checking();
c.runGame();
}
}
class LPanel extends JPanel
{
private Checking ref;
private final Color backgroundcolour = Color.BLACK;
private final Color textcolour = Color.YELLOW;
private final int leftoffset = 200;
private final int topoffset = 25;
private Rectangle2D playbutton;
public LPanel(Checking reference)
{
//constructor
ref = reference;
setBackground(Color.BLACK);
playbutton = new Rectangle2D.Double(); //just in case
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
receiveClick( e.getX(), e.getY() );
}
});
}
private void receiveClick(int x, int y)
{
if( onButton(x,y) )
{
ref.togglePause();
}
}
private boolean onButton(int x, int y)
{
return playbutton.contains(x,y);
}
private void drawSidebar(Graphics2D g2)
{
//draw the "button"
int width = 150;
int height = 75;
int x = 25;
int y = topoffset;
g2.setStroke( new BasicStroke(5.0F) );
g2.setColor(textcolour);
playbutton = new Rectangle2D.Double(x,y,width,height);
g2.draw( playbutton );
String text;
if(ref.isPaused())
{
text = "Play";
}
else
{
text = "Pause";
}
g2.setFont( new Font("Arial", Font.PLAIN, 20) );
FontMetrics fm = g2.getFontMetrics();
int h = fm.getHeight();
int w = fm.stringWidth(text);
x = (leftoffset/2) - (w/2);
y = y+(height/2)+7;
g2.drawString(text,x,y);
text = "Iteration: " + game.getIteration();
w = fm.stringWidth(text);
x = (leftoffset/2) - (w/2);
y = y + height + h;
g2.drawString(text,x,y);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawSidebar(g2);
}
public Dimension getPreferredSize()
{
return new Dimension(800,600);
}
}
Re: while loop keeps going too long
First, I'd recommend looking into using a SwingTimer to do your tasks. All those nested loops are could be removed with the timer, the third one in looks to be the culprit (will keep looping until complete when you pause, as the pause stops the outer loop)
Re: while loop keeps going too long
Ah, so obvious now that you pointed it out, thanks.