minesweeper/src/dev/kske/minesweeper/CustomDialog.java

119 lines
3.8 KiB
Java

package dev.kske.minesweeper;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
/**
* Project: <strong>Minesweeper</strong><br>
* File: <strong>CustomDialog.java</strong><br>
* Created: <strong>03.04.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
public class CustomDialog extends JDialog {
private static final long serialVersionUID = -4019516811065781434L;
private final JPanel mcontentPanel = new JPanel();
private BoardConfig result;
/**
* Create the dialog.
*
* @param owner the frame on top of which the dialog will be displayed
*/
public CustomDialog(Frame owner) {
super(owner, ModalityType.APPLICATION_MODAL);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
mcontentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(mcontentPanel, BorderLayout.CENTER);
mcontentPanel.setLayout(new GridLayout(0, 3, 0, 0));
{
JLabel lblBoardWidthText = new JLabel("Board Width:");
lblBoardWidthText.setFont(new Font("Tahoma", Font.PLAIN, 14));
mcontentPanel.add(lblBoardWidthText);
}
JLabel lblBoardWidth = new JLabel("");
lblBoardWidth.setFont(new Font("Tahoma", Font.PLAIN, 14));
mcontentPanel.add(lblBoardWidth);
JSlider sliderBoardWidth = new JSlider();
sliderBoardWidth.addChangeListener(
(evt) -> lblBoardWidth.setText(String.valueOf(sliderBoardWidth.getValue()))
);
sliderBoardWidth.setValue(16);
sliderBoardWidth.setMinimum(2);
sliderBoardWidth.setMaximum(30);
mcontentPanel.add(sliderBoardWidth);
{
JLabel lblBoardHeightText = new JLabel("Board Height:");
lblBoardHeightText.setFont(new Font("Tahoma", Font.PLAIN, 14));
mcontentPanel.add(lblBoardHeightText);
}
JLabel lblBoardHeight = new JLabel("");
lblBoardHeight.setFont(new Font("Tahoma", Font.PLAIN, 14));
mcontentPanel.add(lblBoardHeight);
JSlider sliderBoardHeight = new JSlider();
sliderBoardHeight
.addChangeListener((evt) -> lblBoardHeight.setText(String.valueOf(sliderBoardHeight.getValue())));
sliderBoardHeight.setValue(16);
sliderBoardHeight.setMaximum(30);
sliderBoardHeight.setMinimum(2);
mcontentPanel.add(sliderBoardHeight);
{
JLabel lblNumberOfMinesText = new JLabel("Number of Mines:");
lblNumberOfMinesText.setFont(new Font("Tahoma", Font.PLAIN, 14));
mcontentPanel.add(lblNumberOfMinesText);
}
JLabel lblNumMines = new JLabel("");
lblNumMines.setFont(new Font("Tahoma", Font.PLAIN, 14));
mcontentPanel.add(lblNumMines);
JSlider sliderNumMines = new JSlider();
sliderNumMines.addChangeListener(
(evt) -> lblNumMines.setText(String.valueOf(sliderNumMines.getValue()))
);
sliderNumMines.setValue(16);
sliderNumMines.setMinimum(2);
sliderNumMines.setMaximum(200);
mcontentPanel.add(sliderNumMines);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
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);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
cancelButton.addActionListener((evt) -> dispose());
buttonPane.add(cancelButton);
}
}
}
/**
* Displays the dialog.
*
* @return the board configuration defined by the user
*/
public BoardConfig showDialog() {
setVisible(true);
return result;
}
}