grundlegendes Schachbrett aus Buttons hinzugefügt und UI

Funktionabilität erweitert.
devGameUi
Matias 2025-06-09 20:58:56 +02:00
parent 1d4ae4abdb
commit dd5e7382bd
8 changed files with 173 additions and 41 deletions

View File

@ -36,5 +36,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="com.jgoodies.common_1.8.1.v20240327-0800.jar"/>
<classpathentry kind="lib" path="com.jgoodies.forms_1.9.0.v20240327-0800.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

Binary file not shown.

Binary file not shown.

View File

@ -1,6 +1,9 @@
package de.mannheim.th.chess;
import de.mannheim.th.chess.ui.Ui;
import de.mannheim.th.chess.ui.MainFrame;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Eine einfache Schach App mithilfe von {@linkplain https://github.com/bhlangonijr/chesslib} entwickelt.
@ -9,13 +12,15 @@ import de.mannheim.th.chess.ui.Ui;
*/
public class App {
private static Ui userinterface;
private static final Logger logger = LogManager.getLogger(App.class);
private static MainFrame userinterface;
/**
* Main-Methode.
* @param args
*/
public static void main(String[] args) {
System.out.println("Hello World!");
userinterface = new Ui();
logger.info("Hello World.");
userinterface = new MainFrame();
}
}

View File

@ -7,17 +7,27 @@ import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.Color;
public class MainFrame extends JFrame {
private ArrayList<SpielFrame> spiele = new ArrayList<>();
private static final long serialVersionUID = 1L;
private JPanel contentPane;
@ -25,36 +35,38 @@ public class MainFrame extends JFrame {
/**
* 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();
// }
// }
// });
// }
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.
*/
public MainFrame() {
setBackground(Color.WHITE);
setResizable(false);
setBackground(Color.LIGHT_GRAY);
setResizable(true);
setAlwaysOnTop(true);
setTitle("Schach");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 500);
contentPane = new JPanel();
contentPane.setBackground(Color.GRAY);
contentPane.setForeground(Color.GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.add(Box.createVerticalStrut(10));
@ -78,6 +90,17 @@ public class MainFrame extends JFrame {
btnNewButton.setForeground(Color.BLACK);
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 16));
btnNewButton.setAlignmentX(Component.CENTER_ALIGNMENT);
btnNewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SpielFrame sp = new SpielFrame();
spiele.add(sp);
}
});
contentPane.add(btnNewButton);
contentPane.add(Box.createVerticalStrut(15));
@ -87,6 +110,24 @@ public class MainFrame extends JFrame {
btnNewButton_1.setForeground(Color.BLACK);
btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 16));
btnNewButton_1.setAlignmentX(Component.CENTER_ALIGNMENT);
btnNewButton_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser dateiWaehler = new JFileChooser();
JFrame jfFile = new JFrame();
int auswahl = dateiWaehler.showOpenDialog(jfFile);
if (auswahl == JFileChooser.APPROVE_OPTION) {
File ausgewaehlteDatei = dateiWaehler.getSelectedFile();
JOptionPane.showMessageDialog(jfFile,"Gewählte Datei:\n" + ausgewaehlteDatei.getAbsolutePath());
//Uebergabe zu Logik zum extrahieren der Daten
}
}
});
contentPane.add(btnNewButton_1);
contentPane.add(Box.createVerticalStrut(15));
@ -96,7 +137,16 @@ public class MainFrame extends JFrame {
btnNewButton_2.setForeground(Color.BLACK);
btnNewButton_2.setFont(new Font("Tahoma", Font.BOLD, 16));
btnNewButton_2.setAlignmentX(Component.CENTER_ALIGNMENT);
contentPane.add(btnNewButton_2);
btnNewButton_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
contentPane.add(btnNewButton_2);
setVisible(true);
}
}

View File

@ -0,0 +1,97 @@
package de.mannheim.th.chess.ui;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.mannheim.th.chess.App;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import java.awt.BorderLayout;
import java.awt.Color;
import java.util.ArrayList;
import java.awt.GridLayout;
public class SpielFrame extends JFrame {
private static final Logger logger = LogManager.getLogger(App.class);
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private ArrayList<JButton> buttons = new ArrayList<>();
private JPanel panelLinks, panelRechts;
/**
* Launch the application.
*/
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.
*/
public SpielFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1920, 1080);
setTitle("Spiel");
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
setContentPane(contentPane);
// Linkes Panel mit GridLayout 8x8 für Schachbrett
panelLinks = new JPanel(new GridLayout(8, 8));
erstelleBrett();
// Rechtes Panel für Steuerung oder zusätzliche Eingaben
panelRechts = new JPanel();
panelRechts.setBackground(Color.LIGHT_GRAY);
// JSplitPane horizontal (linke und rechte Hälfte)
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelLinks, panelRechts);
splitPane.setResizeWeight(0.70);
splitPane.setDividerSize(5);
splitPane.setEnabled(false);
contentPane.add(splitPane, BorderLayout.CENTER);
setVisible(true);
}
private void erstelleBrett() {
for (int i = 0; i < 64; i++) {
JButton b = new JButton();
b.setFocusPainted(false);
b.setFont(new Font("Arial", Font.PLAIN, 30));
if ((i / 8 + i % 8) % 2 == 0) {
logger.info("Helles Feld erstellt."+i);
b.setBackground(new Color(90, 90, 90));
} else {
logger.info("Dunkles Feld erstellt."+i);
b.setBackground(new Color(65, 65, 65));
}
b.setForeground(Color.WHITE);
b.setBorderPainted(false);
panelLinks.add(b);
}
}
}

View File

@ -1,22 +0,0 @@
package de.mannheim.th.chess.ui;
import java.awt.EventQueue;
import java.util.ArrayList;
import de.mannheim.th.chess.utl.GameReader;
/**
* Zeigt das Main-Menü der App an.
*/
public class Ui{
private ArrayList<GameWindow> gamewindows = new ArrayList<>();
private GameReader reader = new GameReader();
private MainFrame mf;
public Ui() {
mf = new MainFrame();
mf.setVisible(true);
}
}