Adjusted font and grid, cleaned up BoardConfig, working on CustomDialog

This commit is contained in:
Kai S. K. Engelbart 2019-04-05 14:52:49 +02:00
parent 75b1bff0a1
commit a83fe6819b
4 changed files with 29 additions and 26 deletions

View File

@ -1,10 +1,12 @@
package dev.kske.minesweeper;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
@ -39,7 +41,7 @@ public class Board extends JPanel {
static {
icons = new HashMap<>();
final String[] names = { "mine2", "mine4", "tile", "tile3", "flag" };
final String[] names = { "mine2", "mine4", "tile", "tile3" };
for (String name : names) {
icons.put(name, TextureLoader.loadScaledImage(name, tileSize));
}
@ -116,11 +118,9 @@ public class Board extends JPanel {
Tile tile = board[i][j];
int x = i * tileSize, y = j * tileSize;
// Draw background with grid
// Draw background
g.setColor(Color.gray);
g.fillRect(x, y, x + tileSize, y + tileSize);
g.setColor(Color.black);
g.drawRect(x, y, x + tileSize, y + tileSize);
// Draw tile with normal mine
if (gameState == GameState.LOST && tile.isMine()) g.drawImage(icons.get("mine2"), x, y, this);
@ -135,18 +135,24 @@ public class Board extends JPanel {
else if (tile.isDrawSurroundingMines() && tile.getSurroundingMines() > 0) {
// Draw number of surrounding mines
String numStr = String.valueOf(tile.getSurroundingMines());
g.setFont(new Font("Helvetica", Font.PLAIN, 18));
g.setFont(new Font("Arial", Font.BOLD, 18));
g.setColor(Color.red);
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth(numStr), h = fm.getHeight();
g.drawString(numStr, x + tileSize / 2 - w / 2, y + tileSize / 2 + h / 2);
g.drawString(numStr, x + (tileSize - w) / 2, y + (tileSize - h) / 2 + fm.getAscent());
}
}
// Draw flagged tile
else if (tile.isFlagged()) g.drawImage(icons.get("flag"), x, y, this);
else if (tile.isFlagged()) g.drawImage(icons.get("tile3"), x, y, this);
// Draw normal tile
else g.drawImage(icons.get("tile"), x, y, this);
// Draw grid
((Graphics2D) g).setStroke(new BasicStroke(2.0f));
g.setColor(Color.black);
g.drawRect(x, y, x + tileSize, y + tileSize);
}
}

View File

@ -8,11 +8,9 @@ package dev.kske.minesweeper;
*/
public class BoardConfig {
public final int x, y, width, height, mines;
public final int width, height, mines;
public BoardConfig(int x, int y, int width, int height, int mines) {
this.x = x;
this.y = y;
public BoardConfig(int width, int height, int mines) {
this.width = width;
this.height = height;
this.mines = mines;

View File

@ -24,6 +24,7 @@ public class CustomDialog extends JDialog {
private static final long serialVersionUID = -4019516811065781434L;
private final JPanel mcontentPanel = new JPanel();
private final JSlider sliderBoardWidth, sliderBoardHeight, sliderNumMines;
/**
* Create the dialog.
@ -43,7 +44,7 @@ public class CustomDialog extends JDialog {
mcontentPanel.add(lblBoardWidth);
}
{
JSlider sliderBoardWidth = new JSlider();
sliderBoardWidth = new JSlider();
sliderBoardWidth.setValue(16);
sliderBoardWidth.setMinimum(2);
sliderBoardWidth.setMaximum(30);
@ -59,7 +60,7 @@ public class CustomDialog extends JDialog {
mcontentPanel.add(lblBoardHeight);
}
{
JSlider sliderBoardHeight = new JSlider();
sliderBoardHeight = new JSlider();
sliderBoardHeight.setValue(16);
sliderBoardHeight.setMaximum(30);
sliderBoardHeight.setMinimum(2);
@ -75,11 +76,11 @@ public class CustomDialog extends JDialog {
mcontentPanel.add(lblNumberOfMines);
}
{
JSlider slider = new JSlider();
slider.setValue(16);
slider.setMinimum(2);
slider.setMaximum(200);
mcontentPanel.add(slider);
sliderNumMines = new JSlider();
sliderNumMines.setValue(16);
sliderNumMines.setMinimum(2);
sliderNumMines.setMaximum(200);
mcontentPanel.add(sliderNumMines);
}
{
JLabel label = new JLabel("");
@ -98,10 +99,13 @@ public class CustomDialog extends JDialog {
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
cancelButton.addActionListener((evt) -> dispose());
buttonPane.add(cancelButton);
}
}
setVisible(true);
}
public BoardConfig getBoardConfig() {
return new BoardConfig(sliderBoardWidth.getValue(), sliderBoardHeight.getValue(), sliderNumMines.getValue());
}
}

View File

@ -29,8 +29,8 @@ public class Minesweeper {
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);
private final BoardConfig easyConfig = new BoardConfig(8, 8, 10), mediumConfig = new BoardConfig(16, 16, 40),
hardConfig = new BoardConfig(30, 16, 99);
/**
* Launch the application.
@ -79,7 +79,6 @@ public class Minesweeper {
mframe.getContentPane().setLayout(new BorderLayout(0, 0));
mframe.getContentPane().add(board, BorderLayout.CENTER);
JPanel headerPanel = new JPanel();
mframe.getContentPane().add(headerPanel, BorderLayout.NORTH);
headerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
@ -134,10 +133,6 @@ public class Minesweeper {
easyMenuItem.addActionListener((evt) -> initGame(easyConfig));
mediumMenuItem.addActionListener((evt) -> initGame(mediumConfig));
hardMenuItem.addActionListener((evt) -> initGame(hardConfig));
customMenuItem.addActionListener((evt) -> {
var dlg = new CustomDialog(mframe);
});
aboutMenuItem.addActionListener((evt) -> {
JOptionPane.showMessageDialog(board, "Minesweeper version " + VERSION + "\nby Kai S. K. Engelbart");
});