# Solved: Help with GUI for Java Game Of Life



## robobobo (Apr 27, 2008)

Hey guys,

I've been working on the Game Of Life in Java,

I've got all my algorithms working perfectly and the program runs and works fine, except for the GUI end of things.

We haven't actually worked with these before, but they way it worked was we were given code and told to write the rest of it, mainly all the algorithms which I've done without too much trouble.

I just can't get it to display properly, at the moment this is the code I have to paint 

```
public void paint(Graphics g) {
		
			for(int i = 0; i<board.length; i++){
				for(int k = 0; k<board[i].length; k++){
			if(board[i][k] == true){
				g.setColor(Color.green);
				g.drawRect(i, k, 10, 10);
				g.fillRect(i,k,10,10);			}
			else {
				g.setColor (Color.black);
				g.drawRect(i,k,10,10);
				g.fillRect(i,k,10,10);
			}
             }
     }
}
```
board is an array of booleans which I use to store whether or not the cell is dead or alive. So if it's true (alive) it should paint a green square, otherwise black (assume it's dead)

What I'm doing seems to work sort off, I've attached a screen of the output

Anyone any advice for me? As I said I've never done any of this painting stuff before, we've only ever used the terminal or console up till now


----------



## allnodcoms (Jun 30, 2007)

A couple of little problems here:

Loop variables 'i' and 'k' are incremented each time - by 1.
The second set of coordinates are 'always' 10 - there is no relation to 'i' and 'k'
I think you will find that this gets you a lot closer to where you want to be...

```
if(board[i][k] == true){
				g.setColor(Color.green); // see notes below...
			/*	g.drawRect(i, k, 10, 10); this is not needed */
				g.fillRect(i*10,k*10,(i*10)+9,(k*10)+9);}
```
The _i*10_ and _k*10_ convert your array indexes into screen coordinates (assuming 10px boxes) and the second set of parameters set the end points to the start points with a 9px offset.

The call to drawRect is not necessary, fillRect draws a filled Rect, it doesn't fill one you've already drawn.
If you clear your image to black initially, then set colour to Green before your loops you can remove setColor() and the else clause (as your screen is already black - so no need to draw black ones again. It's just quicker and more efficient).

Hope that helps.

Danny


----------



## robobobo (Apr 27, 2008)

Thanks very much for your reply,

While your code didn't exactly work for me, looking at it made me realise where I was going wrong.


```
public void paint(Graphics g) {

		for (int i = 0; i < BoardSize;) {
			for (int k = 0; k < BoardSize;) {

				if (board[i][k] == true) {
					g.setColor(Color.green);
					g.fillRect(i * 7, k * 7, 7, 7);
					g.setColor(Color.black);
					g.drawRect(i * 7, k * 7, 7, 7);
				} else {
					g.setColor(Color.white);
					g.fillRect(i * 7, k * 7, 7, 7);
				}
				g.setColor(Color.black);
				g.drawRect(i * 7, k * 7, 7, 7);
				k = k + 1;
			}
			i = i + 1;
		}

	}
```
That's the code I got working perfectly for me in the end, probably horrible inefficient but hey 

Thanks again for you help though, much appreciated


----------



## FusionLord (Jan 25, 2009)

Can you please mark the thread as solved.

Thanks,
-FusionLord


----------

