Compare commits

...

8 Commits

Author SHA1 Message Date
beratkocak 214dc0e286 Merge branch 'gui' into main 2024-12-20 14:16:38 +01:00
beratkocak 8171a410cc added highscore maker to highscore gui 2024-12-20 14:13:45 +01:00
beratkocak 8dcb29d701 added backonestep method to retract steps from user and repaint panel 2024-12-20 14:04:03 +01:00
beratkocak 6a505ef34d added gridUpdate to refrech gameGrid after user press to revalidate
colors of buttons
2024-12-20 14:00:39 +01:00
beratkocak 1e640026f7 added paint button to color the buttons after user press 2024-12-20 13:57:57 +01:00
beratkocak cf38b86df1 added paint game method to take input from csv reading method and make a
jframe of buttons
2024-12-20 13:55:22 +01:00
beratkocak 2aaac10f48 added highscore jframe to highscore gui 2024-12-20 13:48:46 +01:00
beratkocak 093c9bf601 added main menu to menugui class 2024-12-20 13:45:39 +01:00
3 changed files with 403 additions and 1 deletions

View File

@ -1,5 +1,209 @@
package de.hs_mannheim.informatik.pr2projekt.gui;
public class GameGUI {
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.io.FileNotFoundException;
import java.util.EmptyStackException;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import de.hs_mannheim.informatik.pr2projekt.domain.HitoriMain;
public class GameGUI {
public static void paintGame(String[] filepath, JButton[][] buttons, String[][] colors, Stack<String> madeMoves, String[][] data){
JFrame frame = new JFrame();
int num = buttons.length;
JPanel gameGrid = new JPanel(new GridLayout(num,num,0,0));
for(int i=0;i<num;i++){
for(int j=0;j<num;j++){
JButton b = buttons[i][j];
b.setOpaque(true);
b.setContentAreaFilled(true);
b.setBorderPainted(false);
b.setFocusPainted(false);
b.setBackground(Color.WHITE);
gameGrid.add(b);
String[] pos = HitoriMain.getCords(i,j);
b.addActionListener(e -> {paintButton(b, pos, colors , madeMoves);});
}
}
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(gameGrid, BorderLayout.CENTER);
JPanel buttonGrid = new JPanel(new GridLayout(1,4,10,10));
JButton b0 = new JButton("Aufgeben");
b0.addActionListener(e -> {
frame.dispose();
MenuGUI.getPath(filepath);
});
JButton b1 = new JButton("Zurück");
b1.addActionListener(e -> {HitoriMain.backOneStep(madeMoves, buttons, colors, gameGrid);});
JButton b2 = new JButton("Zurücksetzen");
b2.addActionListener(e -> {HitoriMain.totalResetButton(buttons, colors, madeMoves, data);});
JButton b3 = new JButton("Abgeben");
String path = filepath[0];
b3.addActionListener(e -> {
try{
boolean levelFinished = HitoriMain.abgabeMöglich(path, data, colors);
if(levelFinished == true){
GameGUI.finish(filepath, path, data, colors);
frame.dispose();
} else {
JLabel text1 = new JLabel("Abgabe nicht richtig!");
mainPanel.add(text1, BorderLayout.NORTH);
mainPanel.revalidate();
mainPanel.repaint();
}
} catch (FileNotFoundException i) {
System.out.println("File not found: " + i.getMessage());
}
});
buttonGrid.add(b0);buttonGrid.add(b1);buttonGrid.add(b2);buttonGrid.add(b3);
mainPanel.add(buttonGrid, BorderLayout.SOUTH);
mainPanel.setVisible(true);
frame.add(mainPanel);
frame.setVisible(true);
frame.setSize(600,600);
}
public static void paintButton(JButton b, String[] pos, String[][] colors, Stack<String> madeMoves){
int i = Integer.parseInt(pos[0]);
int j = Integer.parseInt(pos[1]);
String col = colors[Integer.parseInt(pos[0])][Integer.parseInt(pos[1])];
if(col.endsWith("W")){
b.setOpaque(true);
b.setForeground(Color.BLACK);
b.setContentAreaFilled(true);
b.setBorderPainted(false);
b.setFocusPainted(false);
b.setBackground(Color.lightGray);
colors[i][j] += "G";
String logEntrance = i+"."+j+"."+"G";
madeMoves.push(logEntrance);
}
if(col.endsWith("G")){
b.setOpaque(true);
b.setForeground(Color.WHITE);
b.setContentAreaFilled(true);
b.setBorderPainted(false);
b.setFocusPainted(false);
b.setBackground(Color.BLACK);
colors[i][j] += "B";
String logEntrance = i+"."+j+"."+"B";
madeMoves.push(logEntrance);
}
if(col.endsWith("B")){
b.setOpaque(true);
b.setForeground(Color.BLACK);
b.setContentAreaFilled(true);
b.setBorderPainted(false);
b.setFocusPainted(false);
b.setBackground(Color.WHITE);
colors[i][j] += "W";
String logEntrance = i+"."+j+"."+"W";
madeMoves.push(logEntrance);
}
}
public static void backOneStep(Stack<String> movesMade, JButton[][] buttons, String[][] colors, JPanel grid){
try {
String move = movesMade.pop();
String[] line = move.split("\\.");
String y = line[0];
String x = line[1];
String color = line[2];
if(color.equals("W")){
int i = Integer.parseInt(y);
int j = Integer.parseInt(x);
JButton b0 = buttons[i][j];
b0.setOpaque(true);
b0.setForeground(Color.WHITE);
b0.setContentAreaFilled(true);
b0.setBorderPainted(false);
b0.setFocusPainted(false);
b0.setBackground(Color.BLACK);
buttons[i][j] = b0;
String str = colors[i][j];
String str0 = str.substring(0, str.length() - 1);
colors[i][j] = str0;
grid.repaint();
gridUpdate(grid, buttons);
}else if(color.equals("G")){
int i = Integer.parseInt(y);
int j = Integer.parseInt(x);
JButton b0 = buttons[i][j];
b0.setOpaque(true);
b0.setForeground(Color.BLACK);
b0.setContentAreaFilled(true);
b0.setBorderPainted(false);
b0.setFocusPainted(false);
b0.setBackground(Color.WHITE);
buttons[i][j] = b0;
String str = colors[i][j];
String str0 = str.substring(0, str.length() - 1);
colors[i][j] = str0;
gridUpdate(grid, buttons);
} else if(color.equals("B")){
int i = Integer.parseInt(y);
int j = Integer.parseInt(x);
JButton b0 = buttons[i][j];
b0.setOpaque(true);
b0.setForeground(Color.BLACK);
b0.setContentAreaFilled(true);
b0.setBorderPainted(false);
b0.setFocusPainted(false);
b0.setBackground(Color.lightGray);
buttons[i][j] = b0;
String str = colors[i][j];
String str0 = str.substring(0, str.length() - 1);
colors[i][j] = str0;
gridUpdate(grid, buttons);
}
} catch(EmptyStackException e) {}
}
public static void gridUpdate(JPanel grid, JButton[][] buttons){
grid.removeAll();
grid.repaint();
for(int i = 0; i<buttons.length; i++){
for(int j = 0; j<buttons.length;j++){
JButton b0 = buttons[i][j];
grid.add(b0);
grid.repaint();
}
}
}
public static void finish(String[] filepath, String path, String[][] data, String[][] colors) throws FileNotFoundException{
JFrame frame = new JFrame("Highscore anlegen");
JPanel mainPanel = new JPanel(new BorderLayout());
JLabel text = new JLabel("Geben Sie unten Ihren Namen an um sich in der Highscore Liste einzutragen.");
mainPanel.add(text, BorderLayout.NORTH);
JTextField field = new JTextField(20);
mainPanel.add(field, BorderLayout.CENTER);
JButton b = new JButton("In der Highscore Liste eintragen");
mainPanel.add(b, BorderLayout.SOUTH);
frame.setVisible(true);
frame.setSize(600,600);
frame.add(mainPanel);
b.addActionListener(e -> {
String username = field.getText();
try {
HighscoreGUI.newRecord(path, username);
frame.dispose();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
MenuGUI.getPath(filepath);
});
filepath[0] = "";
filepath[1] = "";
}
}

View File

@ -1,5 +1,137 @@
package de.hs_mannheim.informatik.pr2projekt.gui;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import de.hs_mannheim.informatik.pr2projekt.domain.HitoriMain;
public class HighscoreGUI {
public static void highscoreScreen(String[] filepath){
JFrame frame = new JFrame("Highscores");
JPanel highscorePanel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel(new GridLayout(7,1,10,10));
String[] buttons = {
"Highscore 4x4 - leicht",
"Highscore 5x5 - leicht",
"Highscore 8x8 - leicht",
"Highscore 8x8 - medium",
"Highscore 10x10 - medium",
"Highscore 15x15 - medium"
};
for(int i=0;i<buttons.length;i++){
JButton b0 = new JButton(buttons[i]);
buttonPanel.add(b0);
int[] count = {i};
char rowChar = buttons[i].charAt(0);
String[] row = {String.valueOf(rowChar)};
b0.addActionListener(e -> {
int j = count[0];
String[] paths = {
"Hitori_Highscores/Hitori4x4_leicht.txt",
"Hitori_Highscores/Hitori5x5leicht.txt",
"Hitori_Highscores/Hitori8x8leicht.txt",
"Hitori_Highscores/Hitori8x8medium.txt",
"Hitori_Highscores/Hitori10x10medium.txt",
"Hitori_Highscores/Hitori15x15_medium.txt"
};
HighscoreGUI.showHighscores(paths[j]);
frame.dispose();
});
}
JButton b = new JButton("Zurück");
b.addActionListener(e -> {
frame.dispose();
MenuGUI.getPath(filepath);
});
buttonPanel.add(b);
frame.setVisible(true);
frame.setSize(600,600);
highscorePanel.add(buttonPanel, BorderLayout.CENTER);
JLabel text0 = new JLabel("Level für Highscore Liste auswählen!");
highscorePanel.add(text0, BorderLayout.NORTH);
frame.setDefaultCloseOperation(0);
frame.add(highscorePanel);
}
public static void newRecord(String path, String username) throws FileNotFoundException{
String[] parts = path.split("/");
String filename = parts[parts.length - 1];
int dotIndex = filename.lastIndexOf(".");
String result = filename.substring(0, dotIndex);
String ordner = "Hitori_Highscores/";
String filetype = ".txt";
String filepath = ordner+result+filetype;
Scanner sc = new Scanner(filepath);
while (sc.hasNextLine()) {
sc.nextLine();
}
sc.close();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filepath, true))) {
writer.write(username);
writer.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void showHighscores(String[] filepath, String path){
JFrame frame = new JFrame("Highscore");
JPanel mainPanel = new JPanel(new BorderLayout());
File file = new File(path);
int i = 0;
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
scanner.nextLine();
i++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
JPanel highscorePanel = new JPanel(new GridLayout(i,1,10,10));
if(i>0){
ArrayList<String> lines = HitoriMain.readFromFile(path);
for(String s: lines){
JLabel text = new JLabel(s);
highscorePanel.add(text);
}
JButton b = new JButton("Zurück");
b.addActionListener(e -> {
frame.dispose();
highscoreScreen(filepath);
});
mainPanel.add(b, BorderLayout.SOUTH);
} else {
JLabel text = new JLabel("Noch kein Highscore eingetragen.");
highscorePanel.add(text);
JButton b = new JButton("Zurück");
b.addActionListener(e -> {
frame.dispose();
highscoreScreen(filepath);
});
mainPanel.add(b, BorderLayout.SOUTH);
}
JButton b = new JButton("Zurück");
b.addActionListener(e ->{
frame.dispose();
MenuGUI.getPath(filepath);
});
mainPanel.add(highscorePanel, BorderLayout.CENTER);
frame.add(mainPanel);
frame.setVisible(true);
frame.setSize(600,600);
}
}

View File

@ -1,5 +1,71 @@
package de.hs_mannheim.informatik.pr2projekt.gui;
<<<<<<< HEAD
public class MenuGUI {
//ABC
=======
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.FileNotFoundException;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MenuGUI {
public static void getPath(String[] filepath){
String[] filepath0 = new String[2];
JFrame frame = new JFrame("Hitori");
JPanel menuPanel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel(new GridLayout(7,1,10,10));
String[] buttons = {
"4x4 - leicht",
"5x5 - leicht",
"8x8 - leicht",
"8x8 - medium",
"10x10 - medium",
"15x15 - medium"
};
for(int i=0;i<buttons.length;i++){
JButton b0 = new JButton(buttons[i]);
buttonPanel.add(b0);
int[] count = {i};
char rowChar = buttons[i].charAt(0);
String[] row = {String.valueOf(rowChar)};
b0.addActionListener(e -> {
int j = count[0];
String[] paths = {
"Hitori_Spielfelder2/Hitori4x4_leicht.csv",
"Hitori_Spielfelder2/Hitori5x5leicht.csv",
"Hitori_Spielfelder2/Hitori8x8leicht.csv",
"Hitori_Spielfelder2/Hitori8x8medium.csv",
"Hitori_Spielfelder2/Hitori10x10medium.csv",
"Hitori_Spielfelder2/Hitori15x15_medium.csv"
};
filepath0[0] = paths[j];
filepath0[1] = row[0];
filepath[0] = filepath0[0];
filepath[1] = filepath0[1];
frame.dispose();
});
}
JButton b = new JButton("Highscores");
b.addActionListener(e -> {
HighscoreGUI.highscoreScreen(filepath);
frame.dispose();
});
buttonPanel.add(b);
frame.setVisible(true);
frame.setSize(600,600);
menuPanel.add(buttonPanel, BorderLayout.CENTER);
JLabel text0 = new JLabel("Wählen Sie ein Level aus!");
menuPanel.add(text0, BorderLayout.NORTH);
frame.setDefaultCloseOperation(0);
frame.add(menuPanel);
}
>>>>>>> refs/heads/gui
}