1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Help with GUI for Java Game Of Life

Discussion in 'Software Development' started by robobobo, Mar 20, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. robobobo

    robobobo Thread Starter

    Joined:
    Apr 27, 2008
    Messages:
    167
    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
    Code:
    	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
     

    Attached Files:

  2. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    A couple of little problems here:
    1. Loop variables 'i' and 'k' are incremented each time - by 1.
    2. 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...
    Code:
    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
     
  3. robobobo

    robobobo Thread Starter

    Joined:
    Apr 27, 2008
    Messages:
    167
    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.

    Code:
    	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 :p

    Thanks again for you help though, much appreciated
     
  4. FusionLord

    FusionLord

    Joined:
    Jan 24, 2009
    Messages:
    91
    Can you please mark the thread as solved.

    Thanks,
    -FusionLord
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1045956

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice