From f80fb99ba12abd86f4a71e081fe2d988fb6c53e3 Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 23 Jun 2025 02:28:59 +0200 Subject: [PATCH] Reworked whole creativeMode --- .../chess/controller/CreativeController.java | 35 +++-- .../informatik/chess/view/CreativeGui.java | 147 +++++++++++------- 2 files changed, 110 insertions(+), 72 deletions(-) diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/CreativeController.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/CreativeController.java index 85b2a52..9d961e1 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/CreativeController.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/CreativeController.java @@ -39,7 +39,8 @@ public class CreativeController { setupFieldListeners(); setupFenUpdateListener(); - gui.setFenText(boardToFen()); + gui.setFenText("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + applyFenToBoard(); } private void setupFieldListeners() { @@ -57,18 +58,26 @@ public class CreativeController { }); } } + gui.getFlipBoardButton().addActionListener(e -> { + applyFenToBoard(); + gui.setFlipped(!gui.isFlipped()); + updateGuiFromEngine(); // Board wird neu angezeigt, ggf. invertiert + }); } private void fieldClicked(int row, int col) { - // Nach jedem Klick: FEN aktualisieren - SwingUtilities.invokeLater(new Runnable() { - public void run() { - String fen = boardToFen(); - gui.setFenText(fen); - } - }); + String selectedPiece = gui.getSelectedPiece(); + if (selectedPiece == null) return; + if ("ERASER".equals(selectedPiece)) { + gui.getFields()[row][col].setText(""); + } else { + gui.getFields()[row][col].setText(CreativeGui.UNICODE_MAP.get(selectedPiece)); + } + // Jetzt FEN synchronisieren: + gui.setFenText(boardToFen()); } + private void setupFenUpdateListener() { //Getter für das Feld in deiner Gui: JTextField fenField = gui.getFenField(); @@ -134,20 +143,22 @@ public class CreativeController { } private void updateGuiFromEngine() { - // Lies das BoardDTO und update die Felder PieceDTO[][] board = engine.getBoardAsDTO().getBoard(); JLabel[][] fields = gui.getFields(); + boolean flipped = gui.isFlipped(); // NEU + for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { + int displayRow = flipped ? 7 - row : row; + int displayCol = flipped ? 7 - col : col; PieceDTO p = board[row][col]; if (p == null) { - fields[row][col].setText(""); + fields[displayRow][displayCol].setText(""); } else { - // Suche Symbol-String in UNICODE_MAP for (Map.Entry entry : CreativeGui.UNICODE_MAP.entrySet()) { if (entry.getKey().startsWith(p.getColor()) && entry.getKey().endsWith(p.getType())) { - fields[row][col].setText(entry.getValue()); + fields[displayRow][displayCol].setText(entry.getValue()); break; } } diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/view/CreativeGui.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/view/CreativeGui.java index bd142e9..4f146b9 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/view/CreativeGui.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/view/CreativeGui.java @@ -5,14 +5,15 @@ import java.awt.*; import java.util.HashMap; public class CreativeGui { + private boolean isFlipped = false; + private JFrame frame; private JLabel[][] fields = new JLabel[8][8]; - private String selectedPiece = null; // z.B. "BLACK_KING", "WHITE_QUEEN" + private String selectedPiece = null; private JTextField fenField; - private JButton flipBoardButton; private JButton updateBtn; + private JButton flipBoardButton; - // Unicode-Map für die Pieces (schneller Zugriff) public static final HashMap UNICODE_MAP = new HashMap() {{ put("BLACK_KING", "\u265A"); put("BLACK_QUEEN", "\u265B"); put("BLACK_ROOK", "\u265C"); put("BLACK_BISHOP", "\u265D"); @@ -20,7 +21,7 @@ public class CreativeGui { put("WHITE_KING", "\u2654"); put("WHITE_QUEEN", "\u2655"); put("WHITE_ROOK", "\u2656"); put("WHITE_BISHOP", "\u2657"); put("WHITE_KNIGHT", "\u2658"); put("WHITE_PAWN", "\u2659"); - put("ERASER", "\u2716"); // oder anderes Icon zum Löschen + put("ERASER", "\u2716"); }}; private final Color LIGHT = new Color(0xe0e1dd); @@ -29,122 +30,148 @@ public class CreativeGui { public CreativeGui() { frame = new JFrame("Creative Mode"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - frame.setSize(1200, 1000); + frame.setSize(1600, 1200); frame.setLocationRelativeTo(null); - JPanel mainPanel = new JPanel(new BorderLayout()); + JPanel mainPanel = new JPanel(new GridBagLayout()); mainPanel.setBackground(LIGHT); + GridBagConstraints gbc = new GridBagConstraints(); - mainPanel.add(toolbarPanel(false), BorderLayout.NORTH); - mainPanel.add(boardPanel(), BorderLayout.CENTER); - mainPanel.add(toolbarPanel(true), BorderLayout.SOUTH); - mainPanel.add(fenPanel(), BorderLayout.EAST); + // LINKS: chessPanel (Board+Toolbars) + gbc.gridx = 0; + gbc.gridy = 0; + gbc.weightx = 0.6; + gbc.weighty = 1.0; + gbc.insets = new Insets(5, 5, 5, 0); + gbc.fill = GridBagConstraints.BOTH; + mainPanel.add(chessPanel(), gbc); + + // RECHTS: FEN & Optionen + gbc.gridx = 1; + gbc.gridy = 0; + gbc.weightx = 0.4; + gbc.weighty = 1.0; + gbc.insets = new Insets(5, 0, 5, 5); + gbc.fill = GridBagConstraints.BOTH; + mainPanel.add(fenPanel(), gbc); frame.setContentPane(mainPanel); frame.setVisible(true); } - // Toolbar: oben = schwarz, unten = weiß private JPanel toolbarPanel(boolean white) { JPanel panel = new JPanel(); - panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); + panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); panel.setBackground(new Color(0x1b263b)); - String prefix = white ? "WHITE_" : "BLACK_"; String[] pieces = {"KING", "QUEEN", "ROOK", "BISHOP", "KNIGHT", "PAWN"}; - for (String type : pieces) { JButton btn = new JButton(UNICODE_MAP.get(prefix + type)); - btn.setFont(new Font("Serif", Font.BOLD, 45)); + btn.setFont(new Font("Serif", Font.BOLD, 40)); btn.setFocusPainted(false); + btn.setPreferredSize(new Dimension(70, 70)); btn.setBackground(white ? LIGHT : DARK); - btn.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); + btn.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0)); String key = prefix + type; btn.addActionListener(e -> selectedPiece = key); panel.add(btn); } - - // Löschen-Button JButton del = new JButton(UNICODE_MAP.get("ERASER")); - del.setFont(new Font("Serif", Font.BOLD, 40)); + del.setFont(new Font("Serif", Font.BOLD, 36)); del.setBackground(Color.PINK); + del.setPreferredSize(new Dimension(70, 70)); del.setFocusPainted(false); del.addActionListener(e -> selectedPiece = "ERASER"); - panel.add(Box.createRigidArea(new Dimension(25, 0))); panel.add(del); return panel; } - // Schachbrett private JPanel boardPanel() { JPanel boardPanel = new JPanel(new GridLayout(8, 8)); boardPanel.setPreferredSize(new Dimension(800, 800)); - boolean flip = false; // später per Button änderbar - 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)); label.setBackground((row + col) % 2 == 0 ? LIGHT : DARK); - - int r = row, c = col; // für Lambda final - label.addMouseListener(new java.awt.event.MouseAdapter() { - public void mousePressed(java.awt.event.MouseEvent evt) { - handleBoardClick(r, c); - } - }); fields[row][col] = label; boardPanel.add(label); } } return boardPanel; } + + private JPanel chessPanel() { + JPanel chessPanel = new JPanel(new GridBagLayout()); + chessPanel.setBackground(new Color(0x1b263b)); + GridBagConstraints gbc = new GridBagConstraints(); - // FEN-Panel (rechts) + // Toolbar oben + gbc.gridx = 0; + gbc.gridy = 0; + gbc.fill = GridBagConstraints.HORIZONTAL; + chessPanel.add(toolbarPanel(false), gbc); + + // Board + gbc.gridx = 0; + gbc.gridy = 1; + gbc.fill = GridBagConstraints.BOTH; + chessPanel.add(boardPanel(), gbc); + + // Toolbar unten + gbc.gridx = 0; + gbc.gridy = 2; + gbc.fill = GridBagConstraints.HORIZONTAL; + chessPanel.add(toolbarPanel(true), gbc); + + // Drehknopf + 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 = 2; + btn.weightx = 0.0; + btn.weighty = 0.0; + btn.anchor = GridBagConstraints.EAST; + btn.insets = new Insets(5, 0, 0, 0); + chessPanel.add(flipBoardButton, btn); + + return chessPanel; + } + private JPanel fenPanel() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setBackground(new Color(0xe0e1dd)); - panel.setPreferredSize(new Dimension(250, 150)); - + panel.setPreferredSize(new Dimension(420, 40)); panel.add(new JLabel("FEN:")); fenField = new JTextField(""); - fenField.setFont(new Font("Monospaced", Font.PLAIN, 15)); + fenField.setFont(new Font("Monospaced", Font.PLAIN, 22)); + fenField.setMaximumSize(new Dimension(600, 50)); panel.add(fenField); - - updateBtn = new JButton("Update Board"); // Nicht mehr als lokale Variable! - updateBtn.addActionListener(e -> {/* TODO: set board from fenField.getText()! */}); - panel.add(Box.createRigidArea(new Dimension(0, 10))); + updateBtn = new JButton("Update Board"); + updateBtn.setAlignmentX(Component.CENTER_ALIGNMENT); panel.add(updateBtn); - return panel; } - private void handleBoardClick(int row, int col) { - if (selectedPiece == null) return; - if ("ERASER".equals(selectedPiece)) { - fields[row][col].setText(""); - } else { - fields[row][col].setText(UNICODE_MAP.get(selectedPiece)); - } - // TODO: Board-State/FEN updaten - } - - // Getter für Controller etc. + // Für Controller public JLabel[][] getFields() { return fields; } public String getFenText() { return fenField.getText(); } public void setFenText(String fen) { fenField.setText(fen); } + public JTextField getFenField() { return fenField; } + public JButton getUpdateButton() { return updateBtn; } + public void setSelectedPiece(String piece) { selectedPiece = piece; } + public String getSelectedPiece() { return selectedPiece; } - // CreativeGui.java - public JTextField getFenField() { - return fenField; - } - public JButton getUpdateButton() { - // Musst du im Konstruktor speichern (z.B. als Attribut), falls noch nicht! - return updateBtn; - } - + public boolean isFlipped() { return isFlipped; } + public void setFlipped(boolean f) { isFlipped = f; } + public JButton getFlipBoardButton() { return flipBoardButton; } } -