-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameGUI.java
118 lines (102 loc) · 2.95 KB
/
GameGUI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
provides the visual GUI for the program
*/
import java.awt.Color;
import java.awt.Container;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.WindowConstants;
import javax.swing.text.StyleConstants;
public class GameGUI extends JFrame {
private static Container contentPane;
private int cellSize = 40;
private JLabel[][] labels;
private int[][] board;
/**
* Constructor for GUI to set up the whole interface.
* @param gameBoard
* @param testing
*/
public GameGUI (int[][] gameBoard, boolean testing) {
super("Battleship Game");
board = gameBoard;
int numRows = Config.BOARD_LENGTH, numCols = Config.BOARD_WIDTH;
contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(Color.white);
setSize(numCols*cellSize + 47 + 40, numRows*cellSize + 65 + 40);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
ImageIcon icon;
labels = new JLabel[numRows][numCols];
JLabel numLbl;
// Add numbers and letters along grid axes.
for (int j = 0; j < numCols; j++) {
numLbl = new JLabel(String.valueOf(j+1));
numLbl.setSize(cellSize, cellSize);
numLbl.setLocation(j*cellSize + 45, 0);
numLbl.setHorizontalAlignment(JLabel.CENTER);
contentPane.add(numLbl);
}
for (int i = 0; i < numRows; i++) {
numLbl = new JLabel(String.valueOf(Config.letters[i]));
numLbl.setSize(cellSize, cellSize);
numLbl.setLocation(0, i*cellSize + 50);
numLbl.setHorizontalAlignment(JLabel.CENTER);
contentPane.add(numLbl);
}
JLabel cell;
for (int j = 0; j < numCols; j++) {
for (int i = 0; i < numRows; i++) {
cell = new JLabel();
cell.setSize(cellSize, cellSize);
cell.setLocation(j*cellSize + 45, i*cellSize + 50);
if (testing) {
if (gameBoard[i][j] == -1) {
cell.setBackground(Color.blue);
} else {
cell.setBackground(Color.gray);
}
} else {
cell.setBackground(Color.blue);
}
cell.setOpaque(true);
cell.setBorder(BorderFactory.createLineBorder(Color.black));
labels[i][j] = cell;
contentPane.add(cell);
}
}
addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
});
contentPane.setFocusable(true);
contentPane.requestFocusInWindow();
revalidate();
repaint();
}
/**
* Checks if the chosen cell contains a ship or just water and indicates the result with a colour.
* @param r
* @param c
*/
public void updateCell (int r, int c) {
if (board[r][c] == -1) {
labels[r][c].setBackground(Color.red);
} else {
labels[r][c].setBackground(Color.green);
}
revalidate();
repaint();
}
}