From 6273261fd0ef56d956e8865f836de47991aa8475 Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 23 Jun 2025 02:40:30 +0200 Subject: [PATCH] Added methods to PgnGui class --- .../informatik/chess/view/PgnGui.java | 270 +++++++++++++++++- 1 file changed, 269 insertions(+), 1 deletion(-) diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/view/PgnGui.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/view/PgnGui.java index 459b893..b01b9c6 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/view/PgnGui.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/view/PgnGui.java @@ -1,5 +1,273 @@ package de.hs_mannheim.informatik.chess.view; -public class PgnGui { +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.GridLayout; +import java.awt.Insets; +import java.util.List; +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.SwingConstants; + +import de.hs_mannheim.informatik.chess.model.BoardDTO; +import de.hs_mannheim.informatik.chess.model.PieceDTO; + +public class PgnGui { + + private JLabel[][] fields = new JLabel[8][8]; + private JButton flipBoardButton; + private boolean isFlipped = false; + + JButton btnFirst = new JButton("|<"); + JButton btnPrev = new JButton("<"); + JButton btnNext = new JButton(">"); + JButton btnLast = new JButton(">|"); + + Color LIGHT = new Color(0xe0e1dd); + Color DARK = new Color(0x778da9); + + private JPanel moveListPanel; + private JScrollPane moveListScroll; + + public PgnGui(){ + initFields(); + mainFrame(); + } + + + public JFrame mainFrame() { + JFrame frame = new JFrame(); + frame.setSize(1600, 1000); + frame.setLocationRelativeTo(null); + frame.add(mainPanel()); + + frame.setDefaultCloseOperation(2); + frame.setVisible(true); + return frame; + } + + private void initFields() { + for (int row = 0; row < 8; row++) { + for (int col = 0; col < 8; col++) { + JLabel label = new JLabel("", SwingConstants.CENTER); + label.setOpaque(true); + label.setFont(new Font("Serif", Font.BOLD, 40)); + // Richtige Schachfärbung: + if ((row + col) % 2 == 0) { + label.setBackground(LIGHT); // a1 ist jetzt hell! + } else { + label.setBackground(DARK); + } + fields[row][col] = label; + } + } + } + + public JPanel mainPanel() { + JPanel mainPanel = new JPanel(new GridBagLayout()); + GridBagConstraints gbc = new GridBagConstraints(); + mainPanel.setBackground(new Color(0xe0e1dd)); + // Links (Schach) + gbc.gridx = 0; + gbc.gridy = 0; + gbc.weightx = 0.6; + gbc.weighty = 1.0; + gbc.insets = new Insets(5, 5, 5, 0); + //oben, links, unten, rechts + gbc.fill = GridBagConstraints.BOTH; + mainPanel.add(chessPanel(boardPanel()), gbc); + + // Rechts (Stats) + gbc.gridx = 1; + gbc.gridy = 0; + gbc.weightx = 0.4; + gbc.weighty = 1.0; + gbc.insets = new Insets(5, 0, 5, 5); + //oben, links, unten, rechts + gbc.fill = GridBagConstraints.BOTH; + mainPanel.add(statsPanel(), gbc); + + return mainPanel; + } + + public JPanel boardPanel() { + JPanel boardPanel = new JPanel(new GridLayout(8, 8)); + boardPanel.setPreferredSize(new Dimension(800, 800)); + + for (int row = 0; row < 8; row++) { + for (int col = 0; col < 8; col++) { + JLabel label = new JLabel("", SwingConstants.CENTER); + label.setOpaque(true); + label.setFont(new Font("Serif", Font.BOLD, 70)); + // Richtige Schachfärbung: + if ((row + col) % 2 == 0) { + label.setBackground(LIGHT); + } else { + label.setBackground(DARK); + } + fields[row][col] = label; + boardPanel.add(label); + } + } + boardPanel.setBackground(new Color(0x1b263b)); + return boardPanel; + } + + public JPanel chessPanel(JPanel panel) { + JPanel chessPanel = new JPanel(new GridBagLayout()); + GridBagConstraints board = new GridBagConstraints(); + chessPanel.setBackground(new Color(0x1b263b)); + board.gridx = 0; + board.gridy = 0; + board.weightx = 0.7; + board.weighty = 1.0; + board.insets = new Insets(0, 0, 0, 0); + //oben, links, unten, rechts + board.fill = GridBagConstraints.BOTH; + chessPanel.add(panel); + // Button unten rechts + flipBoardButton = new JButton("⇵"); + flipBoardButton.setPreferredSize(new Dimension(70, 70)); + flipBoardButton.setFont(new Font("SansSerif", Font.BOLD, 40)); + flipBoardButton.setBackground(new Color(0x5500ff)); + flipBoardButton.setForeground(Color.WHITE); + flipBoardButton.setFocusPainted(false); + + GridBagConstraints btn = new GridBagConstraints(); + btn.gridx = 0; + btn.gridy = 1; + btn.weightx = 0.0; + btn.weighty = 0.0; + btn.anchor = GridBagConstraints.SOUTHEAST; + btn.insets = new Insets(10, 0, 0, 0); + + chessPanel.add(flipBoardButton, btn); + + return chessPanel; + } + + public JPanel statsPanel() { + JPanel statsPanel = new JPanel(new BorderLayout()); + statsPanel.setBackground(new Color(0x0d1b2a)); + + // Move-Liste + moveListPanel = new JPanel(); + moveListPanel.setLayout(new BoxLayout(moveListPanel, BoxLayout.Y_AXIS)); + moveListPanel.setBackground(new Color(0x0d1b2a)); + moveListScroll = new JScrollPane(moveListPanel); + moveListScroll.setPreferredSize(new Dimension(250, 800)); + statsPanel.add(moveListScroll, BorderLayout.CENTER); + + // Button-Leiste + JPanel buttonPanel = new JPanel(); + buttonPanel.setBackground(new Color(0x0d1b2a)); + // Grid oder Flow + buttonPanel.setLayout(new GridLayout(1, 4, 10, 0)); + + // Style (optional) + btnFirst.setBackground(new Color(0x212529)); btnFirst.setForeground(Color.WHITE); + btnPrev.setBackground(new Color(0x212529)); btnPrev.setForeground(Color.WHITE); + btnNext.setBackground(new Color(0x212529)); btnNext.setForeground(Color.WHITE); + btnLast.setBackground(new Color(0x212529)); btnLast.setForeground(Color.WHITE); + + // Hinzufügen + buttonPanel.add(btnFirst); + buttonPanel.add(btnPrev); + buttonPanel.add(btnNext); + buttonPanel.add(btnLast); + + // Unten ins BorderLayout + statsPanel.add(buttonPanel, BorderLayout.SOUTH); + + return statsPanel; + } + + public String showPromotionDialog(String color) { + String[] choices = {"Dame", "Turm", "Springer", "Läufer"}; + String choice = (String) JOptionPane.showInputDialog( + null, + color + "er Bauer wird umgewandelt – wähle Figur:", + "Bauernumwandlung", + JOptionPane.PLAIN_MESSAGE, + null, + choices, + choices[0]); + if (choice == null) return "QUEEN"; + switch (choice) { + case "Turm": return "ROOK"; + case "Springer": return "KNIGHT"; + case "Läufer": return "BISHOP"; + default: return "QUEEN"; + } + } + + public void updateMoveList(List moves) { + moveListPanel.removeAll(); + for (String move : moves) { + JLabel moveLabel = new JLabel(move); + moveLabel.setForeground(Color.WHITE); + moveLabel.setFont(new Font("SansSerif", Font.PLAIN, 18)); + moveListPanel.add(moveLabel); + } + moveListPanel.revalidate(); + moveListPanel.repaint(); + } + + public JLabel getField(int row, int col) { + return fields[row][col]; + } + + public JButton getFlipBoardButton() { + return flipBoardButton; + } + + public boolean isFlipped() { + return isFlipped; + } + + public void setFlipped(boolean flipped) { + this.isFlipped = flipped; + } + + public JButton getBtnFirst() { return btnFirst; } + public JButton getBtnPrev() { return btnPrev; } + public JButton getBtnNext() { return btnNext; } + public JButton getBtnLast() { return btnLast; } + + public void updateBoard(BoardDTO boardDTO) { + PieceDTO[][] board = boardDTO.getBoard(); + if (!isFlipped) { + for (int row = 0; row < 8; row++) { + for (int col = 0; col < 8; col++) { + PieceDTO piece = board[row][col]; + fields[row][col].setText(piece != null ? piece.getUnicodeSymbol() : ""); + } + } + } else { + // Invertiere Zeilen und Spalten + for (int row = 0; row < 8; row++) { + for (int col = 0; col < 8; col++) { + PieceDTO piece = board[7 - row][7 - col]; + fields[row][col].setText(piece != null ? piece.getUnicodeSymbol() : ""); + } + } + } + } + + + public void displayMessage(String msg) { + JOptionPane.showMessageDialog(null, msg); + } + } +