add simple pgn loader
parent
dc288ee95e
commit
2afe8d406c
|
|
@ -16,7 +16,6 @@ import com.github.bhlangonijr.chesslib.move.MoveList;
|
||||||
import com.github.bhlangonijr.chesslib.pgn.PgnHolder;
|
import com.github.bhlangonijr.chesslib.pgn.PgnHolder;
|
||||||
|
|
||||||
import de.mannheim.th.chess.App;
|
import de.mannheim.th.chess.App;
|
||||||
import de.mannheim.th.chess.ui.SpielFrame;
|
|
||||||
import de.mannheim.th.chess.utl.Clock;
|
import de.mannheim.th.chess.utl.Clock;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -29,7 +28,6 @@ public class Game {
|
||||||
|
|
||||||
private Board board;
|
private Board board;
|
||||||
private Clock clock;
|
private Clock clock;
|
||||||
private SpielFrame sp;
|
|
||||||
private String modus;
|
private String modus;
|
||||||
private boolean rotieren, zuruecknahme;
|
private boolean rotieren, zuruecknahme;
|
||||||
|
|
||||||
|
|
@ -68,9 +66,6 @@ public class Game {
|
||||||
this.movelist = new MoveList();
|
this.movelist = new MoveList();
|
||||||
|
|
||||||
clock = new Clock(modus);
|
clock = new Clock(modus);
|
||||||
|
|
||||||
sp = new SpielFrame(this);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -89,6 +84,8 @@ public class Game {
|
||||||
this.board.doMove(move);
|
this.board.doMove(move);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.clock = new Clock("blitz");
|
||||||
|
|
||||||
// this.clockPlayer1 = new Clock();
|
// this.clockPlayer1 = new Clock();
|
||||||
// this.clockPlayer2 = new Clock();
|
// this.clockPlayer2 = new Clock();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import de.mannheim.th.chess.App;
|
import de.mannheim.th.chess.App;
|
||||||
|
import de.mannheim.th.chess.domain.Game;
|
||||||
|
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
|
|
||||||
|
|
@ -28,6 +29,7 @@ public class MainFrame extends JFrame {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private JPanel contentPane;
|
private JPanel contentPane;
|
||||||
|
private Game game;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the frame.
|
* Create the frame.
|
||||||
|
|
@ -77,9 +79,7 @@ public class MainFrame extends JFrame {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
openSelectModeFrame();
|
||||||
ModeSelectionFrame ms = new ModeSelectionFrame();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
@ -87,6 +87,12 @@ public class MainFrame extends JFrame {
|
||||||
|
|
||||||
contentPane.add(Box.createVerticalStrut(15));
|
contentPane.add(Box.createVerticalStrut(15));
|
||||||
|
|
||||||
|
JButton pgnLoaderButton = new JButton("Lade aus PGN Datei");
|
||||||
|
pgnLoaderButton.addActionListener(e -> openPgnSelectFrame());
|
||||||
|
contentPane.add(pgnLoaderButton);
|
||||||
|
|
||||||
|
contentPane.add(Box.createVerticalStrut(15));
|
||||||
|
|
||||||
JButton btnNewButton_2 = new JButton("App beenden");
|
JButton btnNewButton_2 = new JButton("App beenden");
|
||||||
|
|
||||||
btnNewButton_2.setBackground(Color.LIGHT_GRAY);
|
btnNewButton_2.setBackground(Color.LIGHT_GRAY);
|
||||||
|
|
@ -105,4 +111,22 @@ public class MainFrame extends JFrame {
|
||||||
contentPane.add(btnNewButton_2);
|
contentPane.add(btnNewButton_2);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void startGame() {
|
||||||
|
if (this.game != null) {
|
||||||
|
SpielFrame sf = new SpielFrame(this.game);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGame(Game game) {
|
||||||
|
this.game = game;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void openSelectModeFrame() {
|
||||||
|
ModeSelectionFrame ms = new ModeSelectionFrame(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void openPgnSelectFrame() {
|
||||||
|
PGNLoaderFrame pf = new PGNLoaderFrame(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ public class ModeSelectionFrame extends JFrame {
|
||||||
private final ArrayList<Game> spiele = new ArrayList<>();
|
private final ArrayList<Game> spiele = new ArrayList<>();
|
||||||
private String fen;
|
private String fen;
|
||||||
|
|
||||||
public ModeSelectionFrame() {
|
public ModeSelectionFrame(MainFrame mf) {
|
||||||
// Frame-Eigenschaften
|
// Frame-Eigenschaften
|
||||||
setTitle("Modusauswahl");
|
setTitle("Modusauswahl");
|
||||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||||
|
|
@ -121,8 +121,10 @@ public class ModeSelectionFrame extends JFrame {
|
||||||
boolean zuruecknahme = jb2.isSelected();
|
boolean zuruecknahme = jb2.isSelected();
|
||||||
|
|
||||||
Game game = new Game(modus, rotieren, zuruecknahme, fen);
|
Game game = new Game(modus, rotieren, zuruecknahme, fen);
|
||||||
|
mf.setGame(game);
|
||||||
|
mf.startGame();
|
||||||
|
|
||||||
spiele.add(game);
|
// spiele.add(game);
|
||||||
|
|
||||||
dispose();
|
dispose();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,100 @@
|
||||||
|
package de.mannheim.th.chess.ui;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import javax.swing.BoxLayout;
|
||||||
|
import javax.swing.DefaultListModel;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JFileChooser;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JList;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.ListSelectionModel;
|
||||||
|
|
||||||
|
import com.github.bhlangonijr.chesslib.game.Game;
|
||||||
|
import com.github.bhlangonijr.chesslib.pgn.PgnHolder;
|
||||||
|
|
||||||
|
public class PGNLoaderFrame extends JFrame {
|
||||||
|
private PgnHolder pgn;
|
||||||
|
private File selectedFile;
|
||||||
|
private List<Game> games;
|
||||||
|
private DefaultListModel<String> gameListModel;
|
||||||
|
private JPanel contentPane;
|
||||||
|
private JList<String> gameList;
|
||||||
|
|
||||||
|
public PGNLoaderFrame(MainFrame mf) {
|
||||||
|
setResizable(true);
|
||||||
|
setAlwaysOnTop(true);
|
||||||
|
setTitle("Schach");
|
||||||
|
setBounds(100, 100, 500, 500);
|
||||||
|
|
||||||
|
contentPane = new JPanel();
|
||||||
|
setContentPane(contentPane);
|
||||||
|
|
||||||
|
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
|
||||||
|
|
||||||
|
JButton fileSelectButton = new JButton("Select File");
|
||||||
|
fileSelectButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
JFileChooser fileChooser = new JFileChooser();
|
||||||
|
int returnValue = fileChooser.showOpenDialog(null);
|
||||||
|
if (returnValue == JFileChooser.APPROVE_OPTION) {
|
||||||
|
selectedFile = fileChooser.getSelectedFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
contentPane.add(fileSelectButton);
|
||||||
|
|
||||||
|
JButton loadPgnButton = new JButton("Load file");
|
||||||
|
loadPgnButton.addActionListener(e -> loadFile());
|
||||||
|
contentPane.add(loadPgnButton);
|
||||||
|
|
||||||
|
gameListModel = new DefaultListModel<>();
|
||||||
|
|
||||||
|
gameList = new JList<>(gameListModel);
|
||||||
|
gameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||||
|
gameList.setVisibleRowCount(5);
|
||||||
|
|
||||||
|
JScrollPane scrollPane = new JScrollPane(gameList);
|
||||||
|
|
||||||
|
contentPane.add(scrollPane);
|
||||||
|
|
||||||
|
JButton startGameButton = new JButton("Starte Spiel");
|
||||||
|
startGameButton.addActionListener(e -> {
|
||||||
|
int index = gameList.getSelectedIndex();
|
||||||
|
de.mannheim.th.chess.domain.Game game = new de.mannheim.th.chess.domain.Game(games.get(index).getHalfMoves());
|
||||||
|
|
||||||
|
mf.setGame(game);
|
||||||
|
mf.startGame();
|
||||||
|
});
|
||||||
|
contentPane.add(startGameButton);
|
||||||
|
|
||||||
|
this.setVisible(true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadFile() {
|
||||||
|
if (this.selectedFile != null) {
|
||||||
|
pgn = new PgnHolder(this.selectedFile.getAbsolutePath());
|
||||||
|
try {
|
||||||
|
pgn.loadPgn();
|
||||||
|
games = pgn.getGames();
|
||||||
|
int i = 0;
|
||||||
|
for (Game game : games) {
|
||||||
|
gameListModel.addElement(i++ + "");
|
||||||
|
}
|
||||||
|
gameList.revalidate();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// TODO: handle exception
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue