move Actionlisensers to controller package
parent
392d704729
commit
12623b5b8f
|
|
@ -0,0 +1,39 @@
|
||||||
|
package de.mannheim.th.chess.controller;
|
||||||
|
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
import com.github.bhlangonijr.chesslib.move.Move;
|
||||||
|
|
||||||
|
import de.mannheim.th.chess.domain.Game;
|
||||||
|
import de.mannheim.th.chess.ui.SpielFrame;
|
||||||
|
import de.mannheim.th.chess.ui.SpielFrame.BoardMode;
|
||||||
|
|
||||||
|
public class ButtonMovePieceListener implements ActionListener {
|
||||||
|
private SpielFrame sf;
|
||||||
|
private Game game;
|
||||||
|
private Move mv;
|
||||||
|
|
||||||
|
public ButtonMovePieceListener(SpielFrame sf, Game game, Move mv) {
|
||||||
|
this.sf = sf;
|
||||||
|
this.game = game;
|
||||||
|
this.mv = mv;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
this.game.playMove(this.mv);
|
||||||
|
if (this.game.isDraw()) {
|
||||||
|
this.game.stopClock();
|
||||||
|
this.sf.setBoardMode(BoardMode.finished);
|
||||||
|
this.sf.showDraw();
|
||||||
|
} else if (this.game.isMate()) {
|
||||||
|
this.game.stopClock();
|
||||||
|
this.sf.setBoardMode(BoardMode.finished);
|
||||||
|
this.sf.showWin(game.getActivePlayer());
|
||||||
|
}
|
||||||
|
this.sf.setBoardMode(BoardMode.normal);
|
||||||
|
this.sf.setCursor(null);
|
||||||
|
this.sf.erstelleBrett();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
package de.mannheim.th.chess.controller;
|
||||||
|
|
||||||
|
import java.awt.Cursor;
|
||||||
|
import java.awt.Image;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
import com.github.bhlangonijr.chesslib.Square;
|
||||||
|
|
||||||
|
import de.mannheim.th.chess.ui.SpielFrame;
|
||||||
|
|
||||||
|
public class ButtonSelectPieceListener implements ActionListener {
|
||||||
|
private SpielFrame sf;
|
||||||
|
private Square selectedSquare;
|
||||||
|
|
||||||
|
public ButtonSelectPieceListener(SpielFrame sf, Square sq) {
|
||||||
|
this.sf = sf;
|
||||||
|
this.selectedSquare = sq;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
sf.setBoardMode(SpielFrame.BoardMode.pieceSelected);
|
||||||
|
sf.setSelectedSquare(this.selectedSquare);
|
||||||
|
|
||||||
|
String symbolChoosed = sf.getBelegung().get(e.getSource());
|
||||||
|
|
||||||
|
// setzt cursor auf spielfigur für die animation
|
||||||
|
String pfad = "src/main/resources/" + (int) symbolChoosed.toCharArray()[2] + ".png";
|
||||||
|
|
||||||
|
// Bild laden und Cursor im gesamten Frame setzen
|
||||||
|
Image image = Toolkit.getDefaultToolkit().getImage(pfad);
|
||||||
|
Image scaled = image.getScaledInstance(32, 32, Image.SCALE_SMOOTH);
|
||||||
|
Cursor figurCursor = Toolkit.getDefaultToolkit().createCustomCursor(scaled, new Point(0, 0),
|
||||||
|
"figurCursor");
|
||||||
|
sf.setCursor(figurCursor);
|
||||||
|
|
||||||
|
sf.erstelleBrett();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
package de.mannheim.th.chess.controller;
|
||||||
|
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
import de.mannheim.th.chess.ui.SpielFrame;
|
||||||
|
import de.mannheim.th.chess.ui.SpielFrame.BoardMode;
|
||||||
|
|
||||||
|
public class ButtonToNormalListener implements ActionListener {
|
||||||
|
private SpielFrame sf;
|
||||||
|
|
||||||
|
public ButtonToNormalListener(SpielFrame sf) {
|
||||||
|
this.sf = sf;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
this.sf.setBoardMode(BoardMode.normal);
|
||||||
|
this.sf.setSelectedSquare(null);
|
||||||
|
this.sf.setCursor(null);
|
||||||
|
this.sf.erstelleBrett();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -4,11 +4,13 @@ import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import com.github.bhlangonijr.chesslib.Square;
|
import com.github.bhlangonijr.chesslib.Square;
|
||||||
import com.github.bhlangonijr.chesslib.game.Player;
|
|
||||||
import com.github.bhlangonijr.chesslib.move.Move;
|
import com.github.bhlangonijr.chesslib.move.Move;
|
||||||
|
|
||||||
import de.mannheim.th.chess.App;
|
import de.mannheim.th.chess.App;
|
||||||
import de.mannheim.th.chess.domain.Game;
|
import de.mannheim.th.chess.domain.Game;
|
||||||
|
import de.mannheim.th.chess.controller.ButtonMovePieceListener;
|
||||||
|
import de.mannheim.th.chess.controller.ButtonSelectPieceListener;
|
||||||
|
import de.mannheim.th.chess.controller.ButtonToNormalListener;
|
||||||
|
|
||||||
import java.awt.EventQueue;
|
import java.awt.EventQueue;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
|
|
@ -20,23 +22,13 @@ import javax.swing.JFrame;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.JSplitPane;
|
import javax.swing.JSplitPane;
|
||||||
import javax.swing.JTextArea;
|
|
||||||
import javax.swing.JTextField;
|
|
||||||
|
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Cursor;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.awt.GridLayout;
|
import java.awt.GridLayout;
|
||||||
import java.awt.Image;
|
|
||||||
import java.awt.Point;
|
|
||||||
import java.awt.Toolkit;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
|
|
||||||
public class SpielFrame extends JFrame {
|
public class SpielFrame extends JFrame {
|
||||||
|
|
||||||
|
|
@ -53,7 +45,7 @@ public class SpielFrame extends JFrame {
|
||||||
private BoardMode mode;
|
private BoardMode mode;
|
||||||
private Square selectedSquare;
|
private Square selectedSquare;
|
||||||
|
|
||||||
enum BoardMode {
|
public enum BoardMode {
|
||||||
normal, pieceSelected, finished
|
normal, pieceSelected, finished
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -109,10 +101,22 @@ public class SpielFrame extends JFrame {
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setBoardMode(BoardMode bm) {
|
||||||
|
this.mode = bm;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSelectedSquare(Square sq) {
|
||||||
|
this.selectedSquare = sq;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashMap<JButton, String> getBelegung() {
|
||||||
|
return this.belegungen;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Erstellt alle Buttons und fügt sie dem Frame hinzu.
|
* Erstellt alle Buttons und fügt sie dem Frame hinzu.
|
||||||
*/
|
*/
|
||||||
private void erstelleBrett() {
|
public void erstelleBrett() {
|
||||||
|
|
||||||
this.clearButtons();
|
this.clearButtons();
|
||||||
this.setDefaultBackground();
|
this.setDefaultBackground();
|
||||||
|
|
@ -228,8 +232,6 @@ public class SpielFrame extends JFrame {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Switches the button actions depending on the boardmode
|
* Switches the button actions depending on the boardmode
|
||||||
*/
|
*/
|
||||||
|
|
@ -242,31 +244,10 @@ public class SpielFrame extends JFrame {
|
||||||
selectables = game.getAllLegalMoveableSquares();
|
selectables = game.getAllLegalMoveableSquares();
|
||||||
|
|
||||||
for (Square square : selectables) {
|
for (Square square : selectables) {
|
||||||
final Square currentSquare = square; // ActionListener need it to be final
|
|
||||||
JButton b = buttons.get(mirrowedGrid(square.ordinal()));
|
JButton b = buttons.get(mirrowedGrid(square.ordinal()));
|
||||||
b.setEnabled(true);
|
b.setEnabled(true);
|
||||||
// b.setBackground(Color.green);
|
// b.setBackground(Color.green);
|
||||||
b.addActionListener(new ActionListener() {
|
b.addActionListener(new ButtonSelectPieceListener(this, square));
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
mode = BoardMode.pieceSelected;
|
|
||||||
selectedSquare = currentSquare;
|
|
||||||
|
|
||||||
symbolChoosed = belegungen.get(b);
|
|
||||||
|
|
||||||
// setzt cursor auf spielfigur für die animation
|
|
||||||
String pfad = "src/main/resources/" + (int) symbolChoosed.toCharArray()[2] + ".png";
|
|
||||||
|
|
||||||
// Bild laden und Cursor im gesamten Frame setzen
|
|
||||||
Image image = Toolkit.getDefaultToolkit().getImage(pfad);
|
|
||||||
Image scaled = image.getScaledInstance(32, 32, Image.SCALE_SMOOTH);
|
|
||||||
Cursor figurCursor = Toolkit.getDefaultToolkit().createCustomCursor(scaled, new Point(0, 0),
|
|
||||||
"figurCursor");
|
|
||||||
setCursor(figurCursor);
|
|
||||||
|
|
||||||
erstelleBrett();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
@ -276,41 +257,18 @@ public class SpielFrame extends JFrame {
|
||||||
JButton s = buttons.get(mirrowedGrid(selectedSquare.ordinal()));
|
JButton s = buttons.get(mirrowedGrid(selectedSquare.ordinal()));
|
||||||
s.setEnabled(true);
|
s.setEnabled(true);
|
||||||
s.setBackground(new Color(165, 42, 42));
|
s.setBackground(new Color(165, 42, 42));
|
||||||
s.addActionListener(new ActionListener() {
|
s.addActionListener(new ButtonToNormalListener(this)); // cancel action
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
mode = BoardMode.normal;
|
|
||||||
selectedSquare = null;
|
|
||||||
setCursor(null);
|
|
||||||
erstelleBrett();
|
|
||||||
}
|
|
||||||
}); // cancel action
|
|
||||||
|
|
||||||
selectables = game.getLegalMoveableSquares(selectedSquare);
|
selectables = game.getLegalMoveableSquares(selectedSquare);
|
||||||
|
|
||||||
for (Square square : selectables) {
|
for (
|
||||||
|
|
||||||
|
Square square : selectables) {
|
||||||
JButton b = buttons.get(mirrowedGrid(square.ordinal()));
|
JButton b = buttons.get(mirrowedGrid(square.ordinal()));
|
||||||
final Move move = new Move(selectedSquare, square);
|
final Move move = new Move(selectedSquare, square);
|
||||||
b.setEnabled(true);
|
b.setEnabled(true);
|
||||||
b.setBackground(new Color(230, 100, 100));
|
b.setBackground(new Color(230, 100, 100));
|
||||||
b.addActionListener(new ActionListener() {
|
b.addActionListener(new ButtonMovePieceListener(this, this.game, move));
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
game.playMove(move);
|
|
||||||
if (game.isDraw()) {
|
|
||||||
game.stopClock();
|
|
||||||
mode = BoardMode.finished;
|
|
||||||
showDraw();
|
|
||||||
} else if (game.isMate()) {
|
|
||||||
game.stopClock();
|
|
||||||
mode = BoardMode.finished;
|
|
||||||
showWin(game.getActivePlayer());
|
|
||||||
}
|
|
||||||
mode = BoardMode.normal;
|
|
||||||
setCursor(null);
|
|
||||||
erstelleBrett();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
@ -328,7 +286,7 @@ public class SpielFrame extends JFrame {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showDraw() {
|
public void showDraw() {
|
||||||
JFrame frame = new JFrame("Result");
|
JFrame frame = new JFrame("Result");
|
||||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
frame.setSize(300, 150);
|
frame.setSize(300, 150);
|
||||||
|
|
@ -342,7 +300,7 @@ public class SpielFrame extends JFrame {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showWin(int player) {
|
public void showWin(int player) {
|
||||||
JFrame frame = new JFrame("Result");
|
JFrame frame = new JFrame("Result");
|
||||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
frame.setSize(300, 150);
|
frame.setSize(300, 150);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue