Associated DropTarget with MainWindow
When a FEN file is dropped into the main window, the drop target automatically loads it into the currently visible (=selected) game.
This commit is contained in:
parent
76fa3859ef
commit
964de02e24
@ -19,27 +19,26 @@ import java.util.List;
|
||||
*/
|
||||
public class FENDropTarget extends DropTargetAdapter {
|
||||
|
||||
private GamePane gamePane;
|
||||
private MainWindow mainWindow;
|
||||
|
||||
public FENDropTarget(GamePane gamePane) {
|
||||
this.gamePane = gamePane;
|
||||
public FENDropTarget(MainWindow mainWindow) {
|
||||
this.mainWindow = mainWindow;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drop(DropTargetDropEvent evt) {
|
||||
try {
|
||||
evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
|
||||
@SuppressWarnings(
|
||||
"unchecked"
|
||||
)
|
||||
@SuppressWarnings("unchecked")
|
||||
List<File> droppedFiles = (List<File>) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(droppedFiles.get(0)))) {
|
||||
gamePane.getGame().reset();
|
||||
gamePane.getGame().getBoard().initFromFEN(br.readLine());
|
||||
gamePane.getBoardPane().getBoardComponent().repaint();
|
||||
gamePane.getGame()
|
||||
mainWindow.getSelectedGamePane().getGame().reset();
|
||||
mainWindow.getSelectedGamePane().getGame().getBoard().initFromFEN(br.readLine());
|
||||
mainWindow.getSelectedGamePane().getBoardPane().getBoardComponent().repaint();
|
||||
mainWindow.getSelectedGamePane()
|
||||
.getGame()
|
||||
.getPlayers()
|
||||
.get(gamePane.getGame().getBoard().getLog().getActiveColor())
|
||||
.get(mainWindow.getSelectedGamePane().getGame().getBoard().getLog().getActiveColor())
|
||||
.requestMove();
|
||||
evt.dropComplete(true);
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package dev.kske.chess.ui;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.dnd.DropTarget;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComponent;
|
||||
@ -28,9 +27,6 @@ public class GamePane extends JComponent {
|
||||
private Game game;
|
||||
private Color activeColor;
|
||||
|
||||
@SuppressWarnings(
|
||||
"unused"
|
||||
)
|
||||
public GamePane() {
|
||||
setLayout(new BorderLayout());
|
||||
boardPane = new BoardPane();
|
||||
@ -38,10 +34,7 @@ public class GamePane extends JComponent {
|
||||
|
||||
JPanel toolPanel = new JPanel();
|
||||
btnRestart = new JButton("Restart");
|
||||
btnRestart.addActionListener((evt) -> {
|
||||
if (game != null) game.reset();
|
||||
game.start();
|
||||
});
|
||||
btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); });
|
||||
|
||||
activeColor = Color.WHITE;
|
||||
btnSwapColors = new JButton("Play as black");
|
||||
@ -67,8 +60,6 @@ public class GamePane extends JComponent {
|
||||
for (int i = 0; i < 8; i++)
|
||||
numberPanel.add(new JLabel(String.valueOf(8 - i)));
|
||||
add(numberPanel, BorderLayout.EAST);
|
||||
|
||||
new DropTarget(this, new FENDropTarget(this));
|
||||
}
|
||||
|
||||
public BoardPane getBoardPane() { return boardPane; }
|
||||
|
@ -2,6 +2,7 @@ package dev.kske.chess.ui;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.dnd.DropTarget;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JTabbedPane;
|
||||
@ -20,9 +21,6 @@ public class MainWindow {
|
||||
/**
|
||||
* Launch the application.
|
||||
*/
|
||||
@SuppressWarnings(
|
||||
"unused"
|
||||
)
|
||||
public static void main(String[] args) {
|
||||
EventQueue.invokeLater(() -> {
|
||||
try {
|
||||
@ -57,6 +55,7 @@ public class MainWindow {
|
||||
frame.getContentPane().add(tabbedPane);
|
||||
|
||||
frame.setJMenuBar(new MenuBar(this));
|
||||
new DropTarget(frame, new FENDropTarget(this));
|
||||
|
||||
// Update position and dimensions
|
||||
frame.pack();
|
||||
|
Reference in New Issue
Block a user