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:
@ -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()
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
16
app/src/main/java/jchess/KeyboardEvent.java
Normal file
16
app/src/main/java/jchess/KeyboardEvent.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user