-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegment.java
113 lines (94 loc) · 3.13 KB
/
Segment.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
package battleship.ship;
import battleship.Team;
import battleship.ship.segment.Position;
import battleship.ship.segment.State;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author j9neave
*/
public class Segment extends javax.swing.JPanel {
private State state;
private final Team team;
private final Position pos;
/**
* Creates new form Segment
* @param xpos
* @param ypos
* @param defaultState
* @param team
*/
public Segment(int xpos, int ypos, State defaultState, Team team) {
initComponents();
this.team = team;
this.state = defaultState;
this.updateColor();
this.pos = new Position(xpos, ypos);
}
public State getState() {
return this.state;
}
public void setState(State state) {
this.state = state;
this.updateColor();
}
public Position getPos() {
return this.pos;
}
public final void updateColor() {
java.awt.Color color;
switch(this.state) {
case UNKNOWN -> color = new java.awt.Color(195, 195, 195);
case ALIVE -> color = new java.awt.Color( 0, 162, 232);
case DEAD -> color = new java.awt.Color(237, 28, 36);
case MISS -> color = new java.awt.Color(127, 127, 127);
case HIT -> color = new java.awt.Color(255, 127, 39);
default -> color = new java.awt.Color(240, 240, 240);
}
this.setBackground(color);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
formMousePressed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
// TODO add your handling code here:
switch(this.state) {
case UNKNOWN -> {
if (this.team.getTeamShipGrid()[this.getPos().getX()][this.getPos().getY()]) {
this.setState(State.HIT);
} else if (!this.team.getTeamShipGrid()[this.getPos().getX()][this.getPos().getY()]) {
this.setState(State.MISS);
Team.FRIENDLY.CPUHit();
}
}
case ALIVE -> this.setState(State.DEAD);
}
}//GEN-LAST:event_formMousePressed
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}