-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathBattleshipElementView.java
More file actions
111 lines (102 loc) · 4.28 KB
/
BattleshipElementView.java
File metadata and controls
111 lines (102 loc) · 4.28 KB
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
package edu.rpi.legup.puzzle.battleship;
import edu.rpi.legup.ui.boardview.GridElementView;
import java.awt.*;
/** The view for a Battleship cell */
public class BattleshipElementView extends GridElementView {
private static final Stroke OUTLINE_STROKE = new BasicStroke(1);
private static final Color OUTLINE_COLOR = new Color(0x212121);
private static final Color UNKNOWN_COLOR = new Color(0xE0E0E0);
private static final Color WATER_COLOR = new Color(0x1565C0);
private static final Color SHIP_COLOR = new Color(0x757575);
private static final Font FONT = new Font("TimesRoman", Font.BOLD, 10);
private static final Color FONT_COLOR = new Color(0xFFEB3B);
public BattleshipElementView(BattleshipCell cell) {
super(cell);
}
@Override
/**
* Draws on the given frame based on the type of the cell of the current puzzleElement
*
* @param graphics2D the frame to be drawn on
*/
public void drawElement(Graphics2D graphics2D) {
BattleshipCell cell = (BattleshipCell) puzzleElement;
BattleshipType type = cell.getType();
switch (type) {
case UNKNOWN:
graphics2D.setColor(UNKNOWN_COLOR);
graphics2D.fillRect(location.x, location.y, size.width, size.height);
break;
case WATER:
graphics2D.setColor(WATER_COLOR);
graphics2D.fillRect(location.x, location.y, size.width, size.height);
break;
case SHIP_UNKNOWN:
graphics2D.setColor(SHIP_COLOR);
graphics2D.fillRect(
location.x + 3 * size.width / 8,
location.y + 3 * size.height / 8,
size.width / 4,
size.height / 4);
graphics2D.setColor(FONT_COLOR);
graphics2D.setFont(FONT);
FontMetrics metrics = graphics2D.getFontMetrics(FONT);
String value = "?";
int xText = location.x + (size.width - metrics.stringWidth(value)) / 2;
int yText =
location.y
+ ((size.height - metrics.getHeight()) / 2)
+ metrics.getAscent();
graphics2D.drawString(value, xText, yText);
break;
case SUBMARINE:
graphics2D.setColor(SHIP_COLOR);
graphics2D.fillOval(
location.x + size.width / 4,
location.y + size.width / 4,
size.width / 2,
size.height / 2);
break;
case SHIP_TOP:
graphics2D.setColor(SHIP_COLOR);
graphics2D.fillArc(
location.x,
location.y - size.height / 2,
size.width,
size.height,
180,
180);
break;
case SHIP_RIGHT:
graphics2D.setColor(SHIP_COLOR);
graphics2D.fillArc(
location.x + size.height / 2, location.y, size.width, size.height, 90, 180);
break;
case SHIP_BOTTOM:
graphics2D.setColor(SHIP_COLOR);
graphics2D.fillArc(
location.x, location.y + size.height / 2, size.width, size.height, 0, 180);
break;
case SHIP_LEFT:
graphics2D.setColor(SHIP_COLOR);
graphics2D.fillArc(
location.x - size.height / 2,
location.y,
size.width,
size.height,
270,
180);
break;
case SHIP_MIDDLE:
graphics2D.setColor(SHIP_COLOR);
graphics2D.fillRect(location.x, location.y, size.width, size.height);
break;
default:
graphics2D.setColor(new Color(0xE040FB));
break;
}
graphics2D.setColor(OUTLINE_COLOR);
graphics2D.setStroke(OUTLINE_STROKE);
graphics2D.drawRect(location.x, location.y, size.width, size.height);
}
}