Working Drawing System
This commit is contained in:
+2
-2
@@ -39,8 +39,8 @@ public class Draw extends JPanel {
|
||||
super.paintComponent(g);
|
||||
g2d = (Graphics2D) g;
|
||||
|
||||
float cellSizeX = width / xSize;
|
||||
float cellSizeY = height / ySize;
|
||||
float cellSizeX = 800f / xSize;
|
||||
float cellSizeY = 800f / ySize;
|
||||
|
||||
if(ySize > xSize)
|
||||
{
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ public class Clock extends Thread{
|
||||
public void run() {
|
||||
if (running){
|
||||
control.showGrid(control.getCells());
|
||||
control.nextGen();
|
||||
//control.nextGen();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
+48
-33
@@ -2,7 +2,6 @@ package game;
|
||||
|
||||
import gui.GUI;
|
||||
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class Control {
|
||||
@@ -54,7 +53,7 @@ public class Control {
|
||||
|
||||
}
|
||||
|
||||
if (gui == null){
|
||||
if (gui == null) {
|
||||
gui = new GUI(this);
|
||||
}
|
||||
|
||||
@@ -89,8 +88,6 @@ public class Control {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// cells[1][2] = true;
|
||||
// cells[2][2] = true;
|
||||
// cells[2][4] = true;
|
||||
@@ -133,26 +130,26 @@ public class Control {
|
||||
}
|
||||
|
||||
public boolean[][] nextGen() {
|
||||
gen ++;
|
||||
gen++;
|
||||
System.out.println("Generation:" + gen);
|
||||
|
||||
for (int x = 0; x < xSize; x++){
|
||||
for (int x = 0; x < xSize; x++) {
|
||||
for (int y = 0; y < ySize; y++) {
|
||||
int n = aliveNeigbours(x, y);
|
||||
|
||||
if (n == 3 && !cells[x][y]){
|
||||
if (n == 3 && !cells[x][y]) {
|
||||
newcells[x][y] = true;
|
||||
}
|
||||
|
||||
if (n < 2){
|
||||
if (n < 2) {
|
||||
newcells[x][y] = false;
|
||||
}
|
||||
|
||||
if (n == 2 || n == 3){
|
||||
if (n == 2 || n == 3) {
|
||||
|
||||
}
|
||||
|
||||
if (n > 3){
|
||||
if (n > 3) {
|
||||
newcells[x][y] = false;
|
||||
}
|
||||
|
||||
@@ -169,7 +166,7 @@ public class Control {
|
||||
}
|
||||
|
||||
|
||||
public boolean[][] nextGen_GridTest(){
|
||||
public boolean[][] nextGen_GridTest() {
|
||||
for (int x = 0; x < xSize; x++) {
|
||||
for (int y = 0; y < ySize; y++) {
|
||||
cells[x][y] = false;
|
||||
@@ -177,12 +174,13 @@ public class Control {
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (x == xSize){ x = 0; y++;}
|
||||
if (x == xSize) {
|
||||
x = 0;
|
||||
y++;
|
||||
}
|
||||
if (y == ySize) y = 0;
|
||||
|
||||
|
||||
|
||||
cells[x][y] = true;
|
||||
x++;
|
||||
|
||||
@@ -216,30 +214,47 @@ public class Control {
|
||||
gui.showGrid(grid);
|
||||
}
|
||||
|
||||
public void addCell(int mouseX, int mouseY) {
|
||||
// System.out.print("X" + mouseX + "|" + "Y" + mouseY + " ");
|
||||
|
||||
System.out.println("x: " + xSize + " y: " + ySize + " ");
|
||||
// System.out.println((mouseX) / (800 / xSize) + " ");
|
||||
|
||||
cells[mouseX / (800 / xSize)][mouseY / (800 / ySize)] = true;
|
||||
showGrid(cells);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void removeCell(int x, int y) {
|
||||
|
||||
cells[Math.round(y/(800f/xSize))][Math.round(y/(800f/ySize))] = false;
|
||||
showGrid(cells);
|
||||
|
||||
}
|
||||
|
||||
public void createControlWindow() {
|
||||
gui.createControlWindow();
|
||||
}
|
||||
|
||||
public void setCells(){
|
||||
public void setCell(int mouseX, int mouseY, boolean value) {
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
|
||||
float ratio = (float) (xSize) / (float) (ySize);
|
||||
|
||||
if (ySize > xSize) {
|
||||
|
||||
x = (int) ((float) mouseX / (800f / ((float) xSize / ratio)));
|
||||
y = (int) ((float) mouseY / (800f / ((float) ySize)));
|
||||
|
||||
} else {
|
||||
|
||||
x = (int) ((float) mouseX / (800f / ((float) xSize)));
|
||||
y = (int) ((float) mouseY / (800f / ((float) ySize * ratio)));
|
||||
|
||||
}
|
||||
|
||||
if (x >= 0 && x < xSize && y >= 0 && y < ySize && (checkCellCount() || !value))
|
||||
cells[x][y] = value;
|
||||
|
||||
gui.showGrid(cells);
|
||||
}
|
||||
|
||||
private boolean checkCellCount() {
|
||||
|
||||
int counter = 0;
|
||||
|
||||
for (int x = 0; x < xSize; x++) {
|
||||
for (int y = 0; y < ySize; y++) {
|
||||
if (cells[x][y])
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
return counter < cellCount;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+29
-60
@@ -16,7 +16,7 @@ public class GameWindow {
|
||||
private final int cellCountY;
|
||||
|
||||
private int width, height;
|
||||
private float ratio;
|
||||
private final float ratio;
|
||||
|
||||
private boolean mouseLeft = false, mouseRight = false;
|
||||
|
||||
@@ -24,11 +24,10 @@ public class GameWindow {
|
||||
this.cellCountX = cellCountX;
|
||||
this.cellCountY = cellCountY;
|
||||
|
||||
calcWindowSize();
|
||||
|
||||
jfGame = new JFrame(title);
|
||||
|
||||
jfGame.getContentPane().setPreferredSize(new Dimension(width, height));
|
||||
jfGame.getContentPane().setPreferredSize(new Dimension(800, 800));
|
||||
jfGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
jfGame.addKeyListener(new KeyListener() {
|
||||
@@ -53,75 +52,55 @@ public class GameWindow {
|
||||
jfGame.getContentPane().addMouseMotionListener(new MouseMotionListener() {
|
||||
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e)
|
||||
{
|
||||
if(mouseLeft)
|
||||
{
|
||||
// control.setCell(e.getX(), e.getY(), true);
|
||||
control.addCell(e.getX(), e.getY());
|
||||
}
|
||||
else if(mouseRight)
|
||||
{
|
||||
control.setCell(e.getX(), e.getY(), 0);
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
if (mouseLeft) {
|
||||
control.setCell(e.getX(), e.getY(), true);
|
||||
} else if (mouseRight) {
|
||||
control.setCell(e.getX(), e.getY(), false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e)
|
||||
{
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
gameWindow.getContentPane().addMouseListener(new MouseListener()
|
||||
{
|
||||
jfGame.getContentPane().addMouseListener(new MouseListener() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e)
|
||||
{
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e)
|
||||
{
|
||||
switch(e.getButton())
|
||||
{
|
||||
case 1:
|
||||
public void mousePressed(MouseEvent e) {
|
||||
switch (e.getButton()) {
|
||||
case 1 -> {
|
||||
mouseLeft = true;
|
||||
// control.setCell(e.getX(), e.getY(), true);
|
||||
control.addCell(e.getX(), e.getY());
|
||||
break;
|
||||
case 3:
|
||||
control.setCell(e.getX(), e.getY(), true);
|
||||
}
|
||||
case 3 -> {
|
||||
mouseRight = true;
|
||||
// control.setCell(e.getX(), e.getY(), false);
|
||||
control.addCell(e.getX(), e.getY());
|
||||
break;
|
||||
control.setCell(e.getX(), e.getY(), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e)
|
||||
{
|
||||
switch(e.getButton())
|
||||
{
|
||||
case 1:
|
||||
mouseLeft = false;
|
||||
break;
|
||||
case 3:
|
||||
mouseRight = false;
|
||||
break;
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
switch (e.getButton()) {
|
||||
case 1 -> mouseLeft = false;
|
||||
case 3 -> mouseRight = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e)
|
||||
{
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e)
|
||||
{
|
||||
public void mouseExited(MouseEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
@@ -129,8 +108,8 @@ public class GameWindow {
|
||||
jfGame.setResizable(false);
|
||||
|
||||
ratio = (float) (cellCountY) / (float) (cellCountX);
|
||||
draw = new Draw(this.cellCountX, this.cellCountY, width, height, ratio);
|
||||
draw.setBounds(0, 0, width, height);
|
||||
draw = new Draw(this.cellCountX, this.cellCountY, 800, 800, ratio);
|
||||
draw.setBounds(0, 0, 800, 800);
|
||||
|
||||
draw.setVisible(true);
|
||||
|
||||
@@ -142,24 +121,14 @@ public class GameWindow {
|
||||
jfGame.setVisible(true);
|
||||
}
|
||||
|
||||
public void showGrid(boolean[][] grid)
|
||||
{
|
||||
if(draw != null)
|
||||
{
|
||||
public void showGrid(boolean[][] grid) {
|
||||
if (draw != null) {
|
||||
draw.showGrid(grid);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
System.err.println("you need to build the game gameWindow before showing a grid");
|
||||
}
|
||||
}
|
||||
|
||||
private void calcWindowSize(){
|
||||
|
||||
width = (int) (cellCountX * ratio );
|
||||
height = (int) (cellCountY * ratio);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user