From 2ac192c2306b26add1d7bc14bc7465790c2661f6 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 7 Apr 2019 16:05:15 +0200 Subject: [PATCH] Implemented custom board config dialog --- src/dev/kske/minesweeper/CustomDialog.java | 54 +++++++++++----------- src/dev/kske/minesweeper/Minesweeper.java | 4 ++ 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/dev/kske/minesweeper/CustomDialog.java b/src/dev/kske/minesweeper/CustomDialog.java index e284f58..328cd22 100644 --- a/src/dev/kske/minesweeper/CustomDialog.java +++ b/src/dev/kske/minesweeper/CustomDialog.java @@ -13,7 +13,6 @@ import javax.swing.JPanel; import javax.swing.JSlider; import javax.swing.border.EmptyBorder; - /** * Project: Minesweeper
* File: CustomDialog.java
@@ -23,8 +22,9 @@ import javax.swing.border.EmptyBorder; public class CustomDialog extends JDialog { private static final long serialVersionUID = -4019516811065781434L; - private final JPanel mcontentPanel = new JPanel(); - private final JSlider sliderBoardWidth, sliderBoardHeight, sliderNumMines; + private final JPanel mcontentPanel = new JPanel(); + + private BoardConfig result; /** * Create the dialog. @@ -43,13 +43,11 @@ public class CustomDialog extends JDialog { lblBoardWidth.setFont(new Font("Tahoma", Font.PLAIN, 14)); mcontentPanel.add(lblBoardWidth); } - { - sliderBoardWidth = new JSlider(); - sliderBoardWidth.setValue(16); - sliderBoardWidth.setMinimum(2); - sliderBoardWidth.setMaximum(30); - mcontentPanel.add(sliderBoardWidth); - } + JSlider sliderBoardWidth = new JSlider(); + sliderBoardWidth.setValue(16); + sliderBoardWidth.setMinimum(2); + sliderBoardWidth.setMaximum(30); + mcontentPanel.add(sliderBoardWidth); { JLabel label = new JLabel(""); mcontentPanel.add(label); @@ -59,13 +57,11 @@ public class CustomDialog extends JDialog { lblBoardHeight.setFont(new Font("Tahoma", Font.PLAIN, 14)); mcontentPanel.add(lblBoardHeight); } - { - sliderBoardHeight = new JSlider(); - sliderBoardHeight.setValue(16); - sliderBoardHeight.setMaximum(30); - sliderBoardHeight.setMinimum(2); - mcontentPanel.add(sliderBoardHeight); - } + JSlider sliderBoardHeight = new JSlider(); + sliderBoardHeight.setValue(16); + sliderBoardHeight.setMaximum(30); + sliderBoardHeight.setMinimum(2); + mcontentPanel.add(sliderBoardHeight); { JLabel label = new JLabel(""); mcontentPanel.add(label); @@ -75,13 +71,11 @@ public class CustomDialog extends JDialog { lblNumberOfMines.setFont(new Font("Tahoma", Font.PLAIN, 14)); mcontentPanel.add(lblNumberOfMines); } - { - sliderNumMines = new JSlider(); - sliderNumMines.setValue(16); - sliderNumMines.setMinimum(2); - sliderNumMines.setMaximum(200); - mcontentPanel.add(sliderNumMines); - } + JSlider sliderNumMines = new JSlider(); + sliderNumMines.setValue(16); + sliderNumMines.setMinimum(2); + sliderNumMines.setMaximum(200); + mcontentPanel.add(sliderNumMines); { JLabel label = new JLabel(""); mcontentPanel.add(label); @@ -93,6 +87,11 @@ public class CustomDialog extends JDialog { { JButton okButton = new JButton("Start Game"); okButton.setActionCommand("OK"); + okButton.addActionListener((evt) -> { + result = new BoardConfig(sliderBoardWidth.getValue(), sliderBoardHeight.getValue(), + sliderNumMines.getValue()); + dispose(); + }); buttonPane.add(okButton); getRootPane().setDefaultButton(okButton); } @@ -103,9 +102,10 @@ public class CustomDialog extends JDialog { buttonPane.add(cancelButton); } } - setVisible(true); } - public BoardConfig getBoardConfig() { - return new BoardConfig(sliderBoardWidth.getValue(), sliderBoardHeight.getValue(), sliderNumMines.getValue()); + + public BoardConfig showDialog() { + setVisible(true); + return result; } } diff --git a/src/dev/kske/minesweeper/Minesweeper.java b/src/dev/kske/minesweeper/Minesweeper.java index 242a929..9c2af9d 100644 --- a/src/dev/kske/minesweeper/Minesweeper.java +++ b/src/dev/kske/minesweeper/Minesweeper.java @@ -133,6 +133,10 @@ public class Minesweeper { easyMenuItem.addActionListener((evt) -> initGame(easyConfig)); mediumMenuItem.addActionListener((evt) -> initGame(mediumConfig)); hardMenuItem.addActionListener((evt) -> initGame(hardConfig)); + customMenuItem.addActionListener((evt) -> { + BoardConfig cfg = new CustomDialog(mframe).showDialog(); + if (cfg != null) initGame(cfg); + }); aboutMenuItem.addActionListener((evt) -> { JOptionPane.showMessageDialog(board, "Minesweeper version " + VERSION + "\nby Kai S. K. Engelbart"); });