Merge branch 'buttonActions' of
https://gitty.informatik.hs-mannheim.de/3020772/Schach.git into buttonActionsbuttonActions
commit
22e5f10039
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,15 +1,11 @@
|
||||||
package de.mannheim.th.chess.ui;
|
package de.mannheim.th.chess.ui;
|
||||||
|
|
||||||
import java.awt.EventQueue;
|
|
||||||
|
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
import javax.swing.JTextField;
|
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
import java.awt.GridLayout;
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
@ -20,10 +16,8 @@ import javax.swing.BoxLayout;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JFileChooser;
|
import javax.swing.JFileChooser;
|
||||||
|
|
||||||
import java.awt.BorderLayout;
|
|
||||||
import java.awt.Component;
|
import java.awt.Component;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.awt.Graphics;
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
|
||||||
public class MainFrame extends JFrame {
|
public class MainFrame extends JFrame {
|
||||||
|
|
@ -33,22 +27,6 @@ public class MainFrame extends JFrame {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private JPanel contentPane;
|
private JPanel contentPane;
|
||||||
|
|
||||||
/**
|
|
||||||
* Launch the application.
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
EventQueue.invokeLater(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
MainFrame frame = new MainFrame();
|
|
||||||
frame.setVisible(true);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the frame.
|
* Create the frame.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,13 @@ import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import com.github.bhlangonijr.chesslib.Piece;
|
import com.github.bhlangonijr.chesslib.Piece;
|
||||||
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;
|
||||||
|
|
@ -22,59 +24,31 @@ 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 {
|
||||||
|
|
||||||
private static final Logger logger = LogManager.getLogger(App.class);
|
private static final Logger logger = LogManager.getLogger(App.class);
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private JPanel contentPane;
|
|
||||||
private ArrayList<JButton> buttons = new ArrayList<>();
|
private ArrayList<JButton> buttons = new ArrayList<>();
|
||||||
private HashMap<JButton, String> belegungen = new HashMap<>();
|
private HashMap<JButton, String> belegungen = new HashMap<>();
|
||||||
private JPanel panelLinks, panelRechts;
|
private JPanel panelLinks, panelRechts, contentPane;
|
||||||
private Game game;
|
private Game game;
|
||||||
private String symbolChoosed;
|
|
||||||
|
|
||||||
private BoardMode mode;
|
private BoardMode mode;
|
||||||
private Square selectedSquare;
|
private Square selectedSquare;
|
||||||
|
|
||||||
enum BoardMode {
|
public enum BoardMode {
|
||||||
normal, pieceSelected, finished
|
normal, pieceSelected, finished
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Launch the application. Die Main-Methode für den WindowBuilder.
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
EventQueue.invokeLater(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
SpielFrame frame = new SpielFrame();
|
|
||||||
frame.setVisible(true);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the frame.
|
* Create the frame.
|
||||||
*/
|
*/
|
||||||
|
|
@ -88,7 +62,7 @@ public class SpielFrame extends JFrame {
|
||||||
setTitle("Schach");
|
setTitle("Schach");
|
||||||
setAlwaysOnTop(true);
|
setAlwaysOnTop(true);
|
||||||
|
|
||||||
JPanel contentPane = new JPanel();
|
contentPane = new JPanel();
|
||||||
contentPane.setLayout(new BorderLayout());
|
contentPane.setLayout(new BorderLayout());
|
||||||
setContentPane(contentPane);
|
setContentPane(contentPane);
|
||||||
|
|
||||||
|
|
@ -111,10 +85,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();
|
||||||
|
|
@ -230,8 +216,6 @@ public class SpielFrame extends JFrame {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Switches the button actions depending on the boardmode
|
* Switches the button actions depending on the boardmode
|
||||||
*/
|
*/
|
||||||
|
|
@ -244,31 +228,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;
|
||||||
|
|
@ -278,18 +241,21 @@ 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) {
|
||||||
|
JButton b = buttons.get(mirrowedGrid(square.ordinal()));
|
||||||
|
final Move move = new Move(selectedSquare, square);
|
||||||
|
b.setEnabled(true);
|
||||||
|
b.setBackground(new Color(230, 100, 100));
|
||||||
|
b.addActionListener(new ButtonMovePieceListener(this, this.game, move));
|
||||||
|
}
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
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);
|
||||||
|
|
@ -322,8 +288,9 @@ public class SpielFrame extends JFrame {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
=======
|
||||||
break;
|
break;
|
||||||
|
>>>>>>> branch 'buttonActions' of https://gitty.informatik.hs-mannheim.de/3020772/Schach.git
|
||||||
|
|
||||||
case finished:
|
case finished:
|
||||||
clearButtons();
|
clearButtons();
|
||||||
|
|
@ -338,26 +305,13 @@ public class SpielFrame extends JFrame {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showDraw() {
|
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("1/2 - 1/2");
|
|
||||||
jl.setBounds(50, 30, 200, 25);
|
|
||||||
jl.setFont(new Font("Tahoma", Font.BOLD, 20));
|
|
||||||
frame.add(jl);
|
|
||||||
frame.setVisible(true);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private 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);
|
||||||
frame.setLayout(null);
|
frame.setLayout(null);
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
JLabel jl = new JLabel(String.format("%d - %d", player / 2, player % 2));
|
JLabel jl = new JLabel(String.format("%d - %d", player / 2, player % 2));
|
||||||
jl.setBounds(50, 30, 200, 25);
|
jl.setBounds(50, 30, 200, 25);
|
||||||
jl.setFont(new Font("Tahoma", Font.BOLD, 20));
|
jl.setFont(new Font("Tahoma", Font.BOLD, 20));
|
||||||
|
|
@ -395,5 +349,27 @@ public class SpielFrame extends JFrame {
|
||||||
return result[0];
|
return result[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
=======
|
||||||
|
JLabel jl = new JLabel("1/2 - 1/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) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
>>>>>>> branch 'buttonActions' of https://gitty.informatik.hs-mannheim.de/3020772/Schach.git
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Configuration status="WARN">
|
||||||
|
<Appenders>
|
||||||
|
<Console name="Console" target="SYSTEM_OUT">
|
||||||
|
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
||||||
|
</Console>
|
||||||
|
</Appenders>
|
||||||
|
<Loggers>
|
||||||
|
<Root level="info">
|
||||||
|
<AppenderRef ref="Console"/>
|
||||||
|
</Root>
|
||||||
|
</Loggers>
|
||||||
|
</Configuration>
|
||||||
Loading…
Reference in New Issue