Datasets:
ArXiv:
License:
File size: 5,538 Bytes
c574d3a |
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
//Csaba
package battleship.gui.Game;
import battleship.Logic.CellStatus;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
/**
*
* @author Csaba
*/
public class CellGUI extends JPanel {
private final Color backgroundColor = new Color(82, 137, 200);
private final Color shipColor = new Color(18, 73, 136);
private final Color waterColor = new Color(0, 0, 0);
private final int i;
private final int j;
private CellStatus cellStatus;
public CellGUI(int i, int j) {
this.i = i;
this.j = j;
cellStatus = CellStatus.Empty;
setBackground(backgroundColor);
setName("Button: " + i + ":" + j);
setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
public void setCell(CellStatus cellStatus) {
switch (cellStatus) {
case Empty:
this.cellStatus = cellStatus;
setBackground(backgroundColor);
break;
case EmptyHit:
this.cellStatus = cellStatus;
setBackground(waterColor);
break;
case NearShip:
this.cellStatus = cellStatus;
setBackground(waterColor);
break;
case Ship:
placeShip();
//paintX(this.getGraphics());
break;
case ShipHit:
this.cellStatus = cellStatus;
this.paintComponent(this.getGraphics());
break;
case ShipSunk:
this.cellStatus = cellStatus;
break;
default:
throw new AssertionError();
}
repaint();
}
private void placeShip() {
if (cellStatus == CellStatus.Empty) {
cellStatus = CellStatus.Ship;
setBackground(shipColor);
}
}
public void select() {
if (cellStatus == CellStatus.Empty) {
// setBackground(selectedColor);
setColorSelected();
}
}
public void unSelect() {
if (cellStatus == CellStatus.Empty) {
//setBackground(backgroundColor);
SetColorUnSelected();
}
}
public CellStatus getStatus() {
return cellStatus;
}
public int getI() {
return i;
}
public int getJ() {
return j;
}
public boolean isIJ(int i, int j) {
return this.i == i && this.j == j;
}
private void setColorSelected() {
int darkeningLevel = -32;
Color color = new Color(
getBackground().getRed() + darkeningLevel < 0 ? 0 : getBackground().getRed() + darkeningLevel,
getBackground().getGreen() + darkeningLevel < 0 ? 0 : getBackground().getGreen() + darkeningLevel,
getBackground().getBlue() + darkeningLevel < 0 ? 0 : getBackground().getBlue() + darkeningLevel
);
setBackground(color);
}
private void SetColorUnSelected() {
int darkeningLevel = 32;
Color color = new Color(
getBackground().getRed() + darkeningLevel > 256 ? 255 : getBackground().getRed() + darkeningLevel,
getBackground().getGreen() + darkeningLevel > 256 ? 255 : getBackground().getGreen() + darkeningLevel,
getBackground().getBlue() + darkeningLevel > 256 ? 255 : getBackground().getBlue() + darkeningLevel
);
setBackground(color);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g2);
switch (cellStatus) {
case Empty:
break;
case EmptyHit:
g2.setColor(backgroundColor);
g2.fillRect(0, 0, size().width, size().height);
g2.setColor(Color.BLUE);
g2.setStroke(new BasicStroke(2));
for (int k = 0; k < 6; k++) {
int[] x = {0, 5, 10, 15, 20, 25, 30};
int[] y = {0 + k * 5, 5 + k * 5, 0 + k * 5, 5 + k * 5, 0 + k * 5, 5 + k * 5, 0 + k * 5};
for (int i = 1; i < x.length; i++) {
g2.draw(new Line2D.Float(x[i - 1], y[i - 1], x[i], y[i]));
}
}
break;
case NearShip:
g2.setColor(Color.BLUE);
g2.setStroke(new BasicStroke(2));
for (int k = 0; k < 6; k++) {
int[] x = {0, 5, 10, 15, 20, 25, 30};
int[] y = {0 + k * 5, 5 + k * 5, 0 + k * 5, 5 + k * 5, 0 + k * 5, 5 + k * 5, 0 + k * 5};
for (int i = 1; i < x.length; i++) {
g2.draw(new Line2D.Float(x[i - 1], y[i - 1], x[i], y[i]));
}
}
break;
case Ship:
break;
case ShipHit:
g2.setColor(shipColor);
g2.fillRect(0, 0, size().width, size().height);
g2.setColor(Color.RED);
g2.setStroke(new BasicStroke(5));
g2.draw(new Line2D.Float(0, 0, 30, 30));
g2.draw(new Line2D.Float(0, 30, 30, 0));
break;
case ShipSunk:
break;
default:
throw new AssertionError();
}
}
}
|