Aufgeben, Draw und Mate sollten jetzt in Ausgabe angezeigt werden.

devButtonAufgeben
Your Name 2025-06-24 14:56:17 +02:00
parent 596a3a4dea
commit cd89c3b131
3 changed files with 95 additions and 37 deletions

View File

@ -5,18 +5,50 @@ import java.awt.event.ActionListener;
import javax.swing.JFrame;
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 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() {
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();
}
}

View File

@ -29,12 +29,16 @@ public class ButtonMovePieceListener implements ActionListener {
if (this.game.isDraw()) {
this.game.stopClock();
this.sf.setBoardMode(BoardMode.finished);
this.sf.showDraw();
this.sf.showResult("Unentschieden!");
this.sf.setBoardMode(BoardMode.gameEnd);
} else if (this.game.isMate()) {
this.game.stopClock();
this.sf.setBoardMode(BoardMode.finished);
this.sf.showWin(game.getActivePlayer());
this.sf.showResult("Spieler "+game.getActivePlayer()+" hat gewonnen!");
this.sf.setBoardMode(BoardMode.gameEnd);
}
this.sf.setBoardMode(BoardMode.normal);
this.sf.setCursor(null);

View File

@ -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;
@ -50,7 +49,7 @@ public class SpielFrame extends JFrame {
private ArrayList<JButton> buttons = new ArrayList<>();
private HashMap<JButton, String> belegungen = new HashMap<>();
private JPanel panelLinks, panelRechts, contentPane;
private JButton undo, undo2;
private JButton undo, undo2, aufgeben, aufgeben2;
private JTextArea ausgabe;
private Game game;
private Clock clock;
@ -60,7 +59,7 @@ public class SpielFrame extends JFrame {
private Square selectedSquare;
public enum BoardMode {
normal, pieceSelected, finished
normal, pieceSelected, finished, gameEnd
}
/**
@ -190,6 +189,7 @@ public class SpielFrame extends JFrame {
buttons.add(b);
}
}
/**
@ -211,7 +211,7 @@ public class SpielFrame extends JFrame {
/*
* Switches the button actions depending on the boardmode
*/
private void setButtonsActions() {
public void setButtonsActions() {
List<Square> selectables;
@ -248,6 +248,14 @@ public class SpielFrame extends JFrame {
break;
case finished:
clearButtons();
break;
case gameEnd:
//alle Eingabemöglichkeiten deaktivieren
panelLinks.setEnabled(false);
break;
default:
break;
@ -260,16 +268,8 @@ 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);
}
public void showWin(int player) {
@ -285,6 +285,14 @@ public class SpielFrame extends JFrame {
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 };
@ -354,15 +362,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);
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);
// Button-Listener
aufgeben.addActionListener(new ButtonAufgebenListener());
aufgeben2.addActionListener(new ButtonAufgebenListener(this, this.game));
aufgebenUndo.add(aufgeben2);
aufgebenUndo.add(Box.createHorizontalStrut(10));
@ -463,15 +470,14 @@ public class SpielFrame extends JFrame {
aufgebenUndo.add(Box.createHorizontalStrut(10));
JButton aufgeben = new JButton("Aufgeben");
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);
aufgeben.addActionListener(new ButtonAufgebenListener(this, this.game));
// Button-Listener
aufgeben.addActionListener(new ButtonAufgebenListener());
aufgebenUndo.add(aufgeben);
aufgebenUndo.add(Box.createHorizontalStrut(10));
@ -532,4 +538,20 @@ public class SpielFrame extends JFrame {
return clock;
}
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;
}
}