package com.javatmz.javaapp.minesweeper;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
public class Minesweeper extends JFrame {
private static final int D_W = 500;
private static final int D_H = 500;
DrawPanel drawPanel = new DrawPanel();
boolean gameOver = false;
int width = 32;
int[][] grid = new int[12][12];
int[][] sGrid = new int[12][12];
public Minesweeper() {
add(drawPanel);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
Random rx = new Random();
if (rx.nextInt(32) % 5 == 0) {
sGrid[i][j] = 9;
}
grid[i][j] = 10;
}
}
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (sGrid[i][j] == 9) {
continue;
}
int n = 0;
if (sGrid[i + 1][j] == 9)
n++;
if (sGrid[i][j + 1] == 9)
n++;
if (sGrid[i - 1][j] == 9)
n++;
if (sGrid[i][j - 1] == 9)
n++;
if (sGrid[i + 1][j + 1] == 9)
n++;
if (sGrid[i - 1][j - 1] == 9)
n++;
if (sGrid[i - 1][j + 1] == 9)
n++;
if (sGrid[i + 1][j - 1] == 9)
n++;
sGrid[i][j] = n;
}
}
drawPanel.repaint();
}
private class DrawPanel extends JPanel {
public DrawPanel() {
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
String VK_RIGHT = "VK_RIGHT";
KeyStroke WVK_RIGHT = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);
inputMap.put(WVK_RIGHT, VK_RIGHT);
actionMap.put(VK_RIGHT, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
drawPanel.repaint();
}
});
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
if (!gameOver) {
Point pos = me.getPoint();
int x = pos.x / width;
int y = pos.y / width;
grid[x][y] = 12;
if (sGrid[x][y] == 9) {
gameOver = true;
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
grid[i][j] = 12;
}
}
}
drawPanel.repaint();
}
// System.out.println(x+" "+y);
}
});
/*
* ActionListener listener = new AbstractAction() { public void
* actionPerformed(ActionEvent e) {
*
* } }; Timer timer = new Timer(10, listener);
*
* timer.start();
*/
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawValuesSteps(g);
if (gameOver) {
drawValues(g);
}
}
private void drawValuesSteps(Graphics g) {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
Graphics gG = g;
if (grid[i][j] == 12 && sGrid[i][j] == 0) {
gG.setColor(Color.black);
gG.drawRect(i * width, j * width, width, width);
continue;
}
gG.setColor(Color.black);
if (sGrid[i][j] != 9) {
gG.setFont(new Font("My font", 20, 20));
if (sGrid[i][j] == 1)
gG.setColor(Color.black);
if (sGrid[i][j] == 2)
gG.setColor(Color.green);
if (sGrid[i][j] == 3)
gG.setColor(Color.blue);
if (sGrid[i][j] == 4)
gG.setColor(Color.red);
gG.drawString(sGrid[i][j] + "", i * width + width / 2, j * width + width / 2);
} else {
gG.fillOval(i * width + width / 3, j * width + width / 3, 15, 15);
}
if (grid[i][j] != 12) {
gG.setColor(Color.gray);
gG.fillRect(i * width, j * width, width, width);
}
gG.setColor(Color.black);
gG.drawRect(i * width, j * width, width, width);
}
}
}
private void drawValues(Graphics g) {
Graphics yellowg = (Graphics) g;
yellowg.setColor(Color.blue);
yellowg.drawString("Oops Game over!", 250, 450);
// yellowg.fillRect(currentX - unit, currentY - 4 * unit, unit, 3 *
// unit);
}
public Dimension getPreferredSize() {
return new Dimension(D_W, D_H);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Minesweeper();
}
});
}
}
Comments
Post a Comment