diff --git a/src/main/java/de/mannheim/th/chess/controller/ButtonAufgebenListener.java b/src/main/java/de/mannheim/th/chess/controller/ButtonAufgebenListener.java index cd14562..0dfd08f 100644 --- a/src/main/java/de/mannheim/th/chess/controller/ButtonAufgebenListener.java +++ b/src/main/java/de/mannheim/th/chess/controller/ButtonAufgebenListener.java @@ -5,17 +5,49 @@ import java.awt.event.ActionListener; import javax.swing.JFrame; -public class ButtonAufgebenListener extends JFrame implements ActionListener{ - - private static final long serialVersionUID = 1L; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; - public ButtonAufgebenListener() { - +import de.mannheim.th.chess.App; +import de.mannheim.th.chess.domain.Game; +import de.mannheim.th.chess.ui.SpielFrame; +import de.mannheim.th.chess.ui.SpielFrame.BoardMode; + +public class ButtonAufgebenListener extends JFrame implements ActionListener { + + private static final Logger logger = LogManager.getLogger(App.class); + + private static final long serialVersionUID = 1L; + private SpielFrame sf; + private Game g; + + public ButtonAufgebenListener(SpielFrame sf, Game g) { + this.sf = sf; + this.g = g; } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub + + Object source = e.getSource(); + + g.stopClock(); + + // weil nur der aktive Spieler button drücken darf + if (source == sf.getAufgeben()) { + logger.info("Spieler 1 will aufgeben."); + sf.getAufgeben2().setEnabled(false); + sf.showResult("Spieler 2 hat gewonnen!"); + } else if (source == sf.getAufgeben2()) { + logger.info("Spieler 2 will aufgeben."); + sf.getAufgeben().setEnabled(false); + sf.showResult("Spieler 1 hat gewonnen!"); + } + + this.sf.setBoardMode(BoardMode.finished); + + sf.setButtonsActions(); } diff --git a/src/main/java/de/mannheim/th/chess/controller/ButtonMovePieceListener.java b/src/main/java/de/mannheim/th/chess/controller/ButtonMovePieceListener.java index 9fd4204..4468265 100644 --- a/src/main/java/de/mannheim/th/chess/controller/ButtonMovePieceListener.java +++ b/src/main/java/de/mannheim/th/chess/controller/ButtonMovePieceListener.java @@ -33,12 +33,12 @@ public class ButtonMovePieceListener implements ActionListener { this.game.stopClock(); this.sf.setBoardMode(BoardMode.finished); this.sf.enableControlPanelButtons(); - this.sf.showDraw(); + this.sf.showResult("Unentschieden!"); } else if (this.game.isMate()) { this.game.stopClock(); this.sf.setBoardMode(BoardMode.finished); this.sf.enableControlPanelButtons(); - this.sf.showWin(game.getActivePlayer()); + this.sf.showResult("Spieler "+game.getActivePlayer()+" hat gewonnen!"); } else { this.sf.setBoardMode(BoardMode.normal); } diff --git a/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java b/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java index 97681e0..445c4ce 100644 --- a/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java +++ b/src/main/java/de/mannheim/th/chess/ui/SpielFrame.java @@ -3,7 +3,6 @@ package de.mannheim.th.chess.ui; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import com.github.bhlangonijr.chesslib.Piece; import com.github.bhlangonijr.chesslib.Square; import com.github.bhlangonijr.chesslib.move.Move; import com.github.bhlangonijr.chesslib.move.MoveList; @@ -57,7 +56,7 @@ public class SpielFrame extends JFrame { private ArrayList buttons = new ArrayList<>(); private HashMap belegungen = new HashMap<>(); private JPanel panelLinks, panelRechts, contentPane, controlPanel; - private JButton undo, undo2; + private JButton undo, undo2, aufgeben, aufgeben2; private JTextArea ausgabe; private Game game; private Clock clock; @@ -226,10 +225,10 @@ public class SpielFrame extends JFrame { this.setDefaultBackground(); } - /* - * Switches the button actions depending on the boardmode - */ - private void setButtonsActions() { + /* + * Switches the button actions depending on the boardmode + */ + private void setButtonsActions() { List selectables; @@ -273,20 +272,10 @@ public class SpielFrame extends JFrame { } - } - - public void showDraw() { - JFrame frame = new JFrame("Result"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setSize(300, 150); - frame.setLayout(null); - - // JLabel jl = new JLabel(String.format("%d - %d", player / 2, player % 2)); - // jl.setBounds(50, 30, 200, 25); - // jl.setFont(new Font("Tahoma", Font.BOLD, 20)); - // frame.add(jl); - // frame.setVisible(true); - } + for (JButton b : buttons) { + panelLinks.add(b); + } + } public void showWin(int player) { JFrame frame = new JFrame("Result"); @@ -294,12 +283,20 @@ public class SpielFrame extends JFrame { frame.setSize(300, 150); frame.setLayout(null); - JLabel jl = new JLabel(String.format("%d - %d", player / 2, player % 2)); - jl.setBounds(50, 30, 200, 25); - jl.setFont(new Font("Tahoma", Font.BOLD, 20)); - frame.add(jl); - frame.setVisible(true); - } + JLabel jl = new JLabel(String.format("%d - %d", player / 2, player % 2)); + jl.setBounds(50, 30, 200, 25); + jl.setFont(new Font("Tahoma", Font.BOLD, 20)); + frame.add(jl); + frame.setVisible(true); + } + + public void showResult(String res) { + + ausgabe.setFont(new Font("Calibri", Font.BOLD, 40)); + ausgabe.setForeground(new Color(178, 34, 34)); + ausgabe.setText(" "+res); + + } public int showPromotion() { final int[] result = { -1 }; @@ -419,15 +416,14 @@ public class SpielFrame extends JFrame { aufgebenUndo.add(Box.createHorizontalStrut(10)); - JButton aufgeben = new JButton("Aufgeben"); - aufgeben.setBackground(Color.LIGHT_GRAY); - aufgeben.setForeground(Color.BLACK); - aufgeben.setFont(new Font("Tahoma", Font.BOLD, 16)); - aufgeben.setAlignmentX(Component.CENTER_ALIGNMENT); - aufgebenUndo.add(aufgeben); - - // Button-Listener - aufgeben.addActionListener(new ButtonAufgebenListener()); + aufgeben2 = new JButton("Aufgeben"); + aufgeben2.setBackground(Color.LIGHT_GRAY); + aufgeben2.setForeground(Color.BLACK); + aufgeben2.setFont(new Font("Tahoma", Font.BOLD, 16)); + aufgeben2.setAlignmentX(Component.CENTER_ALIGNMENT); + + aufgeben2.addActionListener(new ButtonAufgebenListener(this, this.game)); + aufgebenUndo.add(aufgeben2); aufgebenUndo.add(Box.createHorizontalStrut(10)); @@ -528,15 +524,14 @@ public class SpielFrame extends JFrame { aufgebenUndo.add(Box.createHorizontalStrut(10)); - JButton aufgeben = new JButton("Aufgeben"); - aufgeben.setBackground(Color.LIGHT_GRAY); - aufgeben.setForeground(Color.BLACK); - aufgeben.setFont(new Font("Tahoma", Font.BOLD, 16)); - aufgeben.setAlignmentX(Component.CENTER_ALIGNMENT); - aufgebenUndo.add(aufgeben); - - // Button-Listener - aufgeben.addActionListener(new ButtonAufgebenListener()); + aufgeben = new JButton("Aufgeben"); + aufgeben.setBackground(Color.LIGHT_GRAY); + aufgeben.setForeground(Color.BLACK); + aufgeben.setFont(new Font("Tahoma", Font.BOLD, 16)); + aufgeben.setAlignmentX(Component.CENTER_ALIGNMENT); + aufgeben.addActionListener(new ButtonAufgebenListener(this, this.game)); + + aufgebenUndo.add(aufgeben); aufgebenUndo.add(Box.createHorizontalStrut(10)); @@ -621,4 +616,60 @@ public class SpielFrame extends JFrame { } } + public JButton getAufgeben() { + return aufgeben; + } + + public void setAufgeben(JButton aufgeben) { + this.aufgeben = aufgeben; + } + + public JButton getAufgeben2() { + return aufgeben2; + } + + public void setAufgeben2(JButton aufgeben2) { + this.aufgeben2 = aufgeben2; + } + + public void setMode(BoardMode mode) { + this.mode = mode; + } + + /** + * Inverts the Enabled property of the controlpanelButtons + */ + public void enableControlPanelButtons() { + for (Component c : this.controlPanel.getComponents()) { + if (c instanceof JButton) { + c.setEnabled(!c.isEnabled()); + } + } + } + + /** + * Adds the buttons to the boardpanel + */ + public void applyBoardButtons() { + for (JButton b : buttons) { + panelLinks.add(b); + } + } + + public JButton getAufgeben() { + return aufgeben; + } + + public void setAufgeben(JButton aufgeben) { + this.aufgeben = aufgeben; + } + + public JButton getAufgeben2() { + return aufgeben2; + } + + public void setAufgeben2(JButton aufgeben2) { + this.aufgeben2 = aufgeben2; + } + }