Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Displaying A Grid. (http://www.programmingforums.org/showthread.php?t=1102)

BebopFusion Nov 11th, 2004 12:05 AM

I am making a cellular automota program and was wondering how I should go about the grid. I am doing it in swing as of now. The only thought that came to mind were JPanels that would have an image icon of blue for a live cell and white for dead. This would require a lot of typing unless I am able to create the 400 or so panels with a for statement. The problem is how would the syntax be written. I would just be throwing stuff out if I tried. Does anyone have sugtgestions?

p.s. Sorry to bug you with this question but GUI is not my strong point. I've been a command line programmer. Any suggestions are appreciated. Thank you for your time.

drunkenCoder Nov 15th, 2004 3:25 AM

Maybe this is what your looking for??
:

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class Cells extends JFrame
{
  public Cells()
  {     
    Container c = getContentPane();
    JPanel p = new JPanel( new GridLayout( 40, 40 ) );
    Random r = new Random( );
   
    for (int i = 0; i<1600; i++)
    {     
      JButton b = new JButton();
      if ( r.nextInt( 100 ) < 50 )
        b.setBackground( Color.BLUE );
      else
        b.setBackground( Color.WHITE );
      p.add( b );
    }
    c.add( p );
    setSize( 500, 500 );
    setTitle( "Cellular Automata" );
    setVisible( true );
   
  }
 
  public static void main(java.lang.String[] args)
  {
    Cells hey = new Cells();
    hey.setDefau ltCloseOperation( JFrame.EXIT_ON_CLOSE );
  }
}

Of course you are probably going to want to change the way the buttons get their color assigned to them. It is completely random as it is now.

BebopFusion Nov 15th, 2004 11:00 PM

Thank you very much. I do not have the time to delve into it it at the moment but will hopefully be able to look at it more tomorrow. I appreciate your time as it looks good and interesting.


All times are GMT -5. The time now is 12:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC