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