Implemented game loading from FEN file
This commit is contained in:
		| @@ -70,12 +70,23 @@ public class MainWindow extends JFrame { | |||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * Creates a new {@link GamePane}, adds it to the tabbed pane and opens it. | 	 * 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} | 	 * @return The new {@link GamePane} | ||||||
| 	 */ | 	 */ | ||||||
| 	public GamePane addGamePane() { | 	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(); | 		GamePane gamePane = new GamePane(); | ||||||
| 		tabbedPane.add("Game " + (tabbedPane.getComponentCount() + 1), gamePane); | 		tabbedPane.add(title, gamePane); | ||||||
| 		tabbedPane.setSelectedIndex(tabbedPane.getComponentCount() - 1); | 		tabbedPane.setSelectedIndex(tabbedPane.getComponentCount() - 1); | ||||||
| 		return gamePane; | 		return gamePane; | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -2,6 +2,9 @@ package dev.kske.chess.ui; | |||||||
|  |  | ||||||
| import java.awt.Toolkit; | import java.awt.Toolkit; | ||||||
| import java.awt.datatransfer.StringSelection; | 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.JMenu; | ||||||
| import javax.swing.JMenuBar; | import javax.swing.JMenuBar; | ||||||
| @@ -47,7 +50,23 @@ public class MenuBar extends JMenuBar { | |||||||
| 			final String extension = file.getName().substring(file.getName().lastIndexOf('.')).toLowerCase(); | 			final String extension = file.getName().substring(file.getName().lastIndexOf('.')).toLowerCase(); | ||||||
| 			switch (extension) { | 			switch (extension) { | ||||||
| 				case ".fen": | 				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; | 					break; | ||||||
| 				case ".pgn": | 				case ".pgn": | ||||||
| 					// TODO: Load board from PGN | 					// TODO: Load board from PGN | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user