-1

I'm trying to create a grid of rectangles but not sure how to go from here? I also don't know if this is right so far

public CGOL (int x, int y, int size, int squares1) {
  numberOfSquares = 100;
  isAlive = new boolean [numberOfSquares][numberOfSquares];
  squares = new Rectangle.Double [numberOfSquares][numberOfSquares];
  int rows = 12;
  int cols = 40;
  int width = getSize().width;
  int height = getSize().height;

  int rowHt = height / (rows);
  int rowWid = width / (cols);

  for (int i = 0; i < rows ; i++) {
    for (int j = 0; i < rows ; j++) {
    double locationX = (i * rowHt);
    double locationY = (j * rowWid);


squares[i][j] = new Rectangle2D.Double(locationX,locationY, rows, cols);
K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
  • 1
    The code you have shown is *highly incomplete*. There are references to invisible parts like `getSize()`, and there are incomplete blocks. Furthermore the function parameters are not used at all and their intended functionality is unclear. Last but not least it contains "magic numbers" like 100 that are not explained by the requirements. You should clarify your question and point out what is the expected behavior. What is input? What is the expected output? – Marcel Dec 10 '18 at 20:28

1 Answers1

0

I don't know what you want to use the grid for, but I'd use a JPanel to draw the grid. Here's a working example. First the JFrame:

package net.stackoverflow;

import javax.swing.JFrame;

public class MainFrame extends JFrame {

    static final long serialVersionUID = 19670916;

    protected GridPanel gridPanel = null;

    public MainFrame() {

        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        createGui();
        setVisible( true );
    }

    protected void createGui() {

        setSize( 600, 400 );
        setTitle( "Test Grid" );

        gridPanel = new GridPanel();

        add( gridPanel );
    }

    public static void main(String args[]) {

        MainFrame mf = new MainFrame();     
    }

}

And now the JPanel:

package net.stackoverflow;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JPanel;

public class GridPanel extends JPanel {

    private static final long serialVersionUID = -5341480790176820445L;

    private final int NUM_SQUARES = 100;
    private final int RECT_SIZE = 10;
    private ArrayList<Rectangle> grid = null;

    public GridPanel() {

        setSize( 200, 200 );

        // Build the grid
        grid = new ArrayList<Rectangle>( NUM_SQUARES );
        for( int y=0; y < NUM_SQUARES / 10; ++y ) {
            for( int x=0; x < NUM_SQUARES / 10; ++x ) {
                Rectangle rect = new Rectangle( x * RECT_SIZE, y * RECT_SIZE, RECT_SIZE, RECT_SIZE );
                grid.add( rect );
            }
        }
    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent( g );

        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 200, 200);

        // paint the grid
        for( Rectangle r : grid ) {

            g.setColor(Color.BLACK);
            g.drawRect( r.x, r.y, r.width, r.height );
        }
    }
}

Within the paintComponent(), you can check to see if the grid box is alive or whatever and draw it however you like.

Frecklefoot
  • 1,660
  • 2
  • 21
  • 52