diff --git a/src/main/java/de/mannheim/th/chess/domain/Game.java b/src/main/java/de/mannheim/th/chess/domain/Game.java index 9329aee..ca793db 100644 --- a/src/main/java/de/mannheim/th/chess/domain/Game.java +++ b/src/main/java/de/mannheim/th/chess/domain/Game.java @@ -16,7 +16,6 @@ import com.github.bhlangonijr.chesslib.move.MoveList; import com.github.bhlangonijr.chesslib.pgn.PgnHolder; import de.mannheim.th.chess.App; -import de.mannheim.th.chess.ui.SpielFrame; import de.mannheim.th.chess.utl.Clock; /** @@ -29,7 +28,6 @@ public class Game { private Board board; private Clock clock; - private SpielFrame sp; private String modus; private boolean rotieren, zuruecknahme; @@ -68,9 +66,6 @@ public class Game { this.movelist = new MoveList(); clock = new Clock(modus); - - sp = new SpielFrame(this); - } /** @@ -89,6 +84,8 @@ public class Game { this.board.doMove(move); } + this.clock = new Clock("blitz"); + // this.clockPlayer1 = new Clock(); // this.clockPlayer2 = new Clock(); } diff --git a/src/main/java/de/mannheim/th/chess/ui/MainFrame.java b/src/main/java/de/mannheim/th/chess/ui/MainFrame.java index 73c5010..dd69abf 100644 --- a/src/main/java/de/mannheim/th/chess/ui/MainFrame.java +++ b/src/main/java/de/mannheim/th/chess/ui/MainFrame.java @@ -8,6 +8,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import de.mannheim.th.chess.App; +import de.mannheim.th.chess.domain.Game; import javax.swing.JLabel; @@ -23,86 +24,109 @@ import java.awt.Font; import java.awt.Color; public class MainFrame extends JFrame { - - private static final Logger logger = LogManager.getLogger(App.class); - private static final long serialVersionUID = 1L; - private JPanel contentPane; + private static final Logger logger = LogManager.getLogger(App.class); - /** - * Create the frame. - */ - public MainFrame() { + private static final long serialVersionUID = 1L; + private JPanel contentPane; + private Game game; - setBackground(Color.LIGHT_GRAY); - setResizable(true); - setAlwaysOnTop(true); - setTitle("Schach"); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setBounds(100, 100, 500, 500); + /** + * Create the frame. + */ + public MainFrame() { - contentPane = new JPanel(); - contentPane.setBackground(new Color(90, 90, 90)); - contentPane.setForeground(Color.BLACK); - contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + setBackground(Color.LIGHT_GRAY); + setResizable(true); + setAlwaysOnTop(true); + setTitle("Schach"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 500, 500); - setContentPane(contentPane); + contentPane = new JPanel(); + contentPane.setBackground(new Color(90, 90, 90)); + contentPane.setForeground(Color.BLACK); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); - contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); + setContentPane(contentPane); - contentPane.add(Box.createVerticalStrut(10)); + contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); - JLabel lblNewLabel = new JLabel("Schach"); - lblNewLabel.setForeground(Color.BLACK); - lblNewLabel.setFont(new Font("Serif", Font.BOLD, 60)); - lblNewLabel.setAlignmentX(Component.CENTER_ALIGNMENT); - contentPane.add(lblNewLabel); + contentPane.add(Box.createVerticalStrut(10)); - contentPane.add(Box.createVerticalStrut(10)); + JLabel lblNewLabel = new JLabel("Schach"); + lblNewLabel.setForeground(Color.BLACK); + lblNewLabel.setFont(new Font("Serif", Font.BOLD, 60)); + lblNewLabel.setAlignmentX(Component.CENTER_ALIGNMENT); + contentPane.add(lblNewLabel); - JLabel lblNewLabel_1 = new JLabel("by Dominik, Marius und Matias"); - lblNewLabel_1.setFont(new Font("Calibri", Font.ITALIC, 24)); - lblNewLabel_1.setAlignmentX(Component.CENTER_ALIGNMENT); - contentPane.add(lblNewLabel_1); + contentPane.add(Box.createVerticalStrut(10)); - contentPane.add(Box.createVerticalStrut(75)); + JLabel lblNewLabel_1 = new JLabel("by Dominik, Marius und Matias"); + lblNewLabel_1.setFont(new Font("Calibri", Font.ITALIC, 24)); + lblNewLabel_1.setAlignmentX(Component.CENTER_ALIGNMENT); + contentPane.add(lblNewLabel_1); - JButton btnNewButton = new JButton("Neues Spiel starten"); - - btnNewButton.setBackground(Color.LIGHT_GRAY); - btnNewButton.setForeground(Color.BLACK); - btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 16)); - btnNewButton.setAlignmentX(Component.CENTER_ALIGNMENT); - btnNewButton.addActionListener(new ActionListener() { + contentPane.add(Box.createVerticalStrut(75)); - @Override - public void actionPerformed(ActionEvent e) { + JButton btnNewButton = new JButton("Neues Spiel starten"); - ModeSelectionFrame ms = new ModeSelectionFrame(); + btnNewButton.setBackground(Color.LIGHT_GRAY); + btnNewButton.setForeground(Color.BLACK); + btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 16)); + btnNewButton.setAlignmentX(Component.CENTER_ALIGNMENT); + btnNewButton.addActionListener(new ActionListener() { - } + @Override + public void actionPerformed(ActionEvent e) { + openSelectModeFrame(); + } - }); - contentPane.add(btnNewButton); + }); + contentPane.add(btnNewButton); - contentPane.add(Box.createVerticalStrut(15)); + contentPane.add(Box.createVerticalStrut(15)); - JButton btnNewButton_2 = new JButton("App beenden"); - - btnNewButton_2.setBackground(Color.LIGHT_GRAY); - btnNewButton_2.setForeground(Color.BLACK); - btnNewButton_2.setFont(new Font("Tahoma", Font.BOLD, 16)); - btnNewButton_2.setAlignmentX(Component.CENTER_ALIGNMENT); - btnNewButton_2.addActionListener(new ActionListener() { + JButton pgnLoaderButton = new JButton("Lade aus PGN Datei"); + pgnLoaderButton.addActionListener(e -> openPgnSelectFrame()); + contentPane.add(pgnLoaderButton); - @Override - public void actionPerformed(ActionEvent e) { - System.exit(0); - } + contentPane.add(Box.createVerticalStrut(15)); - }); + JButton btnNewButton_2 = new JButton("App beenden"); - contentPane.add(btnNewButton_2); - setVisible(true); - } + btnNewButton_2.setBackground(Color.LIGHT_GRAY); + btnNewButton_2.setForeground(Color.BLACK); + btnNewButton_2.setFont(new Font("Tahoma", Font.BOLD, 16)); + btnNewButton_2.setAlignmentX(Component.CENTER_ALIGNMENT); + btnNewButton_2.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + + }); + + contentPane.add(btnNewButton_2); + 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); + } } diff --git a/src/main/java/de/mannheim/th/chess/ui/ModeSelectionFrame.java b/src/main/java/de/mannheim/th/chess/ui/ModeSelectionFrame.java index fb15899..3b19d97 100644 --- a/src/main/java/de/mannheim/th/chess/ui/ModeSelectionFrame.java +++ b/src/main/java/de/mannheim/th/chess/ui/ModeSelectionFrame.java @@ -20,118 +20,120 @@ import de.mannheim.th.chess.controller.ButtonFileLoaderListener; import de.mannheim.th.chess.domain.Game; public class ModeSelectionFrame extends JFrame { - - private static final Logger logger = LogManager.getLogger(App.class); - private static final long serialVersionUID = 1L; - private final JPanel contentPane; - private final ArrayList spiele = new ArrayList<>(); - private String fen; + private static final Logger logger = LogManager.getLogger(App.class); - public ModeSelectionFrame() { - // Frame-Eigenschaften - setTitle("Modusauswahl"); - setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - setBounds(100, 100, 500, 500); - setResizable(true); - setAlwaysOnTop(true); + private static final long serialVersionUID = 1L; + private final JPanel contentPane; + private final ArrayList spiele = new ArrayList<>(); + private String fen; - // Panel konfigurieren - contentPane = new JPanel(); - contentPane.setBackground(new Color(90, 90, 90)); - contentPane.setBorder(new EmptyBorder(20, 20, 20, 20)); - contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); - setContentPane(contentPane); + public ModeSelectionFrame(MainFrame mf) { + // Frame-Eigenschaften + setTitle("Modusauswahl"); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + setBounds(100, 100, 500, 500); + setResizable(true); + setAlwaysOnTop(true); - // Überschrift - JLabel jl = new JLabel("Welchen Modus wollen Sie spielen?"); - jl.setFont(new Font("Calibri", Font.BOLD, 20)); - jl.setForeground(Color.BLACK); - jl.setAlignmentX(Component.CENTER_ALIGNMENT); - contentPane.add(jl); - contentPane.add(Box.createVerticalStrut(15)); + // Panel konfigurieren + contentPane = new JPanel(); + contentPane.setBackground(new Color(90, 90, 90)); + contentPane.setBorder(new EmptyBorder(20, 20, 20, 20)); + contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); + setContentPane(contentPane); - // Modusauswahl - String[] modi = {"Blitz", "Schnellschach", "Klassisch"}; - JComboBox jcb1 = new JComboBox<>(modi); - jcb1 .setMaximumSize(new Dimension(150, 30)); - jcb1 .setAlignmentX(Component.CENTER_ALIGNMENT); - contentPane.add(jcb1 ); - contentPane.add(Box.createVerticalStrut(15)); + // Überschrift + JLabel jl = new JLabel("Welchen Modus wollen Sie spielen?"); + jl.setFont(new Font("Calibri", Font.BOLD, 20)); + jl.setForeground(Color.BLACK); + jl.setAlignmentX(Component.CENTER_ALIGNMENT); + contentPane.add(jl); + contentPane.add(Box.createVerticalStrut(15)); - // Spielbrettdrehen - JLabel jl2 = new JLabel("Soll das Spielbrett nach jedem Zug gedreht werden?"); - jl2 .setFont(new Font("Calibri", Font.BOLD, 20)); - jl2 .setForeground(Color.BLACK); - jl2 .setAlignmentX(Component.CENTER_ALIGNMENT); - contentPane.add(jl2 ); + // Modusauswahl + String[] modi = { "Blitz", "Schnellschach", "Klassisch" }; + JComboBox jcb1 = new JComboBox<>(modi); + jcb1.setMaximumSize(new Dimension(150, 30)); + jcb1.setAlignmentX(Component.CENTER_ALIGNMENT); + contentPane.add(jcb1); + contentPane.add(Box.createVerticalStrut(15)); - JCheckBox jb1 = new JCheckBox(); - jb1.setOpaque(false); - jb1.setFocusPainted(false); - jb1.setForeground(Color.BLACK); - jb1 .setAlignmentX(Component.CENTER_ALIGNMENT); - jb1 .setMaximumSize(new Dimension(30, 30)); - contentPane.add(jb1 ); - contentPane.add(Box.createVerticalStrut(15)); + // Spielbrettdrehen + JLabel jl2 = new JLabel("Soll das Spielbrett nach jedem Zug gedreht werden?"); + jl2.setFont(new Font("Calibri", Font.BOLD, 20)); + jl2.setForeground(Color.BLACK); + jl2.setAlignmentX(Component.CENTER_ALIGNMENT); + contentPane.add(jl2); - // Zurücknahmeoption - JLabel jl3 = new JLabel("Sollen Zurücknahmen erlaubt sein?"); - jl3.setFont(new Font("Calibri", Font.BOLD, 20)); - jl3.setForeground(Color.BLACK); - jl3.setAlignmentX(Component.CENTER_ALIGNMENT); - contentPane.add(jl3); + JCheckBox jb1 = new JCheckBox(); + jb1.setOpaque(false); + jb1.setFocusPainted(false); + jb1.setForeground(Color.BLACK); + jb1.setAlignmentX(Component.CENTER_ALIGNMENT); + jb1.setMaximumSize(new Dimension(30, 30)); + contentPane.add(jb1); + contentPane.add(Box.createVerticalStrut(15)); - JCheckBox jb2 = new JCheckBox(); - jb2.setOpaque(false); - jb2.setFocusPainted(false); - jb2.setForeground(Color.BLACK); - jb2.setAlignmentX(Component.CENTER_ALIGNMENT); - jb2.setMaximumSize(new Dimension(30, 30)); - contentPane.add(jb2); - - contentPane.add(Box.createVerticalStrut(15)); + // Zurücknahmeoption + JLabel jl3 = new JLabel("Sollen Zurücknahmen erlaubt sein?"); + jl3.setFont(new Font("Calibri", Font.BOLD, 20)); + jl3.setForeground(Color.BLACK); + jl3.setAlignmentX(Component.CENTER_ALIGNMENT); + contentPane.add(jl3); - JButton btnNewButton_1 = new JButton("Vergangenes Spiel laden"); - - btnNewButton_1.setBackground(Color.LIGHT_GRAY); - btnNewButton_1.setForeground(Color.BLACK); - btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 16)); - btnNewButton_1.setAlignmentX(Component.CENTER_ALIGNMENT); - btnNewButton_1.addActionListener(new ButtonFileLoaderListener(this)); + JCheckBox jb2 = new JCheckBox(); + jb2.setOpaque(false); + jb2.setFocusPainted(false); + jb2.setForeground(Color.BLACK); + jb2.setAlignmentX(Component.CENTER_ALIGNMENT); + jb2.setMaximumSize(new Dimension(30, 30)); + contentPane.add(jb2); - contentPane.add(btnNewButton_1); - - contentPane.add(Box.createVerticalStrut(25)); + contentPane.add(Box.createVerticalStrut(15)); - // Spiel starten Button - JButton btnNewButton = new JButton("Spiel starten"); - btnNewButton .setBackground(Color.LIGHT_GRAY); - btnNewButton .setForeground(Color.BLACK); - btnNewButton .setFont(new Font("Tahoma", Font.BOLD, 16)); - btnNewButton .setAlignmentX(Component.CENTER_ALIGNMENT); - contentPane.add(btnNewButton ); + JButton btnNewButton_1 = new JButton("Vergangenes Spiel laden"); - // Button-Listener - btnNewButton .addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - String modus = (String) jcb1.getSelectedItem(); - boolean rotieren = jb1.isSelected(); - boolean zuruecknahme = jb2.isSelected(); + btnNewButton_1.setBackground(Color.LIGHT_GRAY); + btnNewButton_1.setForeground(Color.BLACK); + btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 16)); + btnNewButton_1.setAlignmentX(Component.CENTER_ALIGNMENT); + btnNewButton_1.addActionListener(new ButtonFileLoaderListener(this)); - Game game = new Game(modus, rotieren, zuruecknahme, fen); - - spiele.add(game); + contentPane.add(btnNewButton_1); - dispose(); - } - }); + contentPane.add(Box.createVerticalStrut(25)); - setVisible(true); - } - - public void setFen(String fen) { - this.fen = fen; - } + // Spiel starten Button + JButton btnNewButton = new JButton("Spiel starten"); + btnNewButton.setBackground(Color.LIGHT_GRAY); + btnNewButton.setForeground(Color.BLACK); + btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 16)); + btnNewButton.setAlignmentX(Component.CENTER_ALIGNMENT); + contentPane.add(btnNewButton); + + // Button-Listener + btnNewButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String modus = (String) jcb1.getSelectedItem(); + boolean rotieren = jb1.isSelected(); + boolean zuruecknahme = jb2.isSelected(); + + Game game = new Game(modus, rotieren, zuruecknahme, fen); + mf.setGame(game); + mf.startGame(); + + // spiele.add(game); + + dispose(); + } + }); + + setVisible(true); + } + + public void setFen(String fen) { + this.fen = fen; + } } diff --git a/src/main/java/de/mannheim/th/chess/ui/PGNLoaderFrame.java b/src/main/java/de/mannheim/th/chess/ui/PGNLoaderFrame.java new file mode 100644 index 0000000..c036047 --- /dev/null +++ b/src/main/java/de/mannheim/th/chess/ui/PGNLoaderFrame.java @@ -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 games; + private DefaultListModel gameListModel; + private JPanel contentPane; + private JList 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 + } + + } + } + +}