Hallo,
Ich möchte in einem JFrame ein raster mit x Quadraten vertikal und horizontal zeichnen. Ich dachte mir wenn ich die
Ich möchte in einem JFrame ein raster mit x Quadraten vertikal und horizontal zeichnen. Ich dachte mir wenn ich die
Größe des Jframes = der festen größe jedes Quadrates * x
setze passt es, aber rechts und unten sind meine Quadrate abgeschnitten. Außerdem möchte ich über dem Raster noch zwei Jpanels einfügen, da bin ich mir auch nicht sicher wie ich das bewergstelligen soll.
Java:
package com.marco.gameoflife.gui;
import com.marco.gameoflife.Universe;
import javax.swing.*;
import java.awt.*;
public class GameOfLife extends JFrame {
private final byte sizePerCell = 20;
private Universe universe;
public GameOfLife() {
super("Game of Life");
universe = new Universe(10);
setSize(universe.getSize() * sizePerCell, universe.getSize() * sizePerCell);
setTitle("Game of Life");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void paint(Graphics g) {
boolean[][] cells = universe.getCells();
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
//cell is alive
if (cells[i][j]) {
g.fillRect(i * sizePerCell + getInsets().left,
j * sizePerCell + getInsets().top, sizePerCell, sizePerCell);
} else {
g.drawRect(i * sizePerCell + getInsets().left,
j * sizePerCell + getInsets().top, sizePerCell, sizePerCell);
}
}
}
}
}
Java:
package com.marco.gameoflife;
import java.util.Random;
/**
* The {@code Universe} class defines the Universe of Conway's Game of Life
* @version 1.0
* @see <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway's Game of Life</a>
*/
public class Universe {
private int generation = 0;
private final int size;
/**
* Each Cell in the Universe can be living or dead
*/
private boolean cells[][];
/**
* Constructs a Universe with the given size
* @param size the horizontal and vertical size of the Universe
*/
public Universe(int size){
this.size = size;
Random random = new Random();
cells = new boolean[size + 2][size + 2];
for (int i = 1; i < cells.length - 1; i++) {
for (int j = 1; j < cells.length - 1; j++) {
cells[i][j] = random.nextBoolean();
}
}
expand();
generation++;
}
/**
* Constructs a {@code Universe} that represents the next State
* @param universe The current State of the Universe
*/
private Universe(Universe universe) {
this.size = universe.size;
cells = new boolean[size + 2][size + 2];
for (int i = 1; i < cells.length - 1; i++) {
for (int j = 1; j < cells.length - 1; j++) {
if (universe.cells[i][j] && (universe.countLivingNeighbors(i,j) == 2
|| universe.countLivingNeighbors(i,j) == 3)){
this.cells[i][j] = true;
}
else if (!universe.cells[i][j] && universe.countLivingNeighbors(i, j)== 3){
this.cells[i][j] = true;
}
else {
this.cells[i][j] = false;
}
}
}
expand();
generation++;
}
/**
* Expands the Universe for easier comparison across borders
*/
private void expand() {
for (int i = 1; i < cells.length -1; i++) {
cells[i][0] = cells[i][cells.length - 2]; //copies the right cells
cells[i][cells.length - 1] = cells[i][1]; //the left cells
cells[0][i] = cells[cells.length - 2][i]; //the bottom cells
cells[cells.length - 1][i] = cells[1][i]; //the top cells
}
cells[0][0] = cells[cells.length -2][cells.length -2];
cells[cells.length -1][cells.length -1] = cells[1][1];
cells[0][cells.length -1] = cells[cells.length -2][1];
cells[cells.length - 1][0] = cells[1][cells.length -2];
}
/**
* Counts the number of living Cells neighboring the Cell at (y|x).
* @param y The y Position of the Cell
* @param x The x Position of the cell
* @return the Number of living Cells arround the Cell
*/
private int countLivingNeighbors(int y, int x) {
int livingNeighbors = 0;
if (cells[y - 1][x - 1])
livingNeighbors++;
if (cells[y - 1][x])
livingNeighbors++;
if (cells[y - 1][x + 1])
livingNeighbors++;
if (cells[y][x - 1])
livingNeighbors++;
if (cells[y][x + 1])
livingNeighbors++;
if (cells[y + 1][x - 1])
livingNeighbors++;
if (cells[y + 1][x])
livingNeighbors++;
if (cells[y + 1][x + 1])
livingNeighbors++;
return livingNeighbors;
}
/**
* returns the size of the Universe
* @return size of the Universe
*/
public int getSize(){
return size;
}
/**
* prints the Universe to the console
* for each cell it prints O if true (the cell is alive) or a space if false(dead)
*/
public void printUniverse() {
for (int i = 1; i < cells.length - 1; i++) {
for (int j = 1; j < cells.length - 1; j++) {
System.out.print(cells[i][j] ? 'O' : ' ');
}
System.out.println();
}
}
/**
* gets the amount of living Cells in the Universe
* @return the amount of living cells in the Universe
*/
public int getLivingCells(){
int counter = 0;
for (int i = 1; i < cells.length - 1; i++) {
for (int j = 1; j < cells.length - 1; j++) {
if (cells[i][j])
counter++;
}
}
return counter;
}
/**
* returns the 2d Array of the Universe
* @return the 2d Array of the Universe
*/
public boolean[][] getCells(){
return cells;
}
public void evolve(){
cells = new Universe(this).cells;
}
}