Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Improved touch
Browse files Browse the repository at this point in the history
Added button to toggle left click functionality - reveal vs. flag
  • Loading branch information
SebastianThomas committed Oct 24, 2021
1 parent 3d8e418 commit c4ab64a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 13 deletions.
3 changes: 0 additions & 3 deletions main/java/META-INF/MANIFEST.MF

This file was deleted.

65 changes: 62 additions & 3 deletions main/java/de/sth/minesweeper/MineSweeper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class MineSweeper extends JPanel {
private boolean[][] field;
private int revealed;
private int nrOfRevealedFlags;
private boolean useFlags;

public MineSweeper(TopPanel topPanel, BottomPanel bottomPanel, boolean revealFirstSelected) {
this.revealed = 0;
Expand Down Expand Up @@ -107,12 +109,14 @@ public static void start(boolean revealFirstSelected, Difficulty difficulty) {
JPanel container = new JPanel();

// Panels
TopPanel topPanel = new TopPanel(frame, revealFirstSelected);
TopPanel topPanel = new TouchTopPanel(frame, revealFirstSelected);
BottomPanel bottomPanel = new BottomPanel();

// Init game panel
MineSweeper mineSweeperGame = new MineSweeper(topPanel, bottomPanel, revealFirstSelected);

topPanel.setSweeperGame(mineSweeperGame);

container.setLayout(new BorderLayout());
container.add(topPanel, BorderLayout.NORTH);
container.add(mineSweeperGame, BorderLayout.CENTER);
Expand All @@ -130,6 +134,14 @@ public static void start(boolean revealFirstSelected, Difficulty difficulty) {
frame.setVisible(true);
}

public boolean useFlags() {
return useFlags;
}

public void setUseFlags(boolean useFlags) {
this.useFlags = useFlags;
}

public void revealFirstZero() {
int nr = (int) (Math.random() * ROWS * COLS);
int y = nr % COLS;
Expand Down Expand Up @@ -268,19 +280,22 @@ private void finishStats() {

class TopPanel extends JPanel {
private final boolean revealFirstSelected;

private final JFrame frameToDispose;
private final JButton startAgainButton;
private final JButton backToMainMenuButton;
private final JLabel label;
private final JLabel flagsLeft;
private final TimerPanel timerPanel;

protected MineSweeper sweeperGame;

protected boolean useFlags;
private boolean gameOver;

public TopPanel(JFrame frameToDispose, boolean revealFirstSelected) {
this.gameOver = false;
this.revealFirstSelected = revealFirstSelected;
this.useFlags = false;

this.setBackground(ColorConstant.BG_Color);
this.setForeground(ColorConstant.FG_LIGHTER_COLOR);
Expand Down Expand Up @@ -314,6 +329,16 @@ public TopPanel(JFrame frameToDispose, boolean revealFirstSelected) {
});
}

public void setSweeperGame(MineSweeper game) {
this.sweeperGame = game;
this.setUseFlags(true);
}

public void setUseFlags(boolean useFlags) {
this.sweeperGame.setUseFlags(useFlags);
this.useFlags = useFlags;
}

public void setFlagsLeft(int flagsLeft) {
this.flagsLeft.setText("Flags left: " + flagsLeft);
}
Expand Down Expand Up @@ -356,8 +381,42 @@ public boolean isGameOver() {
}
}

class TouchTopPanel extends TopPanel {
// "https://www.freepik.com"
public static String BOMB_FILE_PATH = "buttons/bomb.png";
// Vectors Market
public static String FLAG_FILE_PATH = "buttons/flag_icon.png";

private final JButton useFlagButton;

public TouchTopPanel(JFrame frameToDispose, boolean revealFirstSelected) {
super(frameToDispose, revealFirstSelected);

this.useFlagButton = new JButton();
this.useFlagButton.addActionListener(e -> this.setUseFlags(!this.useFlags));
this.add(this.useFlagButton);
}

@Override
public void setUseFlags(boolean useFlags) {
System.out.println("Set use flags: " + useFlags);
super.setUseFlags(useFlags);
System.out.println(this.useFlags);

String filePath = useFlags ? FLAG_FILE_PATH : BOMB_FILE_PATH;
try {
URL u = this.getClass().getResource(filePath);
if (u == null) throw new NullPointerException("Image not found");
ImageIcon icon = new ImageIcon(u);
this.useFlagButton.setIcon(icon);
} catch (NullPointerException e) {
this.useFlagButton.setText(useFlags ? "F" : "B");
}
}
}

class BottomPanel extends JPanel {
private JLabel error;
private final JLabel error;

public BottomPanel() {
this.setBackground(ColorConstant.BG_Color);
Expand Down
6 changes: 0 additions & 6 deletions main/java/de/sth/minesweeper/Start.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package de.sth.minesweeper;

import de.sth.minesweeper.difficulties.Difficulties;

public class Start {
public static void main(String... args) {
new MainMenu();
}

public static void mainGame() {
MineSweeper.start(true, Difficulties.getDifficulty(Difficulties.ADVANCED));
}
}
3 changes: 2 additions & 1 deletion main/java/de/sth/minesweeper/buttons/SweeperButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public SweeperButton(MineSweeper sweeper, GameStatistic stats, int x, int y, int

this.addActionListener(event -> {
this.stats.incrementMoves();
this.emitReveal();
if (this.game.useFlags()) this.rightClick();
else this.emitReveal();
});
this.addMouseListener(new SweeperButtonMouseAdapter(this));
}
Expand Down
Binary file modified out/minesweeper.jar
Binary file not shown.

0 comments on commit c4ab64a

Please sign in to comment.