diff --git a/src/main/java/com/battleship/gui/GameBoard.java b/src/main/java/com/battleship/gui/GameBoard.java index 56a3b68..6fe177a 100644 --- a/src/main/java/com/battleship/gui/GameBoard.java +++ b/src/main/java/com/battleship/gui/GameBoard.java @@ -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; @@ -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(); @@ -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); } } diff --git a/src/main/java/com/battleship/gui/Settings.form b/src/main/java/com/battleship/gui/Settings.form index e976321..1d632f4 100644 --- a/src/main/java/com/battleship/gui/Settings.form +++ b/src/main/java/com/battleship/gui/Settings.form @@ -1,6 +1,6 @@
- + @@ -22,7 +22,7 @@ - + @@ -32,7 +32,7 @@ - + @@ -50,7 +50,7 @@ - + @@ -58,7 +58,7 @@ - + @@ -82,7 +82,7 @@ - + @@ -90,13 +90,28 @@ - + + + + + + + + + + + + + + + + diff --git a/src/main/java/com/battleship/gui/Settings.java b/src/main/java/com/battleship/gui/Settings.java index 10e3916..e5c6b42 100644 --- a/src/main/java/com/battleship/gui/Settings.java +++ b/src/main/java/com/battleship/gui/Settings.java @@ -25,10 +25,10 @@ public class Settings implements ActionListener { private JLabel labelImage; private JLabel labelWRes; private JLabel labelHRes; + private JComboBox comboSetColor; + private JLabel labelShipColor; - - private JComboBox 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$$$(); @@ -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); @@ -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")); } @@ -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)); @@ -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); } /** @@ -131,4 +136,5 @@ private void createUIComponents() { public JComponent $$$getRootComponent$$$() { return panel; } + } diff --git a/src/main/java/com/battleship/gui/ShipPlanner.java b/src/main/java/com/battleship/gui/ShipPlanner.java index dcf84ed..ff61d9e 100644 --- a/src/main/java/com/battleship/gui/ShipPlanner.java +++ b/src/main/java/com/battleship/gui/ShipPlanner.java @@ -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; @@ -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) { @@ -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); @@ -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 * @@ -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 } diff --git a/src/main/java/com/battleship/utils/BSConfigFile.java b/src/main/java/com/battleship/utils/BSConfigFile.java index 01af3ee..a305459 100644 --- a/src/main/java/com/battleship/utils/BSConfigFile.java +++ b/src/main/java/com/battleship/utils/BSConfigFile.java @@ -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"); @@ -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; }; }