Board Made of Rectangles

I don't know if this is the right way to do this but I seems looks like
a good idea.
This commit is contained in:
Debucquoy Anthony 2023-04-02 23:22:38 +02:00
parent 58f17472d8
commit 26d3bb5c05
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
7 changed files with 76 additions and 10 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>

View File

@ -4,5 +4,5 @@
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" project-jdk-name="19" project-jdk-type="JavaSDK" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="19" project-jdk-type="JavaSDK" />
</project>

BIN
Current_Window.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

View File

@ -1,3 +1,16 @@
# jchess
Java chess game
A chess Board writen in Java
The goal of this project is to get used to javaFX. This might awefull code, don't loook to deeply!
## Build & Run
```shell
$ ./gradlew build
$ ./gradlew run
```
## Appearance
![Current Window](./Current_Window.png "Current Window")

View File

@ -9,6 +9,7 @@
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.13'
}
repositories {
@ -29,6 +30,11 @@ application {
mainClass = 'jchess.App'
}
javafx {
version = "20"
modules = [ 'javafx.controls']
}
tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()

View File

@ -1,14 +1,44 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package jchess;
public class App {
public String getGreeting() {
return "Hello World!";
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class App extends Application {
public final static int WIDTH = 500, HEIGHT = 500;
public final static String PROGRAM_NAME = "JChess";
public static void main(String[] args) {
launch();
}
public static void main(String[] args) {
System.out.println(new App().getGreeting());
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
GridPane board = new GridPane();
Rectangle[][] cases = new Rectangle[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
cases[i][j] = new Rectangle(WIDTH/8, HEIGHT/8);
cases[i][j].setFill((i + j) % 2 == 0 ? Color.WHITE : Color.LIGHTGRAY);
board.add(cases[i][j], i, j);
}
}
root.setCenter(board);
Scene scene = new Scene(root, WIDTH, HEIGHT);
scene.addEventHandler(KeyEvent.KEY_PRESSED, new KeyboardEvent());
primaryStage.setScene(scene);
primaryStage.setTitle(PROGRAM_NAME);
primaryStage.show();
}
}

View File

@ -0,0 +1,16 @@
package jchess;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
public class KeyboardEvent implements EventHandler<KeyEvent> {
@Override
public void handle(KeyEvent event) {
System.out.println(event.getCharacter());
if(event.getCode() == KeyCode.Q){
Platform.exit();
}
}
}