Working on drawing, added icons and loading routine

This commit is contained in:
Kai S. K. Engelbart 2019-03-25 07:04:12 +01:00
parent cf32276388
commit c872f5b670
17 changed files with 68 additions and 15 deletions

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="res"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>

BIN
res/Flag.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

BIN
res/Mine.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

BIN
res/Mine2.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

BIN
res/Mine3.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 KiB

BIN
res/Mine4.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

BIN
res/Smiley.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

BIN
res/Smiley1.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

BIN
res/Smiley2.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB

BIN
res/Smiley3.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

BIN
res/Tile.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

BIN
res/Tile2.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

BIN
res/Tile3.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

BIN
res/Tile4.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

View File

@ -1,10 +1,13 @@
package dev.kske.minesweeper;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.JFrame;
/**
* Project: <strong>Minesweeper</strong><br>
* File: <strong>Board.java</strong><br>
@ -13,20 +16,21 @@ import java.util.Random;
*/
public class Board {
private int tileSize, width, height;
private Rectangle screen;
private BufferedImage[] icons;
private int tileSize, width, height;
private Rectangle screen;
private GameState gameState;
private int mines, activeTiles, flaggedTiles;
private Tile[][] board;
public Board(int x, int y, int width, int height, int tileSize, int mines) {
private final JFrame viewport;
public Board(int x, int y, int width, int height, int tileSize, int mines, JFrame viewport) {
this.tileSize = tileSize;
this.width = width;
this.height = height;
screen = new Rectangle(x, y, x + width * tileSize, y + height * tileSize);
// TODO: Init icons
this.viewport = viewport;
gameState = GameState.ACTIVE;
this.mines = mines;
@ -91,12 +95,12 @@ public class Board {
touchTile(i, j);
// Redraw the touched tile
// TODO: Draw tile
drawTile(n, m);
}
}
public void flagTile(Point tilePos) {
Tile tile = board[tilePos.x][tilePos.y];
public void flagTile(int n, int m) {
Tile tile = board[n][m];
if (!tile.isTouched()) {
if (tile.isFlagged()) {
tile.setFlagged(false);
@ -105,7 +109,28 @@ public class Board {
tile.setFlagged(true);
flaggedTiles++;
}
// TODO: Draw tile
drawTile(n, m);
}
}
public void draw() {
for(int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
drawTile(i, j);
}
public void drawTile(int n, int m) {
Tile tile = board[n][m];
int x = screen.x + n * tileSize, y = screen.y + m * tileSize;
Graphics g = viewport.getGraphics();
// Draw background with grid
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 all mines if the game is lost
// if(gameState == GameState.LOST)
}
}

View File

@ -14,6 +14,8 @@ public class Minesweeper {
private JFrame mframe;
private Board board;
/**
* Launch the application.
*/
@ -47,6 +49,9 @@ public class Minesweeper {
mframe.setTitle("Minesweeper");
mframe.setBounds(100, 100, 450, 300);
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board = new Board(0, 0, 10, 10, 32, 10, mframe);
board.draw();
}
}

View File

@ -1,5 +1,13 @@
package dev.kske.minesweeper;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
/**
* Project: <strong>Minesweeper</strong><br>
* File: <strong>Tile.java</strong><br>
@ -8,17 +16,31 @@ package dev.kske.minesweeper;
*/
public class Tile {
private static Map<String, BufferedImage> icons;
private boolean mine, flagged, touched;
private boolean drawSurroundingMines;
private int surroundingMines;
static {
icons = new HashMap<>();
final String[] names = { "Mine2", "Mine4", "Tile", "Tile3" };
for (String name : names) {
URL file = Tile.class.getResource(name + ".ico");
try {
icons.put(name, ImageIO.read(file));
} catch (IOException e) {
System.err.println("Error loading texture: " + name);
e.printStackTrace();
}
}
}
public Tile() {
mine = false;
flagged = false;
touched = false;
drawSurroundingMines = false;
surroundingMines = 0;
mine = flagged = touched = drawSurroundingMines = false;
surroundingMines = 0;
}
public boolean isMine() { return mine; }