canvas changes

This commit is contained in:
ramollia
2025-10-28 09:13:09 +01:00
parent d6f3bb2185
commit 2cda94e4db
5 changed files with 13 additions and 7 deletions

View File

@@ -6,6 +6,6 @@ import java.util.function.Consumer;
public class Connect4Canvas extends GameCanvas { public class Connect4Canvas extends GameCanvas {
public Connect4Canvas(Color color, int width, int height, Consumer<Integer> onCellClicked) { public Connect4Canvas(Color color, int width, int height, Consumer<Integer> onCellClicked) {
super(color, width, height, 7, 6, 10, true, onCellClicked); super(color, Color.TRANSPARENT, width, height, 7, 6, 10, true, onCellClicked);
} }
} }

View File

@@ -22,6 +22,7 @@ public abstract class GameCanvas {
protected final GraphicsContext graphics; protected final GraphicsContext graphics;
protected final Color color; protected final Color color;
protected final Color backgroundColor;
protected final int width; protected final int width;
protected final int height; protected final int height;
@@ -34,11 +35,12 @@ public abstract class GameCanvas {
protected final Cell[] cells; protected final Cell[] cells;
protected GameCanvas(Color color, int width, int height, int rowSize, int columnSize, int gapSize, boolean edges, Consumer<Integer> onCellClicked) { protected GameCanvas(Color color, Color backgroundColor, int width, int height, int rowSize, int columnSize, int gapSize, boolean edges, Consumer<Integer> onCellClicked) {
canvas = new Canvas(width, height); canvas = new Canvas(width, height);
graphics = canvas.getGraphicsContext2D(); graphics = canvas.getGraphicsContext2D();
this.color = color; this.color = color;
this.backgroundColor = backgroundColor;
this.width = width; this.width = width;
this.height = height; this.height = height;
@@ -83,6 +85,9 @@ public abstract class GameCanvas {
} }
private void render() { private void render() {
graphics.setFill(backgroundColor);
graphics.fillRect(0, 0, width, height);
graphics.setFill(color); graphics.setFill(color);
for (int x = 0; x < rowSize - 1; x++) { for (int x = 0; x < rowSize - 1; x++) {
@@ -123,6 +128,9 @@ public abstract class GameCanvas {
final float height = cells[cell].height(); final float height = cells[cell].height();
graphics.clearRect(x, y, width, height); graphics.clearRect(x, y, width, height);
graphics.setFill(backgroundColor);
graphics.fillRect(x, y, width, height);
} }
public void clearAll() { public void clearAll() {

View File

@@ -6,7 +6,7 @@ import java.util.function.Consumer;
public final class ReversiCanvas extends GameCanvas { public final class ReversiCanvas extends GameCanvas {
public ReversiCanvas(Color color, int width, int height, Consumer<Integer> onCellClicked) { public ReversiCanvas(Color color, int width, int height, Consumer<Integer> onCellClicked) {
super(color, width, height, 8, 8, 10, true, onCellClicked); super(color, Color.GREEN, width, height, 8, 8, 5, true, onCellClicked);
drawStartingDots(); drawStartingDots();
} }

View File

@@ -6,7 +6,7 @@ import java.util.function.Consumer;
public final class TicTacToeCanvas extends GameCanvas { public final class TicTacToeCanvas extends GameCanvas {
public TicTacToeCanvas(Color color, int width, int height, Consumer<Integer> onCellClicked) { public TicTacToeCanvas(Color color, int width, int height, Consumer<Integer> onCellClicked) {
super(color, width, height, 3, 3, 30, false, onCellClicked); super(color, Color.TRANSPARENT, width, height, 3, 3, 30, false, onCellClicked);
} }
public void drawX(Color color, int cell) { public void drawX(Color color, int cell) {

View File

@@ -1,8 +1,6 @@
package org.toop.app.game; package org.toop.app.game;
import javafx.animation.SequentialTransition; import javafx.animation.SequentialTransition;
import javafx.animation.Timeline;
import javafx.util.Duration;
import org.toop.app.App; import org.toop.app.App;
import org.toop.app.GameInformation; import org.toop.app.GameInformation;
import org.toop.app.canvas.ReversiCanvas; import org.toop.app.canvas.ReversiCanvas;
@@ -64,7 +62,7 @@ public final class ReversiGame {
}, onMessage); }, onMessage);
} }
canvas = new ReversiCanvas(Color.GRAY, canvas = new ReversiCanvas(Color.BLACK,
(App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3,
(cell) -> { (cell) -> {
if (onForfeit == null || onExit == null) { if (onForfeit == null || onExit == null) {