117 lines
3.1 KiB
Java
117 lines
3.1 KiB
Java
package dev.kske.minesweeper;
|
|
|
|
import java.awt.BorderLayout;
|
|
import java.awt.EventQueue;
|
|
import java.awt.event.KeyEvent;
|
|
|
|
import javax.swing.JButton;
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JMenu;
|
|
import javax.swing.JMenuBar;
|
|
import javax.swing.JMenuItem;
|
|
import javax.swing.JOptionPane;
|
|
|
|
/**
|
|
* Project: <strong>Minesweeper</strong><br>
|
|
* File: <strong>Minesweeper.java</strong><br>
|
|
* Created: <strong>21.03.2019</strong><br>
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
*/
|
|
public class Minesweeper {
|
|
|
|
private static final String VERSION = "1.0 JE";
|
|
|
|
private JFrame mframe;
|
|
|
|
private Board board;
|
|
private final BoardConfig easyConfig = new BoardConfig(0, 48, 8, 8, 10),
|
|
mediumConfig = new BoardConfig(0, 48, 16, 16, 40), hardConfig = new BoardConfig(0, 48, 30, 16, 99);
|
|
|
|
/**
|
|
* Launch the application.
|
|
*/
|
|
public static void main(String[] args) {
|
|
EventQueue.invokeLater(new Runnable() {
|
|
|
|
public void run() {
|
|
try {
|
|
Minesweeper window = new Minesweeper();
|
|
window.mframe.setVisible(true);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create the application.
|
|
*/
|
|
public Minesweeper() {
|
|
initialize();
|
|
}
|
|
|
|
/**
|
|
* Initialize the contents of the frame.
|
|
*/
|
|
private void initialize() {
|
|
mframe = new JFrame();
|
|
mframe.setResizable(false);
|
|
mframe.setTitle("Minesweeper");
|
|
mframe.setBounds(100, 100, 198, 123);
|
|
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
createMenuBar();
|
|
|
|
board = new Board();
|
|
board.init(easyConfig);
|
|
mframe.getContentPane().setLayout(new BorderLayout(0, 0));
|
|
mframe.getContentPane().add(board, BorderLayout.CENTER);
|
|
|
|
JButton btnRestart = new JButton("Restart");
|
|
btnRestart.addActionListener((evt) -> board.reset());
|
|
mframe.getContentPane().add(btnRestart, BorderLayout.NORTH);
|
|
mframe.pack();
|
|
}
|
|
|
|
private void createMenuBar() {
|
|
var menubar = new JMenuBar();
|
|
|
|
var gameMenu = new JMenu("Game");
|
|
var aboutMenuItem = new JMenuItem("About");
|
|
|
|
var easyMenuItem = new JMenuItem("Easy");
|
|
var mediumMenuItem = new JMenuItem("Medium");
|
|
var hardMenuItem = new JMenuItem("Hard");
|
|
var customMenuItem = new JMenuItem("Custom");
|
|
|
|
gameMenu.setMnemonic(KeyEvent.VK_G);
|
|
easyMenuItem.setMnemonic(KeyEvent.VK_E);
|
|
mediumMenuItem.setMnemonic(KeyEvent.VK_M);
|
|
hardMenuItem.setMnemonic(KeyEvent.VK_H);
|
|
customMenuItem.setMnemonic(KeyEvent.VK_C);
|
|
|
|
easyMenuItem.addActionListener((evt) -> initGame(easyConfig));
|
|
mediumMenuItem.addActionListener((evt) -> initGame(mediumConfig));
|
|
hardMenuItem.addActionListener((evt) -> initGame(hardConfig));
|
|
aboutMenuItem.addActionListener((evt) -> {
|
|
JOptionPane.showMessageDialog(board, "Minesweeper version " + VERSION + "\nby Kai S. K. Engelbart");
|
|
});
|
|
|
|
gameMenu.add(easyMenuItem);
|
|
gameMenu.add(mediumMenuItem);
|
|
gameMenu.add(hardMenuItem);
|
|
gameMenu.addSeparator();
|
|
gameMenu.add(customMenuItem);
|
|
menubar.add(gameMenu);
|
|
menubar.add(aboutMenuItem);
|
|
|
|
mframe.setJMenuBar(menubar);
|
|
}
|
|
|
|
private void initGame(BoardConfig config) {
|
|
board.init(config);
|
|
mframe.pack();
|
|
}
|
|
}
|