pgnReader
dstuck 2025-06-25 03:12:46 +02:00
parent cdf993d998
commit dc6e6d311b
11 changed files with 135 additions and 85 deletions

View File

@ -0,0 +1,24 @@
package de.mannheim.th.chess.controller.controlPanel;
import de.mannheim.th.chess.ui.SpielFrame;
import de.mannheim.th.chess.domain.Game;
public abstract class BaseButtonViewListener {
protected SpielFrame sf;
protected Game game;
public BaseButtonViewListener(Game game, SpielFrame sf) {
this.sf = sf;
this.game = game;
}
/**
* Loads the gamestate and renders the board
*/
protected void updateView() {
this.game.loadView();
this.sf.setDefaultButtons();
this.sf.applyBoardButtons();
this.sf.ladeBrett();
}
}

View File

@ -1,5 +1,4 @@
package de.mannheim.th.chess.controller;
package de.mannheim.th.chess.controller.controlPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

View File

@ -1,4 +1,4 @@
package de.mannheim.th.chess.controller;
package de.mannheim.th.chess.controller.controlPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

View File

@ -6,23 +6,17 @@ import java.awt.event.ActionListener;
import de.mannheim.th.chess.domain.Game;
import de.mannheim.th.chess.ui.SpielFrame;
public class ButtonViewBackListener implements ActionListener {
private Game game;
private SpielFrame sf;
public class ButtonViewBackListener extends BaseButtonViewListener implements ActionListener {
public ButtonViewBackListener(Game game, SpielFrame sf) {
this.game = game;
this.sf = sf;
super(game, sf);
}
@Override
public void actionPerformed(ActionEvent e) {
if (this.game.getViewPointer() > 0) {
this.game.setViewPointer(this.game.getViewPointer() - 1);
this.game.loadView();
this.sf.setDefaultButtons();
this.sf.applyBoardButtons();
this.sf.ladeBrett();
updateView();
}
}

View File

@ -6,22 +6,16 @@ import java.awt.event.ActionListener;
import de.mannheim.th.chess.domain.Game;
import de.mannheim.th.chess.ui.SpielFrame;
public class ButtonViewFirstListener implements ActionListener {
private Game game;
private SpielFrame sf;
public class ButtonViewFirstListener extends BaseButtonViewListener implements ActionListener {
public ButtonViewFirstListener(Game game, SpielFrame sf) {
this.game = game;
this.sf = sf;
super(game, sf);
}
@Override
public void actionPerformed(ActionEvent e) {
this.game.setViewPointer(0);
this.game.loadView();
this.sf.setDefaultButtons();
this.sf.applyBoardButtons();
this.sf.ladeBrett();
updateView();
}
}

View File

@ -6,23 +6,17 @@ import java.awt.event.ActionListener;
import de.mannheim.th.chess.domain.Game;
import de.mannheim.th.chess.ui.SpielFrame;
public class ButtonViewForwardListener implements ActionListener {
private Game game;
private SpielFrame sf;
public class ButtonViewForwardListener extends BaseButtonViewListener implements ActionListener {
public ButtonViewForwardListener(Game game, SpielFrame sf) {
this.game = game;
this.sf = sf;
super(game, sf);
}
@Override
public void actionPerformed(ActionEvent e) {
if (this.game.getMoveList().size() > this.game.getViewPointer()) {
this.game.setViewPointer(this.game.getViewPointer() + 1);
this.game.loadView();
this.sf.setDefaultButtons();
this.sf.applyBoardButtons();
this.sf.ladeBrett();
updateView();
}
}

View File

@ -7,22 +7,16 @@ import java.awt.event.ActionListener;
import de.mannheim.th.chess.domain.Game;
import de.mannheim.th.chess.ui.SpielFrame;
public class ButtonViewLastListener implements ActionListener {
private Game game;
private SpielFrame sf;
public class ButtonViewLastListener extends BaseButtonViewListener implements ActionListener {
public ButtonViewLastListener(Game game, SpielFrame sf) {
this.game = game;
this.sf = sf;
super(game, sf);
}
@Override
public void actionPerformed(ActionEvent e) {
this.game.setViewPointer(this.game.getMoveList().size());
this.game.loadView();
this.sf.setDefaultButtons();
this.sf.applyBoardButtons();
this.sf.ladeBrett();
updateView();
}
}

View File

@ -77,7 +77,6 @@ public class Game {
this.board = new Board();
this.startPosFen = this.board.getFen();
this.movelist = movelist;
for (Move move : movelist) {
@ -85,9 +84,6 @@ public class Game {
}
this.clock = new Clock("blitz");
// this.clockPlayer1 = new Clock();
// this.clockPlayer2 = new Clock();
}
/**
@ -136,7 +132,8 @@ public class Game {
public void quicksave() {
// TODO: save the current clocktime
this.savestate = new MoveList(this.movelist);
logger.info("Quicksaved");
logger.info("Quicksaved...");
}
/**
@ -156,7 +153,7 @@ public class Game {
this.playMove(move);
}
logger.info("Quickloaded");
logger.info("Quickloaded...");
}
}
@ -325,6 +322,12 @@ public class Game {
return this.viewPointer;
}
/**
* Loads the current view
*
* @brief Creates a new gameboard from the start pos and playes moves until it
* reaches the viewPointer
*/
public void loadView() {
this.board = new Board();
this.board.loadFromFen(this.startPosFen);

View File

@ -112,7 +112,20 @@ public class MainFrame extends JFrame {
setVisible(true);
}
/**
* Starts the spielframe and game in playmode
*/
public void startGame() {
if (this.game != null) {
this.game.stopClock();
SpielFrame sf = new SpielFrame(this.game);
}
}
/**
* Starts the spielframe and game in view mode
*/
public void startView() {
if (this.game != null) {
this.game.stopClock();
SpielFrame sf = new SpielFrame(this.game);
@ -125,11 +138,17 @@ public class MainFrame extends JFrame {
this.game = game;
}
/**
* opens the selectmodeframe
*/
private void openSelectModeFrame() {
ModeSelectionFrame ms = new ModeSelectionFrame(this);
new ModeSelectionFrame(this);
}
/**
* Opens the pgnselectorframe
*/
private void openPgnSelectFrame() {
PGNLoaderFrame pf = new PGNLoaderFrame(this);
new PGNLoaderFrame(this);
}
}

View File

@ -17,11 +17,17 @@ import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingWorker;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.github.bhlangonijr.chesslib.game.Game;
import com.github.bhlangonijr.chesslib.pgn.PgnHolder;
import com.github.bhlangonijr.chesslib.pgn.PgnLoadListener;
public class PGNLoaderFrame extends JFrame {
private static final Logger logger = LogManager.getLogger(PGNLoaderFrame.class);
private PgnHolder pgn;
private File selectedFile;
private List<Game> games;
@ -30,56 +36,61 @@ public class PGNLoaderFrame extends JFrame {
private JList<String> gameList;
private JProgressBar progressBar;
/**
* Opens the PNGLoaderFrame
*/
public PGNLoaderFrame(MainFrame mf) {
// ----- Style -----
setResizable(true);
setAlwaysOnTop(true);
setTitle("PGNLoader");
setBounds(100, 100, 500, 500);
// ----- contentPane -----
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
setContentPane(contentPane);
// ----- FileSelector -----
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();
}
fileSelectButton.addActionListener(e -> {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
selectedFile = fileChooser.getSelectedFile();
}
});
contentPane.add(fileSelectButton);
// ----- LoadButton -----
JButton loadPgnButton = new JButton("Load file");
loadPgnButton.addActionListener(e -> loadFile());
contentPane.add(loadPgnButton);
// ----- SelectionList -----
gameListModel = new DefaultListModel<>();
gameList = new JList<>(gameListModel);
gameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
gameList.setVisibleRowCount(5);
JScrollPane scrollPane = new JScrollPane(gameList);
contentPane.add(scrollPane);
// ----- ProgressBar -----
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
contentPane.add(progressBar);
// ----- StartButton -----
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();
mf.startView();
});
contentPane.add(startGameButton);
@ -93,11 +104,9 @@ public class PGNLoaderFrame extends JFrame {
LoadPGNWorker loadPGNWorker = new LoadPGNWorker();
loadPGNWorker.addPropertyChangeListener(e -> {
System.out.println(e.getNewValue());
progressBar.setIndeterminate(true);
});
progressBar.setIndeterminate(true);
pgn.getListener().add(loadPGNWorker);
loadPGNWorker.execute();
@ -117,7 +126,7 @@ public class PGNLoaderFrame extends JFrame {
publish(i);
}
} catch (Exception e) {
// TODO: handle exception
logger.info("Could not load pgn file!");
}
return pgn.getSize();
}

View File

@ -14,8 +14,8 @@ import de.mannheim.th.chess.utl.Clock;
import de.mannheim.th.chess.controller.ButtonAufgebenListener;
import de.mannheim.th.chess.controller.ButtonFileSaverListener;
import de.mannheim.th.chess.controller.ButtonMovePieceListener;
import de.mannheim.th.chess.controller.ButtonQuickloadListener;
import de.mannheim.th.chess.controller.ButtonQuicksaveListener;
import de.mannheim.th.chess.controller.controlPanel.ButtonQuickloadListener;
import de.mannheim.th.chess.controller.controlPanel.ButtonQuicksaveListener;
import de.mannheim.th.chess.controller.ButtonSelectPieceListener;
import de.mannheim.th.chess.controller.ButtonToNormalListener;
import de.mannheim.th.chess.controller.ButtonUndoMoveListener;
@ -218,6 +218,9 @@ public class SpielFrame extends JFrame {
}
}
/**
* Sets the to default buttons
*/
public void setDefaultButtons() {
this.clearButtons();
this.setDefaultBackground();
@ -326,6 +329,9 @@ public class SpielFrame extends JFrame {
return result[0];
}
/**
* Creates the controlPanel and its Buttons
*/
private JPanel createControlPanel() {
this.controlPanel = new JPanel();
this.controlPanel.setBackground(new Color(90, 90, 90));
@ -333,32 +339,40 @@ public class SpielFrame extends JFrame {
this.controlPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, controlPanel.getPreferredSize().height));
JButton viewFirstButton = new JButton("<<-");
JButton viewBackButton = new JButton("<-");
JButton viewForwardButton = new JButton("->");
JButton viewLastButton = new JButton("->>");
// ----- ViewQuicksaveButton -----
JButton quicksave = new JButton("Quicksave");
JButton quickload = new JButton("Quickload");
viewFirstButton.setEnabled(false);
viewBackButton.setEnabled(false);
viewForwardButton.setEnabled(false);
viewLastButton.setEnabled(false);
quicksave.setEnabled(true);
quickload.setEnabled(true);
viewFirstButton.addActionListener(new ButtonViewFirstListener(this.game, this));
viewBackButton.addActionListener(new ButtonViewBackListener(this.game, this));
viewForwardButton.addActionListener(new ButtonViewForwardListener(this.game, this));
viewLastButton.addActionListener(new ButtonViewLastListener(this.game, this));
quicksave.addActionListener(new ButtonQuicksaveListener(this.game));
quickload.addActionListener(new ButtonQuickloadListener(this.game, this));
this.controlPanel.add(quicksave);
// ----- ViewFirstButton -----
JButton viewFirstButton = new JButton("<<-");
viewFirstButton.setEnabled(false);
viewFirstButton.addActionListener(new ButtonViewFirstListener(this.game, this));
this.controlPanel.add(viewFirstButton);
// ----- ViewBackButton -----
JButton viewBackButton = new JButton("<-");
viewBackButton.setEnabled(false);
viewBackButton.addActionListener(new ButtonViewBackListener(this.game, this));
this.controlPanel.add(viewBackButton);
// ----- ViewForwardButton -----
JButton viewForwardButton = new JButton("->");
viewForwardButton.setEnabled(false);
viewForwardButton.addActionListener(new ButtonViewForwardListener(this.game, this));
this.controlPanel.add(viewForwardButton);
// ----- ViewLastButton -----
JButton viewLastButton = new JButton("->>");
viewLastButton.setEnabled(false);
viewLastButton.addActionListener(new ButtonViewLastListener(this.game, this));
this.controlPanel.add(viewLastButton);
// ----- ViewQuickloadButton -----
JButton quickload = new JButton("Quickload");
quickload.setEnabled(true);
quickload.addActionListener(new ButtonQuickloadListener(this.game, this));
this.controlPanel.add(quickload);
return controlPanel;
@ -587,6 +601,9 @@ public class SpielFrame extends JFrame {
this.mode = mode;
}
/**
* Inverts the Enabled property of the controlpanelButtons
*/
public void enableControlPanelButtons() {
for (Component c : this.controlPanel.getComponents()) {
if (c instanceof JButton) {
@ -595,6 +612,9 @@ public class SpielFrame extends JFrame {
}
}
/**
* Adds the buttons to the boardpanel
*/
public void applyBoardButtons() {
for (JButton b : buttons) {
panelLinks.add(b);