93 lines
3.3 KiB
Java
93 lines
3.3 KiB
Java
package ruscz.game.gameOfProd.Windows;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.sql.Time;
|
|
|
|
public class WindowOfMenu extends JLayeredPane {
|
|
public static final int WINDOW_WIDTH = 13;
|
|
public static final int WINDOW_HEIGHT = 13;
|
|
public static final int CELL_SIZE = 50;
|
|
|
|
private static final int KEY_FULLSCREEN = 115;
|
|
private static final int KEY_ESCAPE = 27;
|
|
|
|
private JFrame game;
|
|
private WindowOfFreeMove freeMove;
|
|
private JButton startNewGame;
|
|
private JButton loadSavedGame;
|
|
private JButton exitGame;
|
|
|
|
public WindowOfMenu(JFrame game, WindowOfFreeMove freeMove) {
|
|
this.game = game;
|
|
this.freeMove = freeMove;
|
|
|
|
startNewGame = new JButton("START NEW GAME");
|
|
//startNewGame.setFocusable(false);
|
|
startNewGame.setBounds((WINDOW_WIDTH*CELL_SIZE - 200) / 2,(WINDOW_HEIGHT*CELL_SIZE - 30) / 2 -70,200,30);
|
|
startNewGame.setActionCommand("startNewGame");
|
|
startNewGame.addActionListener(actionEvent -> {
|
|
cleanUp();
|
|
game.add(freeMove);
|
|
freeMove.requestFocus();
|
|
freeMove.start(true);
|
|
});
|
|
|
|
loadSavedGame = new JButton("LOAD SAVED GAME");
|
|
//loadSavedGame.setFocusable(false);
|
|
loadSavedGame.setBounds((WINDOW_WIDTH*CELL_SIZE - 200) / 2,(WINDOW_HEIGHT*CELL_SIZE - 30) / 2,200,30);
|
|
loadSavedGame.setActionCommand("loadSavedGame");
|
|
loadSavedGame.addActionListener(actionEvent -> {
|
|
cleanUp();
|
|
game.add(freeMove);
|
|
freeMove.requestFocus();
|
|
freeMove.start(false);
|
|
});
|
|
|
|
exitGame = new JButton("EXIT GAME");
|
|
exitGame.setBounds((WINDOW_WIDTH*CELL_SIZE - 200) / 2,(WINDOW_HEIGHT*CELL_SIZE - 30) / 2 + 70,200,30);
|
|
exitGame.setActionCommand("exitGame");
|
|
exitGame.addActionListener(actionEvent -> game.dispatchEvent(new WindowEvent(game, WindowEvent.WINDOW_CLOSING)));
|
|
|
|
add(startNewGame);
|
|
add(loadSavedGame);
|
|
add(exitGame);
|
|
setVisible(false);
|
|
}
|
|
|
|
|
|
public void start(){
|
|
Timer timer = new Timer( 0, actionEvent -> {
|
|
setVisible(false);
|
|
centerContent();
|
|
this.repaint();
|
|
setVisible(true);
|
|
});
|
|
timer.start();
|
|
}
|
|
|
|
private void cleanUp() {
|
|
setVisible(false);
|
|
startNewGame.removeActionListener(startNewGame.getActionListeners()[0]);
|
|
loadSavedGame.removeActionListener(loadSavedGame.getActionListeners()[0]);
|
|
exitGame.removeActionListener(exitGame.getActionListeners()[0]);
|
|
menu().removeAll();
|
|
game.remove(this);
|
|
menu().revalidate();
|
|
menu().repaint();
|
|
freeMove.setCursor(freeMove.getToolkit().createCustomCursor(
|
|
new BufferedImage( 1, 1, BufferedImage.TYPE_INT_ARGB ),
|
|
new Point(),
|
|
null ) );
|
|
}
|
|
|
|
public WindowOfMenu menu() { return this; }
|
|
|
|
public void centerContent() {
|
|
game.getComponents()[0].setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - (WINDOW_WIDTH * CELL_SIZE)) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - (WINDOW_HEIGHT * CELL_SIZE)) / 2);
|
|
game.getComponents()[0].setSize(WINDOW_WIDTH * CELL_SIZE, WINDOW_HEIGHT * CELL_SIZE);
|
|
}
|
|
}
|