Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

color selection added and bug fixes #25

Merged
merged 2 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/com/battleship/gui/GameBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.battleship.networking.Client;
import com.battleship.networking.NetworkConnection;
import com.battleship.networking.Server;
import com.battleship.utils.BSConfigFile;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

Expand Down Expand Up @@ -64,6 +65,7 @@ public GameBoard() {
*/
private void setShipOnBoard() {

Color shipColor = BSConfigFile.manageColors(BSConfigFile.readProperties("Color"));
for (Ship shipToPlace : ShipPlanner.board.field.values()) {

int[] h_c = shipToPlace.getHeadCoordinates();
Expand All @@ -72,12 +74,12 @@ private void setShipOnBoard() {
if (shipToPlace.isVertical()) {

for (int i = 0; i < length; i++) {
playerPositions[h_c[0]][h_c[1] + i].setBackground(Color.BLUE);
playerPositions[h_c[0]][h_c[1] + i].setBackground(shipColor);
playerPositions[h_c[0]][h_c[1] + i].setEnabled(true);
}
} else {
for (int i = 0; i < length; i++) {
playerPositions[h_c[0] + i][h_c[1]].setBackground(Color.BLUE);
playerPositions[h_c[0] + i][h_c[1]].setBackground(shipColor);
playerPositions[h_c[0] + i][h_c[1]].setEnabled(true);
}
}
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/com/battleship/gui/Settings.form
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.battleship.gui.Settings">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="7" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="520" height="400"/>
Expand All @@ -22,7 +22,7 @@
</component>
<component id="e5ee" class="javax.swing.JTextField" binding="tfWidth" custom-create="true">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
Expand All @@ -32,7 +32,7 @@
</component>
<component id="e3f08" class="javax.swing.JTextField" binding="tfHeight" custom-create="true">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
Expand All @@ -50,15 +50,15 @@
</component>
<component id="b9a3b" class="javax.swing.JButton" binding="bExit">
<constraints>
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="6" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Exit"/>
</properties>
</component>
<component id="baeed" class="javax.swing.JButton" binding="bSave">
<constraints>
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Save"/>
Expand All @@ -82,21 +82,36 @@
</component>
<component id="ae15f" class="javax.swing.JLabel" binding="labelWRes">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Resolution Width"/>
</properties>
</component>
<component id="ab705" class="javax.swing.JLabel" binding="labelHRes">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<horizontalTextPosition value="0"/>
<text value="Resolution Height"/>
</properties>
</component>
<component id="356cf" class="javax.swing.JComboBox" binding="comboSetColor" custom-create="true">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="be4ae" class="javax.swing.JLabel" binding="labelShipColor">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="356cf"/>
<text value="Ship Color"/>
</properties>
</component>
</children>
</grid>
</form>
32 changes: 19 additions & 13 deletions src/main/java/com/battleship/gui/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public class Settings implements ActionListener {
private JLabel labelImage;
private JLabel labelWRes;
private JLabel labelHRes;
private JComboBox<String> comboSetColor;
private JLabel labelShipColor;


private JComboBox<String> bSetColors;
final String[] colorsPalette = new String[]{"BLUE", "MAGENTA", "RED", "ORANGE", "BLACK", "GREEN"};
final String[] colorsPalette = new String[]{"BLUE", "MAGENTA", "RED", "ORANGE", "GREEN"};

public Settings() {
$$$setupUI$$$();
Expand Down Expand Up @@ -64,7 +64,7 @@ public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(() -> new ImageChooser(avatar_button));
} else if (source == bSave) {
String newName = tfNick.getText();
String setColor = (String) bSetColors.getSelectedItem();
String setColor = (String) comboSetColor.getSelectedItem();
String resolutionWidth = tfWidth.getText();
String resolutionHeight = tfHeight.getText();
BSConfigFile.updateConfiguration(newName, setColor, resolutionWidth, resolutionHeight);
Expand All @@ -78,8 +78,8 @@ private void createUIComponents() {

tfNick = new JTextField(BSConfigFile.readProperties("Name"));
avatar_button = new JButton(loadImage(BSConfigFile.readProperties("Avatar_Path")));
bSetColors = new JComboBox<>(colorsPalette);
bSetColors.setSelectedItem(BSConfigFile.readProperties("Color"));
comboSetColor = new JComboBox<>(colorsPalette);
comboSetColor.setSelectedItem(BSConfigFile.readProperties("Color"));
tfWidth = new JTextField(BSConfigFile.readProperties("Resolution_Width"));
tfHeight = new JTextField(BSConfigFile.readProperties("Resolution_Height"));
}
Expand All @@ -94,22 +94,22 @@ private void createUIComponents() {
private void $$$setupUI$$$() {
createUIComponents();
panel = new JPanel();
panel.setLayout(new GridLayoutManager(6, 2, new Insets(0, 0, 0, 0), -1, -1));
panel.setLayout(new GridLayoutManager(7, 2, new Insets(0, 0, 0, 0), -1, -1));
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
tfNick.setHorizontalAlignment(0);
panel.add(tfNick, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
tfWidth.setHorizontalAlignment(0);
panel.add(tfWidth, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
panel.add(tfWidth, new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
tfHeight.setHorizontalAlignment(0);
panel.add(tfHeight, new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
panel.add(tfHeight, new GridConstraints(4, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
avatar_button.setText("");
panel.add(avatar_button, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
bExit = new JButton();
bExit.setText("Exit");
panel.add(bExit, new GridConstraints(5, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
panel.add(bExit, new GridConstraints(6, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
bSave = new JButton();
bSave.setText("Save");
panel.add(bSave, new GridConstraints(4, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
panel.add(bSave, new GridConstraints(5, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
labelUserName = new JLabel();
labelUserName.setText("UserName");
panel.add(labelUserName, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
Expand All @@ -118,11 +118,16 @@ private void createUIComponents() {
panel.add(labelImage, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
labelWRes = new JLabel();
labelWRes.setText("Resolution Width");
panel.add(labelWRes, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
panel.add(labelWRes, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
labelHRes = new JLabel();
labelHRes.setHorizontalTextPosition(0);
labelHRes.setText("Resolution Height");
panel.add(labelHRes, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
panel.add(labelHRes, new GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
panel.add(comboSetColor, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
labelShipColor = new JLabel();
labelShipColor.setText("Ship Color");
panel.add(labelShipColor, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
labelShipColor.setLabelFor(comboSetColor);
}

/**
Expand All @@ -131,4 +136,5 @@ private void createUIComponents() {
public JComponent $$$getRootComponent$$$() {
return panel;
}

}
53 changes: 34 additions & 19 deletions src/main/java/com/battleship/gui/ShipPlanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.battleship.game.boardpack.Board;
import com.battleship.game.shippack.Ship;
import com.battleship.utils.BSConfigFile;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
Expand Down Expand Up @@ -35,6 +36,7 @@ public class ShipPlanner implements ActionListener {
private final boolean isServer;
private final int port;
private final String ip;
private final Color shipColor = BSConfigFile.manageColors(BSConfigFile.readProperties("Color"));

public ShipPlanner(boolean isServer, int port, String ip) {

Expand Down Expand Up @@ -221,23 +223,9 @@ public void mouseClicked(MouseEvent e) {
int shipLen = Integer.parseInt(((String) Objects.requireNonNull(comboBoxShipSelector.getSelectedItem())).substring(0, 1));

if (SwingUtilities.isRightMouseButton(e)) {
if (j + shipLen <= 10 && isValidPosition(i, j, i, j + shipLen - 1)) {
for (int l = j; l < j + shipLen; l++) {
this.disableSurrounding(i, l);
positions[i][l].setBackground(Color.BLUE);
}
board.addShip(new Ship(i, j, i, j + shipLen), (String) comboBoxShipSelector.getSelectedItem());
comboBoxShipSelector.removeItem(comboBoxShipSelector.getSelectedItem());
}
verticalShipsSurroundingArea(i, j, shipLen); //manages the surrounding area of the vertical ships
} else {
if (i + shipLen <= 10 && isValidPosition(i, j, i + shipLen - 1, j)) {
for (int l = i; l < i + shipLen; l++) {
this.disableSurrounding(l, j);
positions[l][j].setBackground(Color.BLUE);
}
board.addShip(new Ship(i, j, i + shipLen, j), (String) comboBoxShipSelector.getSelectedItem());
comboBoxShipSelector.removeItem(comboBoxShipSelector.getSelectedItem());
}
horizontalShipsSurroundingArea(i, j, shipLen);//manages the surrounding area of the horizontal ships
}
if (comboBoxItemCount == 1) {
buttonOk.setEnabled(true);
Expand All @@ -248,6 +236,33 @@ public void mouseClicked(MouseEvent e) {
}
}

private void verticalShipsSurroundingArea(int i, int j, int shipLen) {
if (j + shipLen <= 10 && isValidPosition(i, j, i, j + shipLen - 1)) {

for (int l = j; l < j + shipLen; l++) { // disables the surrounding ship area
this.disableSurrounding(i, l);
}
for (int l = j; l < j + shipLen; l++) {
positions[i][l].setBackground(shipColor); // sets the ships color to Color.BLUE
}
board.addShip(new Ship(i, j, i, j + shipLen), (String) comboBoxShipSelector.getSelectedItem());
comboBoxShipSelector.removeItem(comboBoxShipSelector.getSelectedItem());
}
}

private void horizontalShipsSurroundingArea(int i, int j, int shipLen) {
if (i + shipLen <= 10 && isValidPosition(i, j, i + shipLen - 1, j)) {
for (int l = i; l < i + shipLen; l++) { // disables the surrounding ship area
this.disableSurrounding(l, j);
}
for (int l = i; l < i + shipLen; l++) { // sets the ships color to Color.BLUE
positions[l][j].setBackground(shipColor);
}
board.addShip(new Ship(i, j, i + shipLen, j), (String) comboBoxShipSelector.getSelectedItem());
comboBoxShipSelector.removeItem(comboBoxShipSelector.getSelectedItem());
}
}

/**
* Checks if the selected position is valid for placing a ship
*
Expand All @@ -266,9 +281,9 @@ private void disableSurrounding(int x, int y) {
for (int j = y - 1; j <= y + 1; j++) {
try {
positions[i][j].setEnabled(false);
if (i != x && j != y) { // Not working properly
positions[i][j].setBackground(new Color(175, 175, 175));
}
//if (i != x && j != y) { // Not working properly
positions[i][j].setBackground(new Color(175, 175, 175));
//}
} catch (IndexOutOfBoundsException ex) {
// Nothing to do but maybe there is another way to do this
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/battleship/utils/BSConfigFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static void newFile() {
Properties props = new Properties();

props.setProperty("Name", "UserName");
props.setProperty("Avatar_Path", "src/resources/anonymous.png");
props.setProperty("Avatar_Path", "src/main/resources/anonymous.png");
props.setProperty("Resolution_Width", "1000");
props.setProperty("Resolution_Height", "500");
props.setProperty("Color", "BLUE");
Expand Down Expand Up @@ -72,7 +72,7 @@ public static Color manageColors(String colorString) {
case "GREEN" -> Color.GREEN;
case "MAGENTA" -> Color.MAGENTA;
case "ORANGE" -> Color.ORANGE;
case "BLACK" -> Color.BLACK;
case "BLACK" -> Color.BLACK; // this color has been removed because it's the default color for destroyed ships
default -> Color.BLUE;
};
}
Expand Down