New Projekt Stukture

This commit is contained in:
Noah
2021-10-02 17:15:49 +02:00
parent d2ab1ba1d7
commit 0c09667677
8 changed files with 465 additions and 454 deletions
+13 -435
View File
@@ -1,457 +1,35 @@
package gui;
import draw.Draw;
import game.Control;
import game.StartMode;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.*;
public class GUI {
private boolean sliderLocked = false;
private final String title = "Conway's game if Live";
private final Control control;
/* Universial Atributes */
private final String title = "Conway's Game of Life";
/* End of Universial Atributes */
/* Components of the menue View */
private JFrame jfMenue;
private JLabel heading;
private int xAxisSize = 6;
private JSlider xAxis;
private JLabel xAxisName;
private JTextField xAxisLabel;
private int yAxisSize = 6;
private JSlider yAxis;
private JLabel yAxisName;
private JLabel yAxisLabel;
private int cellCount = 6;
private JSlider startCells;
private JLabel startCellsName;
private JLabel startCellsLabel;
private JLabel modeName;
private JComboBox<String> modeBox;
private MouseListener hover;
private JButton startButton;
private JButton manuelStart;
private ImageIcon preview;
private JLabel previewLabel;
/* End of control view Elements */
/* Game view Elements */
private JFrame jfGame;
private Draw draw;
private int xSize, ySize;
/* End of Game view Elements */
private JFrame jfGameManuel;
public GUI(Control control) {
private MenuWindow menuWindow;
private GameWindow gameWindow;
private ControlWindow controlWindow;
public GUI(Control control){
this.control = control;
}
public void controlWindow() {
jfMenue = new JFrame();
jfMenue.setTitle(title);
ImageIcon imageIcon = new ImageIcon("src/data/PNG/Icon.png");
jfMenue.setIconImage(imageIcon.getImage());
//getContentPane().setBackground(Color.decode("#121212"));
jfMenue.setUndecorated(false);
jfMenue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfMenue.setAutoRequestFocus(false);
jfMenue.setLayout(null);
jfMenue.setSize(752, 424);
jfMenue.setResizable(false);
initControlComponents();
addControlComponents();
jfMenue.setLocationRelativeTo(null);
jfMenue.setVisible(true);
public void createMenuWindow(){
menuWindow = new MenuWindow(title, control);
}
private void initControlComponents() {
Font head = new Font("Segoe UI Light", Font.PLAIN, 24);
Font text = new Font("Segoe UI Light", Font.PLAIN, 16);
heading = new JLabel("Control Panel");
heading.setFont(head);
heading.setForeground(Color.decode("#121212"));
heading.setBounds(50, 25, 400, 50);
heading.setVisible(true);
int xAxisMin = 6;
int xAxisMax = 1024;
xAxis = new JSlider(xAxisMin, xAxisMax);
xAxis.setBounds(180, 110, 200, 20);
xAxis.setFont(text);
xAxis.setForeground(Color.decode("#121212"));
xAxis.setValue(6);
xAxis.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if(!sliderLocked) {
xAxisSize = xAxis.getValue();
updateSliderLables(0, xAxisSize);
updateStartCells();
}
}
});
xAxis.setVisible(true);
xAxisName = new JLabel("Breite in Zellen:");
xAxisName.setBounds(50, 100, 200, 30);
xAxisName.setFont(text);
xAxisName.setForeground(Color.decode("#121212"));
xAxisName.setVisible(true);
xAxisLabel = new JTextField("6");
xAxisLabel.setBackground(null);
xAxisLabel.setBorder(null);
xAxisLabel.setBounds(180, 80, 200, 30);
xAxisLabel.setFont(text);
xAxisLabel.setForeground(Color.decode("#121212"));
xAxisLabel.setVisible(true);
xAxisLabel.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent evt) {
if (!Character.isDigit(evt.getKeyChar())) {
evt.consume();
}
}
});
xAxisLabel.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
sliderLocked = true;
String s = xAxisLabel.getText();
xAxis.setValue(Integer.parseInt(s));
sliderLocked = false;
}
@Override
public void removeUpdate(DocumentEvent e) {
}
@Override
public void changedUpdate(DocumentEvent e) {
}
});
int yAxisMin = 6;
int yAxisMax = 1024;
yAxis = new JSlider(yAxisMin, yAxisMax);
yAxis.setBounds(180, 160, 200, 20);
yAxis.setFont(text);
yAxis.setForeground(Color.decode("#121212"));
yAxis.setValue(6);
yAxis.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
yAxisSize = yAxis.getValue();
updateSliderLables(1, yAxisSize);
updateStartCells();
}
});
yAxis.setVisible(true);
yAxisName = new JLabel("Höhe in Zellen:");
yAxisName.setBounds(50, 150, 200, 30);
yAxisName.setFont(text);
yAxisName.setForeground(Color.decode("#121212"));
yAxisName.setVisible(true);
yAxisLabel = new JLabel("6");
yAxisLabel.setBounds(180, 130, 200, 30);
yAxisLabel.setFont(text);
yAxisLabel.setForeground(Color.decode("#121212"));
yAxisLabel.setVisible(true);
//TODO: Inputfield witch Changelistner to cange Slider
startCells = new JSlider(6, 6);
startCells.setBounds(180, 210, 200, 20);
startCells.setFont(text);
startCells.setForeground(Color.decode("#121212"));
startCells.setValue(6);
startCells.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
cellCount = startCells.getValue();
updateSliderLables(2, cellCount);
}
});
startCells.setVisible(true);
startCellsName = new JLabel("Start Zellen:");
startCellsName.setBounds(50, 200, 200, 30);
startCellsName.setFont(text);
startCellsName.setForeground(Color.decode("#121212"));
startCellsName.setVisible(true);
startCellsLabel = new JLabel("6");
startCellsLabel.setBounds(180, 180, 200, 30);
startCellsLabel.setFont(text);
startCellsLabel.setForeground(Color.decode("#121212"));
startCellsLabel.setVisible(true);
updateStartCells();
hover = new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
super.mouseEntered(e);
JButton b = (JButton) e.getSource();
b.setForeground(Color.decode("#C40233"));
b.setBounds(b.getX() - 10, b.getY() - 10, b.getWidth() + 20, b.getHeight() + 20);
}
@Override
public void mouseExited(MouseEvent e) {
super.mouseExited(e);
JButton b = (JButton) e.getSource();
b.setForeground(Color.decode("#121212"));
b.setBounds(b.getX() + 10, b.getY() + 10, b.getWidth() - 20, b.getHeight() - 20);
}
};
modeName = new JLabel("Select Figure: ");
modeName.setBounds(50, 250, 200, 30);
modeName.setFont(text);
modeName.setForeground(Color.decode("#121212"));
modeName.setVisible(true);
modeBox = new JComboBox<String>();
modeBox.setBounds(180, 257, 100, 20);
addComboBoxEntry(modeBox, "Randomized");
addComboBoxEntry(modeBox, "Task1");
addComboBoxEntry(modeBox, "Blinker");
addComboBoxEntry(modeBox, "Toad");
startButton = new JButton();
startButton.setText("Start Game");
startButton.setFont(text);
startButton.setForeground(Color.decode("#121212"));
startButton.setBorderPainted(true);
startButton.setFocusPainted(false);
startButton.setContentAreaFilled(false);
startButton.setBounds(50, 310, 150, 50);
startButton.setVisible(true);
startButton.addMouseListener(hover);
startButton.setBorder(new RoundedBorder(25));
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
StartMode startMethod = StartMode.Task1;
int index = modeBox.getSelectedIndex();
startMethod = StartMode.values()[index + 1]; // + 1 BECAUSE MANUEL HAS THE FIRST INDEX IN ENUM
control.buildGameWindow(xAxisSize, yAxisSize, cellCount, startMethod);
}
});
manuelStart = new JButton();
manuelStart.setText("Manuel Start");
manuelStart.setFont(text);
manuelStart.setForeground(Color.decode("#121212"));
manuelStart.setBorderPainted(true);
manuelStart.setFocusPainted(false);
manuelStart.setContentAreaFilled(false);
manuelStart.setBounds(225, 310, 150, 50);
manuelStart.setVisible(true);
manuelStart.addMouseListener(hover);
manuelStart.setBorder(new RoundedBorder(25));
manuelStart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
control.buildGameWindow(xAxisSize, yAxisSize, cellCount, StartMode.Manuel);
}
});
preview = new ImageIcon("src/data/PNG/Preview.png");
previewLabel = new JLabel(preview);
previewLabel.setBounds(450, 45, 203, 302);
public void createGameWindow(int cellCountX, int cellCountY){
gameWindow = new GameWindow(title, control, cellCountX, cellCountY);
}
private void updateStartCells() {
startCells.setMaximum(xAxisSize * yAxisSize);
public void createControlWindow(){
controlWindow = new ControlWindow();
}
private void updateSliderLables(int slider, int val) {
switch (slider) {
case 0:
xAxisLabel.setText(String.valueOf(val));
break;
case 1:
yAxisLabel.setText(String.valueOf(val));
break;
case 2:
startCellsLabel.setText(String.valueOf(val));
break;
}
}
private void addControlComponents() {
jfMenue.add(heading);
jfMenue.add(xAxis);
jfMenue.add(xAxisName);
jfMenue.add(xAxisLabel);
jfMenue.add(yAxis);
jfMenue.add(yAxisName);
jfMenue.add(yAxisLabel);
jfMenue.add(startCells);
jfMenue.add(startCellsName);
jfMenue.add(startCellsLabel);
jfMenue.add(startButton);
jfMenue.add(manuelStart);
jfMenue.add(previewLabel);
jfMenue.add(modeName);
jfMenue.add(modeBox);
}
private void addComboBoxEntry(JComboBox<String> comboBox, String entry) {
comboBox.addItem(entry);
}
public void gameWindow(int xSize, int ySize) {
this.xSize = xSize;
this.ySize = ySize;
jfGame = new JFrame(title);
jfGame.getContentPane().setPreferredSize(new Dimension(1000, 1000));
jfGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfGame.setResizable(false);
float ratio = (float)(ySize) / (float)(xSize);
draw = new Draw(this.xSize, this.ySize, ratio);
draw.setBounds(0, 0, 800, 800);
draw.setVisible(true);
jfGame.add(draw);
jfGame.pack();
jfGame.setLayout(null);
jfGame.setLocationRelativeTo(null);
jfGame.setVisible(true);
}
public void manuelGameWindow(int xSize, int ySize) {
this.xSize = xSize;
this.ySize = ySize;
jfGameManuel = new JFrame(title);
jfGameManuel.getContentPane().setPreferredSize(new Dimension(800, 800));
jfGameManuel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfGameManuel.setResizable(false);
float ratio = (float)(ySize) / (float)(xSize);
draw = new Draw(this.xSize, this.ySize, ratio);
jfGameManuel.setBounds(0, 0, 800, 800);
jfGameManuel.setVisible(true);
jfGameManuel.getContentPane().addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
control.removeCell(e.getX(), e.getY());
}
@Override
public void mouseMoved(MouseEvent e) {
int top = jfGameManuel.getInsets().top;
System.out.println("top: " + top);
control.addCell(e.getX(), e.getY());
}
});
jfGameManuel.add(draw);
jfGameManuel.pack();
jfGameManuel.setLayout(null);
jfGameManuel.setLocationRelativeTo(null);
jfGameManuel.setVisible(true);
}
private void initGameComponents() {
}
private void addGameComponents() {
}
public void showGrid(boolean[][] grid)
{
if(draw != null)
{
draw.showGrid(grid);
}
else
{
System.err.println("There is no Window");
}
public void showGrid(boolean[][] grid){
gameWindow.showGrid(grid);
}