From 5dcdabb2150ba7bf7655cf5c70ab7dc71fb03143 Mon Sep 17 00:00:00 2001 From: Justin Date: Mon, 23 Jun 2025 01:35:57 +0200 Subject: [PATCH] Fully implemented CreativeGui class --- .../informatik/chess/view/CreativeGui.java | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) 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 56b0582..bd142e9 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 @@ -1,5 +1,150 @@ package de.hs_mannheim.informatik.chess.view; +import javax.swing.*; +import java.awt.*; +import java.util.HashMap; + public class CreativeGui { + private JFrame frame; + private JLabel[][] fields = new JLabel[8][8]; + private String selectedPiece = null; // z.B. "BLACK_KING", "WHITE_QUEEN" + private JTextField fenField; + private JButton flipBoardButton; + private JButton updateBtn; + + // 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"); + put("BLACK_KNIGHT", "\u265E"); put("BLACK_PAWN", "\u265F"); + 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 + }}; + + private final Color LIGHT = new Color(0xe0e1dd); + private final Color DARK = new Color(0x778da9); + + public CreativeGui() { + frame = new JFrame("Creative Mode"); + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + frame.setSize(1200, 1000); + frame.setLocationRelativeTo(null); + + JPanel mainPanel = new JPanel(new BorderLayout()); + mainPanel.setBackground(LIGHT); + + mainPanel.add(toolbarPanel(false), BorderLayout.NORTH); + mainPanel.add(boardPanel(), BorderLayout.CENTER); + mainPanel.add(toolbarPanel(true), BorderLayout.SOUTH); + mainPanel.add(fenPanel(), BorderLayout.EAST); + + 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.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.setFocusPainted(false); + btn.setBackground(white ? LIGHT : DARK); + btn.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); + 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.setBackground(Color.PINK); + 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; + } + + // FEN-Panel (rechts) + 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.add(new JLabel("FEN:")); + fenField = new JTextField(""); + fenField.setFont(new Font("Monospaced", Font.PLAIN, 15)); + 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))); + 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. + public JLabel[][] getFields() { return fields; } + public String getFenText() { return fenField.getText(); } + public void setFenText(String fen) { fenField.setText(fen); } + + // CreativeGui.java + public JTextField getFenField() { + return fenField; + } + public JButton getUpdateButton() { + // Musst du im Konstruktor speichern (z.B. als Attribut), falls noch nicht! + return updateBtn; + } } +