Implemented game loading from FEN file

This commit is contained in:
Kai S. K. Engelbart 2019-10-18 17:45:13 +02:00
parent a0b4405976
commit 86b696ef4c
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
2 changed files with 32 additions and 2 deletions

View File

@ -70,12 +70,23 @@ public class MainWindow extends JFrame {
/**
* Creates a new {@link GamePane}, adds it to the tabbed pane and opens it.
* The new tab has the title {@code Game n} where {@code n} is its number.
*
* @return The new {@link GamePane}
*/
public GamePane addGamePane() {
return addGamePane("Game " + (tabbedPane.getComponentCount() + 1));
}
/**
* Creates a new {@link GamePane}, adds it to the tabbed pane and opens it.
*
* @param title The title of the {@link GamePane}
* @return The new {@link GamePane}
*/
public GamePane addGamePane(String title) {
GamePane gamePane = new GamePane();
tabbedPane.add("Game " + (tabbedPane.getComponentCount() + 1), gamePane);
tabbedPane.add(title, gamePane);
tabbedPane.setSelectedIndex(tabbedPane.getComponentCount() - 1);
return gamePane;
}

View File

@ -2,6 +2,9 @@ package dev.kske.chess.ui;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
@ -47,7 +50,23 @@ public class MenuBar extends JMenuBar {
final String extension = file.getName().substring(file.getName().lastIndexOf('.')).toLowerCase();
switch (extension) {
case ".fen":
// TODO: Load board from FEN
try {
final GamePane gamePane = mainWindow
.addGamePane(file.getName().substring(0, file.getName().lastIndexOf('.')));
final String fen = new String(Files.readAllBytes(file.toPath()),
StandardCharsets.UTF_8);
DialogUtil.showGameConfigurationDialog((whiteName, blackName) -> {
final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, fen);
gamePane.setGame(game);
game.start();
});
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(mainWindow,
"Failed to load the file " + file.getName() + ": " + e.toString(),
"File loading error",
JOptionPane.ERROR_MESSAGE);
}
break;
case ".pgn":
// TODO: Load board from PGN