New Projekt Stukture
This commit is contained in:
@@ -24,7 +24,7 @@ public class Draw extends JLabel {
|
|||||||
// for (int y = 0; y < Control.CELLCOUNT_Y; y++) {
|
// for (int y = 0; y < Control.CELLCOUNT_Y; y++) {
|
||||||
// if (Control.cells[x][y]) {
|
// if (Control.cells[x][y]) {
|
||||||
// g.setColor(Color.BLACK);
|
// g.setColor(Color.BLACK);
|
||||||
// g.fillRect(x + GUI.XOFF, y + GUI.YOFF, 1, 1);
|
// g.fillRect(x + OldGUI.XOFF, y + OldGUI.YOFF, 1, 1);
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|||||||
+6
-3
@@ -5,6 +5,7 @@ import java.awt.*;
|
|||||||
|
|
||||||
public class Draw extends JPanel {
|
public class Draw extends JPanel {
|
||||||
private int xSize, ySize;
|
private int xSize, ySize;
|
||||||
|
private int width, height;
|
||||||
private float ratio;
|
private float ratio;
|
||||||
|
|
||||||
private Graphics2D g2d;
|
private Graphics2D g2d;
|
||||||
@@ -12,9 +13,11 @@ public class Draw extends JPanel {
|
|||||||
|
|
||||||
private boolean drawLocked = false;
|
private boolean drawLocked = false;
|
||||||
|
|
||||||
public Draw(int gridSizeX, int gridSizeY, float ratio) {
|
public Draw(int gridSizeX, int gridSizeY, int width, int height, float ratio) {
|
||||||
this.xSize = gridSizeX;
|
this.xSize = gridSizeX;
|
||||||
this.ySize = gridSizeY;
|
this.ySize = gridSizeY;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
this.ratio = ratio;
|
this.ratio = ratio;
|
||||||
currentGrid = new boolean[gridSizeX][gridSizeY];
|
currentGrid = new boolean[gridSizeX][gridSizeY];
|
||||||
|
|
||||||
@@ -36,8 +39,8 @@ public class Draw extends JPanel {
|
|||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
g2d = (Graphics2D) g;
|
g2d = (Graphics2D) g;
|
||||||
|
|
||||||
float cellSizeX = 800f / xSize;
|
float cellSizeX = width / xSize;
|
||||||
float cellSizeY = 800f / ySize;
|
float cellSizeY = height / ySize;
|
||||||
|
|
||||||
if(ySize > xSize)
|
if(ySize > xSize)
|
||||||
{
|
{
|
||||||
|
|||||||
+2
-6
@@ -9,24 +9,20 @@ public class Clock extends Thread{
|
|||||||
|
|
||||||
private boolean running = true;
|
private boolean running = true;
|
||||||
|
|
||||||
public void start(Control control, GUI gui){
|
public void start(Control control, GUI gui, int velocity){
|
||||||
|
|
||||||
|
|
||||||
Timer t = new Timer();
|
Timer t = new Timer();
|
||||||
TimerTask tt = new TimerTask() {
|
TimerTask tt = new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
if (running){
|
if (running){
|
||||||
control.showGrid(control.getCells());
|
control.showGrid(control.getCells());
|
||||||
control.nextGen();
|
control.nextGen();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
t.scheduleAtFixedRate(tt, 1000, velocity);
|
||||||
t.scheduleAtFixedRate(tt, 1000, 200);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+14
-9
@@ -2,6 +2,7 @@ package game;
|
|||||||
|
|
||||||
import gui.GUI;
|
import gui.GUI;
|
||||||
|
|
||||||
|
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
public class Control {
|
public class Control {
|
||||||
@@ -21,13 +22,13 @@ public class Control {
|
|||||||
public void start() {
|
public void start() {
|
||||||
|
|
||||||
gui = new GUI(this);
|
gui = new GUI(this);
|
||||||
gui.controlWindow();
|
gui.createMenuWindow();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startGame() {
|
public void startGame() {
|
||||||
clock = new Clock();
|
clock = new Clock();
|
||||||
clock.start(this, gui);
|
clock.start(this, gui, 2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void buildGameWindow(int xSize, int ySize, int cellCount, StartMode startMode) {
|
public void buildGameWindow(int xSize, int ySize, int cellCount, StartMode startMode) {
|
||||||
@@ -57,20 +58,20 @@ public class Control {
|
|||||||
gui = new GUI(this);
|
gui = new GUI(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (startMode == StartMode.Manuel){
|
gui.createGameWindow(this.xSize, this.ySize);
|
||||||
gui.manuelGameWindow(this.xSize, this.ySize);
|
|
||||||
} else {
|
|
||||||
gui.gameWindow(this.xSize, this.ySize);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// startGame();
|
startGame();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void buildManuelGame() {
|
private void buildManuelGame() {
|
||||||
|
|
||||||
|
for (int x = 0; x < xSize; x++) {
|
||||||
|
for (int y = 0; y < ySize; y++) {
|
||||||
|
cells[x][y] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,6 +234,10 @@ public class Control {
|
|||||||
showGrid(cells);
|
showGrid(cells);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void createControlWindow() {
|
||||||
|
gui.createControlWindow();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package gui;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class ControlWindow {
|
||||||
|
|
||||||
|
private JFrame controlWindow;
|
||||||
|
|
||||||
|
public ControlWindow(){
|
||||||
|
controlWindow = new JFrame();
|
||||||
|
controlWindow.setVisible(true);
|
||||||
|
controlWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+13
-435
@@ -1,457 +1,35 @@
|
|||||||
package gui;
|
package gui;
|
||||||
|
|
||||||
import draw.Draw;
|
|
||||||
import game.Control;
|
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 {
|
public class GUI {
|
||||||
|
|
||||||
private boolean sliderLocked = false;
|
private final String title = "Conway's game if Live";
|
||||||
|
|
||||||
private final Control control;
|
private final Control control;
|
||||||
|
|
||||||
/* Universial Atributes */
|
private MenuWindow menuWindow;
|
||||||
|
private GameWindow gameWindow;
|
||||||
private final String title = "Conway's Game of Life";
|
private ControlWindow controlWindow;
|
||||||
|
|
||||||
/* 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) {
|
|
||||||
|
|
||||||
|
public GUI(Control control){
|
||||||
this.control = control;
|
this.control = control;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void controlWindow() {
|
public void createMenuWindow(){
|
||||||
jfMenue = new JFrame();
|
menuWindow = new MenuWindow(title, control);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initControlComponents() {
|
public void createGameWindow(int cellCountX, int cellCountY){
|
||||||
|
gameWindow = new GameWindow(title, control, cellCountX, cellCountY);
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateStartCells() {
|
public void createControlWindow(){
|
||||||
startCells.setMaximum(xAxisSize * yAxisSize);
|
controlWindow = new ControlWindow();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateSliderLables(int slider, int val) {
|
public void showGrid(boolean[][] grid){
|
||||||
switch (slider) {
|
gameWindow.showGrid(grid);
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package gui;
|
||||||
|
|
||||||
|
import draw.Draw;
|
||||||
|
import game.Control;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.KeyListener;
|
||||||
|
|
||||||
|
public class GameWindow {
|
||||||
|
|
||||||
|
private final JFrame jfGame;
|
||||||
|
private final Draw draw;
|
||||||
|
|
||||||
|
private final int cellCountX;
|
||||||
|
private final int cellCountY;
|
||||||
|
|
||||||
|
private int width, height;
|
||||||
|
private float ratio;
|
||||||
|
|
||||||
|
public GameWindow(String title, Control control, int cellCountX, int cellCountY) {
|
||||||
|
this.cellCountX = cellCountX;
|
||||||
|
this.cellCountY = cellCountY;
|
||||||
|
|
||||||
|
calcWindowSize();
|
||||||
|
|
||||||
|
jfGame = new JFrame(title);
|
||||||
|
|
||||||
|
jfGame.getContentPane().setPreferredSize(new Dimension(width, height));
|
||||||
|
jfGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
jfGame.addKeyListener(new KeyListener() {
|
||||||
|
@Override
|
||||||
|
public void keyTyped(KeyEvent e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
if (e.isControlDown() && e.isAltDown() && e.getKeyCode() == KeyEvent.VK_C) {
|
||||||
|
control.createControlWindow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
jfGame.setResizable(false);
|
||||||
|
|
||||||
|
ratio = (float) (cellCountY) / (float) (cellCountX);
|
||||||
|
draw = new Draw(this.cellCountX, this.cellCountY, width, height, ratio);
|
||||||
|
draw.setBounds(0, 0, width, height);
|
||||||
|
|
||||||
|
draw.setVisible(true);
|
||||||
|
|
||||||
|
jfGame.add(draw);
|
||||||
|
|
||||||
|
jfGame.pack();
|
||||||
|
jfGame.setLayout(null);
|
||||||
|
jfGame.setLocationRelativeTo(null);
|
||||||
|
jfGame.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showGrid(boolean[][] grid)
|
||||||
|
{
|
||||||
|
if(draw != null)
|
||||||
|
{
|
||||||
|
draw.showGrid(grid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.err.println("you need to build the game gameWindow before showing a grid");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void calcWindowSize(){
|
||||||
|
|
||||||
|
width = (int) (cellCountX * ratio );
|
||||||
|
height = (int) (cellCountY * ratio);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,328 @@
|
|||||||
|
package gui;
|
||||||
|
|
||||||
|
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 MenuWindow {
|
||||||
|
|
||||||
|
private final JFrame menuWindow;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
private boolean sliderLocked = false;
|
||||||
|
|
||||||
|
public MenuWindow(String title, Control control){
|
||||||
|
menuWindow = new JFrame();
|
||||||
|
|
||||||
|
menuWindow.setTitle(title);
|
||||||
|
|
||||||
|
ImageIcon imageIcon = new ImageIcon("src/data/PNG/Icon.png");
|
||||||
|
menuWindow.setIconImage(imageIcon.getImage());
|
||||||
|
|
||||||
|
|
||||||
|
menuWindow.setUndecorated(false);
|
||||||
|
|
||||||
|
menuWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
menuWindow.setAutoRequestFocus(false);
|
||||||
|
menuWindow.setLayout(null);
|
||||||
|
menuWindow.setSize(752, 424);
|
||||||
|
|
||||||
|
menuWindow.setResizable(false);
|
||||||
|
|
||||||
|
|
||||||
|
initComponents(control);
|
||||||
|
addComponents();
|
||||||
|
|
||||||
|
|
||||||
|
menuWindow.setLocationRelativeTo(null);
|
||||||
|
menuWindow.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initComponents(Control control) {
|
||||||
|
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();
|
||||||
|
updateSliderLabels(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();
|
||||||
|
updateSliderLabels(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();
|
||||||
|
updateSliderLabels(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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateStartCells() {
|
||||||
|
startCells.setMaximum(xAxisSize * yAxisSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateSliderLabels(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 addComponents(){
|
||||||
|
menuWindow.add(heading);
|
||||||
|
menuWindow.add(xAxis);
|
||||||
|
menuWindow.add(xAxisName);
|
||||||
|
menuWindow.add(xAxisLabel);
|
||||||
|
menuWindow.add(yAxis);
|
||||||
|
menuWindow.add(yAxisName);
|
||||||
|
menuWindow.add(yAxisLabel);
|
||||||
|
menuWindow.add(startCells);
|
||||||
|
menuWindow.add(startCellsName);
|
||||||
|
menuWindow.add(startCellsLabel);
|
||||||
|
menuWindow.add(startButton);
|
||||||
|
menuWindow.add(manuelStart);
|
||||||
|
menuWindow.add(previewLabel);
|
||||||
|
menuWindow.add(modeName);
|
||||||
|
menuWindow.add(modeBox);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addComboBoxEntry(JComboBox<String> comboBox, String entry) {
|
||||||
|
comboBox.addItem(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user