简体   繁体   中英

how can I add multiple rectangles to JComponent?

I'm creating the game of life in java and need to draw the cells randomly on the screen, so I am extending the JComponent class to draw the rectangles then I want to add the JComponent to my JFrame. I previously tried drawing the rectangles straight onto the JFrame but came to find out that is not how things are to be done so now I created a nested private class and done the drawing of the rectangle on that and added it to my JFrame. Everything's compiling correctly but nothing is shown on my JFrame. My code is as follows:

import javax.swing.JFrame;
import java.util.Random;
import java.util.Arrays;
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Color;

public class GameOfLife extends JFrame {

  int PIXELSIZE = 10;
  int ROW = 700;
  int COLUMN = 700;
  Random random = new Random();
  int grid[][] = new int[ROW][COLUMN];
  int updatedGrid[][] = new int[ROW][COLUMN];

  public GameOfLife() {
    this.setTitle("Game Of Life: Java");
    this.setSize(ROW, COLUMN);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(new DrawCells());
    this.setVisible(true);
  }

  public int[][] createGrid() {
    for (int i = 0; i < grid.length; i++) {
      for (int j = 0; j < grid[i].length; j++) {
        grid[i][j] = random.nextInt(2);
      }
    }
    //System.out.println(Arrays.deepToString(grid).replace("], ", "]\n"));
    return grid;
  }

  private class DrawCells extends JComponent {
    int x;
    int y;
    public void paintComponent(Graphics g) {
      x = random.nextInt();
      y = random.nextInt();
      Graphics2D g2 = (Graphics2D) g;
      for (int i = 0; i < ROW - 1; i++) {
        for (int j = 0; j < COLUMN - 1; j++) {
          if (grid[ROW][COLUMN] == 1) {
            super.paintComponent(g);
            g2.setColor(Color.BLUE);
            g2.drawRect(x, y, 10, 10);
          }
        }
      }
    }
  }

  public static void main(String[] args) {
    GameOfLife gol = new GameOfLife();
  }
}

EDIT: I'm getting a runtime error which is java.lang.ArrayIndexOutOfBoundsException:

Any help or information on how to get my rectangles drawn correctly will be very much appreciated, thanks.

Several issues with the code:

super.paintComponent(g);

The purpose of invoking super.paintComponent() is to invoke the default painting code of the component. It should only ever be invoked once at the top of the method. If should NOT be invoked inside the looping code.

if (grid[ROW][COLUMN] == 1) {

The above statement does nothing (well actually it causes an Exception) because you always check the same value in the array.

What you really want is to use the value of your "i" and "j" indexes to see which rectangles should be painted.

  for (int i = 0; i < ROW - 1; i++) {
    for (int j = 0; j < COLUMN - 1; j++) {

There is no need for the "- 1". You want to check each value in the array.

  x = random.nextInt();
  y = random.nextInt();

Don't use random logic in a painting method. The painting method should only ever paint the current state of the component. It should not change the state.

int PIXELSIZE = 10;
int ROW = 700;
int COLUMN = 700;

Those numbers don't make sense. If you have a row with 700 objects and each object is 10 pixels, then you would need a screen size of 7,000 pixels.

Maybe use something like:

int PIXELSIZE = 2;
int ROW = 500;
int COLUMN = 300;

Now when you do your painting you would have code something like:

//g2.drawRect(x, y, 10, 10);
g2.fillRect(j * PIXELSIZE, i * PIXELSIZE, PIXELSIZE, PIXELSIZE);

So each entry in your array will be painted as two pixels on the screen.

Also, you never invoked the createGrid() method so the values in your array never get set.

However, the biggest problem is that the Array should be part of the DrawCells class. The data should be stored in the class so the painting method can use that data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM