From f1e4928445e878289c417c205433f7f975fef81d Mon Sep 17 00:00:00 2001 From: "ityurner02@mail.ru" Date: Thu, 22 Dec 2022 20:45:05 +0400 Subject: [PATCH 1/2] =?UTF-8?q?3=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../antiaircraftgun/ControllerArmoredCar.java | 59 +++- .../antiaircraftgun/ControllerMap.java | 182 ------------ .../ControllerMapWithSetArmoredCars.java | 280 ++++++++++++++++++ .../ControllerSetMixedArmoredCars.java | 181 +++++++++++ .../antiaircraftgun/DrawingArmoredCar.java | 5 + .../antiaircraftgun/FormArmoredCar.java | 5 + .../FormMapWithSetArmoredCars.java | 113 +++++++ ...mMap.java => FormSetMixedArmoredCars.java} | 13 +- .../MapWithSetArmoredCarsGeneric.java | 126 ++++++++ .../SetArmoredCarsGeneric.java | 87 ++++++ .../SetMixedArmoredCarsGeneric.java | 68 +++++ .../form-armored-vehicle-view.fxml | 15 +- ...rm-map-with-set-armored-vehicles-view.fxml | 72 +++++ ...form-set-mixed-armored-vehicles-view.fxml} | 41 +-- 14 files changed, 997 insertions(+), 250 deletions(-) delete mode 100644 AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerMap.java create mode 100644 AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerMapWithSetArmoredCars.java create mode 100644 AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerSetMixedArmoredCars.java create mode 100644 AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormMapWithSetArmoredCars.java rename AntiAircraftGun/src/main/java/com/example/antiaircraftgun/{FormMap.java => FormSetMixedArmoredCars.java} (82%) create mode 100644 AntiAircraftGun/src/main/java/com/example/antiaircraftgun/MapWithSetArmoredCarsGeneric.java create mode 100644 AntiAircraftGun/src/main/java/com/example/antiaircraftgun/SetArmoredCarsGeneric.java create mode 100644 AntiAircraftGun/src/main/java/com/example/antiaircraftgun/SetMixedArmoredCarsGeneric.java create mode 100644 AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-map-with-set-armored-vehicles-view.fxml rename AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/{form-map-view.fxml => form-set-mixed-armored-vehicles-view.fxml} (62%) diff --git a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerArmoredCar.java b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerArmoredCar.java index 34e514d..cf1a250 100644 --- a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerArmoredCar.java +++ b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerArmoredCar.java @@ -5,6 +5,7 @@ import javafx.fxml.FXML; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.control.Button; +import javafx.scene.control.ColorPicker; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.layout.FlowPane; @@ -25,8 +26,6 @@ public class ControllerArmoredCar { @FXML private Label labelWeightValue; @FXML - private Label labelBodyColorValue; - @FXML private ComboBox comboBoxNumOfRollers; @FXML private ComboBox comboBoxOrnamentType; @@ -42,8 +41,21 @@ public class ControllerArmoredCar { private Button buttonUp; @FXML private Button buttonDown; + @FXML + private Button buttonSelectArmoredCar; + @FXML + private ColorPicker bodyColorPicker; + @FXML + private ColorPicker dopColorPicker; private final double rootPadding = 10.0; private DrawingArmoredCar _armoredCar; + private boolean bodyColorPickerChanged = false; + private boolean dopColorPickerChanged = false; + + public DrawingArmoredCar GetSelectedArmoredCar() + { + return _armoredCar; + } @FXML public void initialize() @@ -74,8 +86,12 @@ public class ControllerArmoredCar { void ButtonCreate_Click() { Random rnd = new Random(); + if (!bodyColorPickerChanged) + { + bodyColorPicker.setValue(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + } _armoredCar = new DrawingArmoredCar(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, - Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + bodyColorPicker.getValue()); SetData(); Draw(); } @@ -84,10 +100,16 @@ public class ControllerArmoredCar { void ButtonCreateModif_Click() { Random rnd = new Random(); + if (!bodyColorPickerChanged) + { + bodyColorPicker.setValue(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + } + if (!dopColorPickerChanged) + { + dopColorPicker.setValue(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + } _armoredCar = new DrawingAntiAircraftGun(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, - Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - (rnd.nextInt(2) != 0), (rnd.nextInt(2) != 0)); + bodyColorPicker.getValue(), dopColorPicker.getValue(), (rnd.nextInt(2) != 0), (rnd.nextInt(2) != 0)); SetData(); Draw(); } @@ -99,7 +121,7 @@ public class ControllerArmoredCar { { return; } - String buttonName = ((Button) event.getSource()).getId(); + String buttonName = ((Button)event.getSource()).getId(); switch (buttonName) { case "buttonUp" -> _armoredCar.MoveTransport(Direction.Up); @@ -129,6 +151,17 @@ public class ControllerArmoredCar { Draw(); } } + @FXML + private void BodyColorPicker_Changed() + { + bodyColorPickerChanged = true; + } + + @FXML + private void DopColorPicker_Changed() + { + dopColorPickerChanged = true; + } private void ChangeDrawingRollers() { @@ -181,19 +214,16 @@ public class ControllerArmoredCar { buttonRight.setTranslateY(rootHeight - flowPaneHeight - buttonMoveSize - rootPadding); buttonRight.setTranslateX(rootWidth - rootPadding - buttonMoveSize); + + buttonSelectArmoredCar.setTranslateY(rootHeight - flowPaneHeight - buttonCreateHeight - rootPadding); + buttonSelectArmoredCar.setTranslateX(rootWidth - rootPadding - buttonMoveSize * 3.0 - + distanceBetweenButtons * 3.0 - buttonSelectArmoredCar.getWidth()); } private void Draw() { GraphicsContext gc = canvas.getGraphicsContext2D(); - gc.clearRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); - gc.setFill(Color.WHITE); - gc.fillRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); - - gc.setStroke(Color.BLACK); - gc.setLineWidth(4); - gc.strokeRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); if (_armoredCar != null) { @@ -208,6 +238,5 @@ public class ControllerArmoredCar { _armoredCar.SetPosition(rnd.nextInt(90) + 10, rnd.nextInt(90) + 5, (int) canvas.getWidth(), (int) canvas.getHeight()); labelSpeedValue.setText(Integer.toString(_armoredCar.GetArmoredCar().GetSpeed())); labelWeightValue.setText(Double.toString(_armoredCar.GetArmoredCar().GetWeight())); - labelBodyColorValue.setText(_armoredCar.GetArmoredCar().GetBodyColor().toString()); } } diff --git a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerMap.java b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerMap.java deleted file mode 100644 index a1dee09..0000000 --- a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerMap.java +++ /dev/null @@ -1,182 +0,0 @@ -package com.example.antiaircraftgun; - -import javafx.event.ActionEvent; -import javafx.fxml.FXML; -import javafx.scene.canvas.Canvas; -import javafx.scene.canvas.GraphicsContext; -import javafx.scene.control.Button; -import javafx.scene.control.ComboBox; -import javafx.scene.control.Label; -import javafx.scene.layout.FlowPane; -import javafx.scene.layout.Pane; -import javafx.scene.paint.Color; - -import java.util.Random; - -public class ControllerMap { - @FXML - private Pane root; - @FXML - private Canvas canvas; - @FXML - private FlowPane flowPane; - @FXML - private Label labelSpeedValue; - @FXML - private Label labelWeightValue; - @FXML - private Label labelBodyColorValue; - @FXML - private ComboBox comboBoxNumOfRollers; - @FXML - private ComboBox comboBoxOrnamentType; - @FXML - private ComboBox comboBoxSelectorMap; - @FXML - private Button buttonCreate; - @FXML - private Button buttonCreateModif; - @FXML - private Button buttonLeft; - @FXML - private Button buttonRight; - @FXML - private Button buttonUp; - @FXML - private Button buttonDown; - private final double buttonMargin = 10.0; - private AbstractMap _abstractMap; - - @FXML - public void initialize() - { - buttonCreate.setTranslateX(buttonMargin); - - root.widthProperty().addListener((obs, oldVal, newVal) -> - { - UpdateGUI(); - if (_abstractMap != null) - { - _abstractMap.DrawMapWithObject(); - } - }); - root.heightProperty().addListener((obs, oldVal, newVal) -> - { - UpdateGUI(); - if (_abstractMap != null) - { - _abstractMap.DrawMapWithObject(); - } - }); - } - - @FXML - void ButtonCreate_Click() - { - Random rnd = new Random(); - DrawingArmoredCar armoredCar = new DrawingArmoredCar(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, - Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); - SetData(armoredCar); - } - - @FXML - void ButtonCreateModif_Click() - { - Random rnd = new Random(); - DrawingAntiAircraftGun armoredCar = new DrawingAntiAircraftGun(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, - Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - (rnd.nextInt(2) != 0), (rnd.nextInt(2) != 0)); - SetData(armoredCar); - } - - @FXML - void ButtonMove_Click(ActionEvent event) - { - if (_abstractMap == null) - { - return; - } - String buttonName = ((Button) event.getSource()).getId(); - switch (buttonName) - { - case "buttonUp" -> _abstractMap.MoveObject(Direction.Up); - case "buttonDown" -> _abstractMap.MoveObject(Direction.Down); - case "buttonLeft" -> _abstractMap.MoveObject(Direction.Left); - case "buttonRight" -> _abstractMap.MoveObject(Direction.Right); - } - } - - @FXML - private void ComboBoxSelectorMap_Changed() - { - switch (comboBoxSelectorMap.getValue()) - { - case "Simple map" -> _abstractMap = new SimpleMap(); - case "Training ground map" -> _abstractMap = new TrainingGroundMap(); - case "Desert map" -> _abstractMap = new DesertMap(); - } - } - - private void ChangeDrawningTrackRollers(DrawingArmoredCar armoredCar) - { - IDrawingAdditionalElement newDrawningTrackRollers = switch (comboBoxOrnamentType.getValue()) - { - case "None" -> new DrawingRollers(armoredCar.GetArmoredCar().GetBodyColor()); - case "Rectangle" -> new DrawingRectangleOrnamentRollers(armoredCar.GetArmoredCar().GetBodyColor()); - case "Oval" -> - new DrawingOvalOrnamentRollers(armoredCar.GetArmoredCar().GetBodyColor()); - default -> null; - }; - if (newDrawningTrackRollers != null) - { - armoredCar.SetDrawingRollers(newDrawningTrackRollers); - armoredCar.GetDrawingRollers().SetNumberRollers(Integer.parseInt(comboBoxNumOfRollers.getValue())); - } - } - - private void UpdateGUI() - { - double sceneWidth = root.getWidth(); - double sceneHeight = root.getHeight(); - - double flowPaneHeight = flowPane.getHeight(); - double buttonCreateHeight = buttonCreate.getHeight(); - - canvas.setWidth(sceneWidth); - flowPane.setPrefWidth(sceneWidth); - canvas.setHeight(sceneHeight - flowPaneHeight); - flowPane.setTranslateY(sceneHeight - flowPaneHeight); - - buttonCreate.setTranslateY(sceneHeight - flowPaneHeight - buttonCreateHeight - buttonMargin); - buttonCreateModif.setTranslateY(sceneHeight - flowPaneHeight - buttonCreateHeight - buttonMargin); - - int buttonMoveHeight = 30; - int distanceBetweenMoveButtons = 5; - buttonUp.setTranslateY(sceneHeight - flowPaneHeight - buttonMoveHeight * 2.0 - buttonMargin - - distanceBetweenMoveButtons); - int buttonMoveWidth = 30; - buttonUp.setTranslateX(sceneWidth - buttonMargin - buttonMoveWidth * 2.0 - distanceBetweenMoveButtons); - - buttonDown.setTranslateY(sceneHeight - flowPaneHeight - buttonMoveHeight - buttonMargin); - buttonDown.setTranslateX(sceneWidth- buttonMargin - buttonMoveWidth * 2.0 - distanceBetweenMoveButtons); - - buttonLeft.setTranslateY(sceneHeight - flowPaneHeight - buttonMoveHeight - buttonMargin); - buttonLeft.setTranslateX(sceneWidth - buttonMargin - buttonMoveWidth * 3.0 - - distanceBetweenMoveButtons * 2.0); - - buttonRight.setTranslateY(sceneHeight - flowPaneHeight - buttonMoveHeight - buttonMargin); - buttonRight.setTranslateX(sceneWidth - buttonMargin - buttonMoveWidth); - } - - private void SetData(DrawingArmoredCar armoredCar) - { - ChangeDrawningTrackRollers(armoredCar); - labelSpeedValue.setText(Integer.toString(armoredCar.GetArmoredCar().GetSpeed())); - labelWeightValue.setText(Double.toString(armoredCar.GetArmoredCar().GetWeight())); - labelBodyColorValue.setText(armoredCar.GetArmoredCar().GetBodyColor().toString()); - GraphicsContext gc = canvas.getGraphicsContext2D(); - gc.clearRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); - _abstractMap.CreateMap((int)canvas.getWidth(), (int)canvas.getHeight(), new DrawingObjectArmoredCar(armoredCar), gc); - } -} diff --git a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerMapWithSetArmoredCars.java b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerMapWithSetArmoredCars.java new file mode 100644 index 0000000..4856bfd --- /dev/null +++ b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerMapWithSetArmoredCars.java @@ -0,0 +1,280 @@ +package com.example.antiaircraftgun; + +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.canvas.Canvas; +import javafx.scene.control.*; +import javafx.scene.layout.FlowPane; +import javafx.scene.layout.Pane; +import javafx.stage.Stage; + +import java.io.IOException; +import java.util.Objects; + +public class ControllerMapWithSetArmoredCars +{ + @FXML + private Pane root; + @FXML + private Canvas canvas; + @FXML + private Pane paneRight; + @FXML + private Label labelTools; + @FXML + private ComboBox comboBoxSelectorMap; + @FXML + private Button buttonAddArmoredVehicle; + @FXML + private TextField textFieldPosition; + @FXML + private Button buttonLeft; + @FXML + private Button buttonRight; + @FXML + private Button buttonUp; + @FXML + private Button buttonDown; + private MapWithSetArmoredCarsGeneric _mapArmoredCarsCollectionGeneric; + @FXML + public void initialize() + { + textFieldPosition.textProperty().addListener((ov, oldValue, newValue) -> + { + if (Objects.equals(newValue, "")) + { + return; + } + if (newValue.length() > 2) { + String s = newValue.substring(0, 2); + textFieldPosition.setText(s); + } + if (!newValue.matches("[0-9]+")) + { + textFieldPosition.setText(oldValue); + } + }); + + root.widthProperty().addListener((obs, oldVal, newVal) -> + { + UpdateGUI(); + }); + + root.heightProperty().addListener((obs, oldVal, newVal) -> + { + UpdateGUI(); + }); + } + @FXML + private void ComboBoxSelectorMap_Changed() + { + AbstractMap map = switch (comboBoxSelectorMap.getValue()) + { + case "Simple map" -> new SimpleMap(); + case "Training ground map" -> new TrainingGroundMap(); + case "Desert map" -> new DesertMap(); + default -> null; + }; + if (map != null) + { + _mapArmoredCarsCollectionGeneric = new MapWithSetArmoredCarsGeneric( + (int)canvas.getWidth(), (int)canvas.getHeight(), map, DrawingObjectArmoredCar.class, canvas.getGraphicsContext2D()); + } + else + { + _mapArmoredCarsCollectionGeneric = null; + } + } + @FXML + public void ButtonAddArmoredCar_Click() throws IOException + { + if (_mapArmoredCarsCollectionGeneric == null) + { + return; + } + + Stage stageArmoredCar = new Stage(); + FXMLLoader fxmlLoader = new FXMLLoader(FormArmoredCar.class.getResource("form-armored-vehicle-view.fxml")); + Scene sceneArmoredCar = new Scene(fxmlLoader.load(), 600, 400); + + FirstUpdateArmoredCarGUI(sceneArmoredCar, stageArmoredCar, fxmlLoader); + + stageArmoredCar.setTitle("Armored car"); + stageArmoredCar.setScene(sceneArmoredCar); + stageArmoredCar.show(); + } + + @FXML + private void ButtonRemoveArmoredCar_Click() + { + String stringPosition = textFieldPosition.getText(); + if (stringPosition == null || stringPosition.length() == 0) + { + return; + } + + Alert removeAlert = new Alert(Alert.AlertType.CONFIRMATION, "Remove object?", ButtonType.YES, ButtonType.NO); + removeAlert.showAndWait(); + + Alert infoAlert; + if (removeAlert.getResult() == ButtonType.YES) + { + int pos = Integer.parseInt(stringPosition); + if (_mapArmoredCarsCollectionGeneric.Delete(pos) != null) + { + infoAlert = new Alert(Alert.AlertType.INFORMATION, "Object removed", ButtonType.OK); + _mapArmoredCarsCollectionGeneric.ShowSet(); + } + else + { + infoAlert = new Alert(Alert.AlertType.INFORMATION, "Failed to remove object", ButtonType.OK); + } + } + else + { + infoAlert = new Alert(Alert.AlertType.INFORMATION, "Remove operation canceled", ButtonType.OK); + } + infoAlert.showAndWait(); + } + + @FXML + private void ButtonShowStorage_Click() + { + if (_mapArmoredCarsCollectionGeneric == null) + { + return; + } + _mapArmoredCarsCollectionGeneric.ShowSet(); + } + + @FXML + private void ButtonShowOnMap_Click() + { + if (_mapArmoredCarsCollectionGeneric == null) + { + return; + } + _mapArmoredCarsCollectionGeneric.ShowOnMap(); + } + + @FXML + private void ButtonMove_Click(ActionEvent event) + { + if (_mapArmoredCarsCollectionGeneric == null) + { + return; + } + String buttonName = ((Button)event.getSource()).getId(); + Direction dir = switch (buttonName) + { + case "buttonUp" -> Direction.Up; + case "buttonDown" -> Direction.Down; + case "buttonLeft" -> Direction.Left; + case "buttonRight" -> Direction.Right; + default -> Direction.None; + }; + _mapArmoredCarsCollectionGeneric.MoveObject(dir); + } + + private void UpdateGUI() + { + double rootWidth = root.getWidth(); + double rootHeight = root.getHeight(); + double rightPaneWidth = paneRight.getWidth(); + + canvas.setWidth(rootWidth - rightPaneWidth); + canvas.setHeight(rootHeight); + + paneRight.setTranslateX(rootWidth - rightPaneWidth); + paneRight.setPrefHeight(rootHeight); + + int _distanceBetweenMoveButtons = 5; + double moveButtonsSize = 30.0; + double moveButtonsXMargin = (rightPaneWidth - moveButtonsSize * 3.0 - _distanceBetweenMoveButtons * 2.0) / 2.0; + + int rightPaneMargin = 5; + buttonUp.setTranslateY(rootHeight - moveButtonsSize * 2.0 - _distanceBetweenMoveButtons - rightPaneMargin); + buttonUp.setTranslateX(rootWidth - moveButtonsSize * 2.0 - moveButtonsXMargin - _distanceBetweenMoveButtons); + + buttonDown.setTranslateY(rootHeight - moveButtonsSize - rightPaneMargin); + buttonDown.setTranslateX(rootWidth - moveButtonsSize * 2.0 - moveButtonsXMargin - _distanceBetweenMoveButtons); + + buttonLeft.setTranslateY(rootHeight - moveButtonsSize - rightPaneMargin); + buttonLeft.setTranslateX(rootWidth - moveButtonsSize * 3.0 - moveButtonsXMargin - _distanceBetweenMoveButtons * 2.0); + + buttonRight.setTranslateY(rootHeight - moveButtonsSize - rightPaneMargin); + buttonRight.setTranslateX(rootWidth - moveButtonsSize - moveButtonsXMargin); + } + + private void FirstUpdateArmoredCarGUI(Scene scene, Stage stageArmoredCar, FXMLLoader fxmlLoader) + { + Pane root = (Pane)scene.lookup("#root"); + Canvas canvas = (Canvas)scene.lookup("#canvas"); + Button buttonCreate = (Button)scene.lookup("#buttonCreate"); + Button buttonCreateModif = (Button)scene.lookup("#buttonCreateModif"); + Button buttonUp = (Button)scene.lookup("#buttonUp"); + Button buttonLeft = (Button)scene.lookup("#buttonLeft"); + Button buttonRight = (Button)scene.lookup("#buttonRight"); + Button buttonDown = (Button)scene.lookup("#buttonDown"); + Button buttonSelectArmoredCar = (Button)scene.lookup("#buttonSelectArmoredCar"); + FlowPane flowPane = (FlowPane)scene.lookup("#flowPane"); + + root.applyCss(); + root.layout(); + + double flowPaneHeight = flowPane.getHeight(); + double buttonCreateHeight = buttonCreate.getHeight(); + double buttonCreateWidth = buttonCreate.getWidth(); + double rootWidth = root.getWidth(); + double rootHeight = root.getHeight(); + double rootPadding = 10.0; + double distanceBetweenButtons = 5.0; + double buttonMoveSize = 30.0; + + canvas.setWidth(rootWidth); + flowPane.setPrefWidth(rootWidth); + canvas.setHeight(rootHeight - flowPaneHeight); + flowPane.setTranslateY(rootHeight - flowPaneHeight); + buttonCreateModif.setTranslateX(rootPadding + buttonCreateWidth + distanceBetweenButtons); + buttonCreate.setTranslateY(rootHeight - flowPaneHeight - buttonCreateHeight - rootPadding); + buttonCreateModif.setTranslateY(rootHeight - flowPaneHeight - buttonCreateHeight - rootPadding); + + buttonUp.setTranslateY(rootHeight - flowPaneHeight - buttonMoveSize * 2.0 - rootPadding - + distanceBetweenButtons); + buttonUp.setTranslateX(rootWidth - rootPadding - buttonMoveSize * 2.0 - distanceBetweenButtons); + + buttonDown.setTranslateY(rootHeight - flowPaneHeight - buttonMoveSize - rootPadding); + buttonDown.setTranslateX(rootWidth- rootPadding - buttonMoveSize * 2.0 - distanceBetweenButtons); + + buttonLeft.setTranslateY(rootHeight - flowPaneHeight - buttonMoveSize - rootPadding); + buttonLeft.setTranslateX(rootWidth - rootPadding - buttonMoveSize * 3.0 - distanceBetweenButtons * 2.0); + + buttonRight.setTranslateY(rootHeight - flowPaneHeight - buttonMoveSize - rootPadding); + buttonRight.setTranslateX(rootWidth - rootPadding - buttonMoveSize); + + buttonSelectArmoredCar.setTranslateY(rootHeight - flowPaneHeight - buttonCreateHeight - rootPadding); + buttonSelectArmoredCar.setTranslateX(rootWidth - rootPadding - buttonMoveSize * 3.0 - + distanceBetweenButtons * 3.0 - buttonSelectArmoredCar.getWidth()); + + buttonSelectArmoredCar.setOnAction(e -> + { + stageArmoredCar.close(); + ControllerArmoredCar controllerArmoredCar = fxmlLoader.getController(); + DrawingObjectArmoredCar armoredCar = new DrawingObjectArmoredCar(controllerArmoredCar.GetSelectedArmoredCar()); + + Alert alert; + if (_mapArmoredCarsCollectionGeneric.Add(armoredCar) != -1) + { + alert = new Alert(Alert.AlertType.INFORMATION, "Object added", ButtonType.OK); + _mapArmoredCarsCollectionGeneric.ShowSet(); + } + else + { + alert = new Alert(Alert.AlertType.ERROR, "Failed to add object", ButtonType.OK); + } + alert.showAndWait(); + }); + } +} diff --git a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerSetMixedArmoredCars.java b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerSetMixedArmoredCars.java new file mode 100644 index 0000000..ed85007 --- /dev/null +++ b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/ControllerSetMixedArmoredCars.java @@ -0,0 +1,181 @@ +package com.example.antiaircraftgun; + +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.layout.FlowPane; +import javafx.scene.layout.Pane; +import javafx.scene.paint.Color; + +import java.util.Random; + +public class ControllerSetMixedArmoredCars +{ + @FXML + private Pane root; + @FXML + private Canvas canvas; + @FXML + private FlowPane flowPane; + @FXML + private Label labelSpeedValue; + @FXML + private Label labelWeightValue; + @FXML + private Label labelBodyColorValue; + @FXML + private Button buttonCreate; + @FXML + private Button buttonLeft; + @FXML + private Button buttonRight; + @FXML + private Button buttonUp; + @FXML + private Button buttonDown; + private final double rootPadding = 10.0; + private DrawingArmoredCar _armoredCar; + private SetMixedArmoredCarsGeneric _setMixedArmoredCars; + + @FXML + public void initialize() + { + _setMixedArmoredCars = new SetMixedArmoredCarsGeneric<>(10, EntityArmoredCar.class, IDrawingAdditionalElement.class); + + for (int i = 0; i < 3; i++) + { + _setMixedArmoredCars.AddArmoredCar(CreateRandomArmoredCar()); + _setMixedArmoredCars.AddAdditionalElement(CreateRandomAdditionalElement()); + } + + buttonCreate.setTranslateX(rootPadding); + + root.widthProperty().addListener((obs, oldVal, newVal) -> + { + UpdateGUI(); + if (_armoredCar != null) + { + _armoredCar.ChangeBorders((int) canvas.getWidth(), (int) canvas.getHeight()); + } + Draw(); + }); + root.heightProperty().addListener((obs, oldVal, newVal) -> + { + UpdateGUI(); + if (_armoredCar != null) + { + _armoredCar.ChangeBorders((int) canvas.getWidth(), (int) canvas.getHeight()); + } + Draw(); + }); + } + + public EntityArmoredCar CreateRandomArmoredCar() + { + Random rnd = new Random(); + return new EntityArmoredCar(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, + Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + } + + public IDrawingAdditionalElement CreateRandomAdditionalElement() + { + Random rnd = new Random(); + int trackRollersType = rnd.nextInt(0, 3); + int numOfTrackRollers = rnd.nextInt(4, 7); + IDrawingAdditionalElement additionalElement; + switch (trackRollersType) + { + case 1 -> + additionalElement = new DrawingOvalOrnamentRollers(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + case 2 -> + additionalElement = new DrawingRectangleOrnamentRollers(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + default -> + additionalElement = new DrawingRollers(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + } + additionalElement.SetNumberRollers(numOfTrackRollers); + + return additionalElement; + } + + @FXML + void ButtonCreate_Click() + { + _armoredCar = _setMixedArmoredCars.GetRandomArmoredCar(); + SetData(); + Draw(); + } + + @FXML + void ButtonMove_Click(ActionEvent event) + { + if (_armoredCar == null) + { + return; + } + String buttonName = ((Button)event.getSource()).getId(); + switch (buttonName) + { + case "buttonUp" -> _armoredCar.MoveTransport(Direction.Up); + case "buttonDown" -> _armoredCar.MoveTransport(Direction.Down); + case "buttonLeft" -> _armoredCar.MoveTransport(Direction.Left); + case "buttonRight" -> _armoredCar.MoveTransport(Direction.Right); + } + Draw(); + } + + private void UpdateGUI() + { + double rootWidth = root.getWidth(); + double rootHeight = root.getHeight(); + + double flowPaneHeight = flowPane.getHeight(); + double buttonCreateHeight = buttonCreate.getHeight(); + + canvas.setWidth(rootWidth); + flowPane.setPrefWidth(rootWidth); + canvas.setHeight(rootHeight - flowPaneHeight); + flowPane.setTranslateY(rootHeight - flowPaneHeight); + + buttonCreate.setTranslateY(rootHeight - flowPaneHeight - buttonCreateHeight - rootPadding); + + int buttonMoveSize = 30; + int distanceBetweenButtons = 5; + buttonUp.setTranslateY(rootHeight - flowPaneHeight - buttonMoveSize * 2.0 - rootPadding - + distanceBetweenButtons); + buttonUp.setTranslateX(rootWidth - rootPadding - buttonMoveSize * 2.0 - distanceBetweenButtons); + + buttonDown.setTranslateY(rootHeight - flowPaneHeight - buttonMoveSize - rootPadding); + buttonDown.setTranslateX(rootWidth- rootPadding - buttonMoveSize * 2.0 - distanceBetweenButtons); + + buttonLeft.setTranslateY(rootHeight - flowPaneHeight - buttonMoveSize - rootPadding); + buttonLeft.setTranslateX(rootWidth - rootPadding - buttonMoveSize * 3.0 - + distanceBetweenButtons * 2.0); + + buttonRight.setTranslateY(rootHeight - flowPaneHeight - buttonMoveSize - rootPadding); + buttonRight.setTranslateX(rootWidth - rootPadding - buttonMoveSize); + } + + private void Draw() + { + GraphicsContext gc = canvas.getGraphicsContext2D(); + gc.clearRect(0.0, 0.0, canvas.getWidth(), canvas.getHeight()); + + if (_armoredCar != null) + { + _armoredCar.DrawTransport(gc); + } + } + + private void SetData() + { + Random rnd = new Random(); + _armoredCar.SetPosition(rnd.nextInt(90) + 10, rnd.nextInt(90), + (int) canvas.getWidth(), (int) canvas.getHeight()); + labelSpeedValue.setText(Integer.toString(_armoredCar.GetArmoredCar().GetSpeed())); + labelWeightValue.setText(Double.toString(_armoredCar.GetArmoredCar().GetWeight())); + labelBodyColorValue.setText(_armoredCar.GetArmoredCar().GetBodyColor().toString()); + } +} \ No newline at end of file diff --git a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/DrawingArmoredCar.java b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/DrawingArmoredCar.java index c4340b2..8d9252c 100644 --- a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/DrawingArmoredCar.java +++ b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/DrawingArmoredCar.java @@ -41,6 +41,11 @@ public class DrawingArmoredCar _armoredCarHeight = armoredCarHeight; _armoredCarWidth = armoredCarWidth; } + public DrawingArmoredCar(EntityArmoredCar armoredCar, IDrawingAdditionalElement additionalElement) + { + _armoredCar = armoredCar; + _drawingRollers = additionalElement; + } public void SetPosition(int x, int y, int width, int height) { if (x < 0 || y < 0) { diff --git a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormArmoredCar.java b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormArmoredCar.java index 6839aad..121ddaa 100644 --- a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormArmoredCar.java +++ b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormArmoredCar.java @@ -37,6 +37,7 @@ public class FormArmoredCar extends Application Button buttonLeft = (Button)scene.lookup("#buttonLeft"); Button buttonRight = (Button)scene.lookup("#buttonRight"); Button buttonDown = (Button)scene.lookup("#buttonDown"); + Button buttonSelectArmoredCar = (Button)scene.lookup("#buttonSelectArmoredCar"); FlowPane flowPane = (FlowPane)scene.lookup("#flowPane"); root.applyCss(); @@ -71,6 +72,10 @@ public class FormArmoredCar extends Application buttonRight.setTranslateY(rootHeight - flowPaneHeight - buttonMoveSize - rootPadding); buttonRight.setTranslateX(rootWidth - rootPadding - buttonMoveSize); + + buttonSelectArmoredCar.setTranslateY(rootHeight - flowPaneHeight - buttonCreateHeight - rootPadding); + buttonSelectArmoredCar.setTranslateX(rootWidth - rootPadding - buttonMoveSize * 3.0 - + distanceBetweenButtons * 3.0 - buttonSelectArmoredCar.getWidth()); } public static void main(String[] args) { diff --git a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormMapWithSetArmoredCars.java b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormMapWithSetArmoredCars.java new file mode 100644 index 0000000..7e4133b --- /dev/null +++ b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormMapWithSetArmoredCars.java @@ -0,0 +1,113 @@ +package com.example.antiaircraftgun; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.canvas.Canvas; +import javafx.scene.control.*; +import javafx.scene.layout.Pane; +import javafx.stage.Stage; + +import java.io.IOException; + +public class FormMapWithSetArmoredCars extends Application +{ + @Override + public void start(Stage stage) throws IOException + { + FXMLLoader fxmlLoader = new FXMLLoader(FormArmoredCar.class.getResource("form-map-with-set-armored-vehicles-view.fxml")); + Scene scene = new Scene(fxmlLoader.load(), 600, 400); + + FirstUpdateGUI(scene); + + stage.setTitle("Map with a set of objects"); + stage.setScene(scene); + + stage.show(); + } + + private void FirstUpdateGUI(Scene scene) + { + Pane root = (Pane)scene.lookup("#root"); + Canvas canvas = (Canvas)scene.lookup("#canvas"); + Pane paneRight = (Pane)scene.lookup("#paneRight"); + Label labelTools = (Label)scene.lookup("#labelTools"); + @SuppressWarnings("unchecked") + ComboBox comboBoxSelectorMap = (ComboBox)scene.lookup("#comboBoxSelectorMap"); + Button buttonAddArmoredCar = (Button)scene.lookup("#buttonAddArmoredCar"); + TextField textFieldPosition = (TextField)scene.lookup("#textFieldPosition"); + Button buttonRemoveArmoredCar = (Button)scene.lookup("#buttonRemoveArmoredCar"); + Button buttonShowStorage = (Button)scene.lookup("#buttonShowStorage"); + Button buttonShowOnMap = (Button)scene.lookup("#buttonShowOnMap"); + Button buttonUp = (Button)scene.lookup("#buttonUp"); + Button buttonLeft = (Button)scene.lookup("#buttonLeft"); + Button buttonRight = (Button)scene.lookup("#buttonRight"); + Button buttonDown = (Button)scene.lookup("#buttonDown"); + + root.applyCss(); + root.layout(); + + double paneRightPadding = 5.0; + double rootWidth = root.getWidth(); + double rootHeight = root.getHeight(); + double standardWidth = buttonRemoveArmoredCar.getWidth(); + double standardHeight = buttonRemoveArmoredCar.getHeight(); + double paneRightWidth = standardWidth + paneRightPadding * 2; + + canvas.setWidth(rootWidth - paneRightWidth); + canvas.setHeight(rootHeight); + paneRight.setPrefWidth(paneRightWidth); + paneRight.setPrefHeight(rootHeight); + paneRight.setTranslateX(rootWidth - paneRightWidth); + + double topMargin = labelTools.getHeight(); + + comboBoxSelectorMap.setTranslateX(paneRightPadding); + comboBoxSelectorMap.setTranslateY(topMargin); + topMargin += standardHeight + paneRightPadding * 3.0; + + buttonAddArmoredCar.setPrefWidth(standardWidth); + buttonAddArmoredCar.setTranslateX(paneRightPadding); + buttonAddArmoredCar.setTranslateY(topMargin); + topMargin += standardHeight + paneRightPadding; + + textFieldPosition.setPrefWidth(standardWidth); + textFieldPosition.setTranslateX(paneRightPadding); + textFieldPosition.setTranslateY(topMargin); + topMargin += standardHeight + paneRightPadding; + + buttonRemoveArmoredCar.setTranslateX(paneRightPadding); + buttonRemoveArmoredCar.setTranslateY(topMargin); + topMargin += standardHeight + paneRightPadding * 3.0; + + buttonShowStorage.setPrefWidth(standardWidth); + buttonShowStorage.setTranslateX(paneRightPadding); + buttonShowStorage.setTranslateY(topMargin); + topMargin += standardHeight + paneRightPadding; + + buttonShowOnMap.setPrefWidth(standardWidth); + buttonShowOnMap.setTranslateX(paneRightPadding); + buttonShowOnMap.setTranslateY(topMargin); + + int _distanceBetweenMoveButtons = 5; + double moveButtonsSize = 30.0; + double moveButtonsXMargin = (paneRightWidth - moveButtonsSize * 3.0 - _distanceBetweenMoveButtons * 2.0) / 2.0; + + buttonUp.setTranslateY(rootHeight - moveButtonsSize * 2.0 - _distanceBetweenMoveButtons - paneRightPadding); + buttonUp.setTranslateX(rootWidth - moveButtonsSize * 2.0 - moveButtonsXMargin - _distanceBetweenMoveButtons); + + buttonDown.setTranslateY(rootHeight - moveButtonsSize - paneRightPadding); + buttonDown.setTranslateX(rootWidth - moveButtonsSize * 2.0 - moveButtonsXMargin - _distanceBetweenMoveButtons); + + buttonLeft.setTranslateY(rootHeight - moveButtonsSize - paneRightPadding); + buttonLeft.setTranslateX(rootWidth - moveButtonsSize * 3.0 - moveButtonsXMargin - _distanceBetweenMoveButtons * 2.0); + + buttonRight.setTranslateY(rootHeight - moveButtonsSize - paneRightPadding); + buttonRight.setTranslateX(rootWidth - moveButtonsSize - moveButtonsXMargin); + } + + public static void main(String[] args) + { + launch(); + } +} diff --git a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormMap.java b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormSetMixedArmoredCars.java similarity index 82% rename from AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormMap.java rename to AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormSetMixedArmoredCars.java index 76e81a1..9f32d68 100644 --- a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormMap.java +++ b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/FormSetMixedArmoredCars.java @@ -11,14 +11,14 @@ import javafx.stage.Stage; import java.io.IOException; -public class FormMap extends Application{ +public class FormSetMixedArmoredCars extends Application{ @Override public void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(FormMap.class.getResource("form-map-view.fxml")); + FXMLLoader fxmlLoader = new FXMLLoader(FormSetMixedArmoredCars.class.getResource("form-set-mixed-armored-vehicles-view.fxml")); Scene scene = new Scene(fxmlLoader.load(), 600, 400); - stage.setTitle("Map"); + stage.setTitle("Set mixed armored cars"); stage.setScene(scene); FirstUpdateGUI(scene); @@ -30,7 +30,6 @@ public class FormMap extends Application{ Pane root = (Pane)scene.lookup("#root"); Canvas canvas = (Canvas)scene.lookup("#canvas"); Button buttonCreate = (Button)scene.lookup("#buttonCreate"); - Button buttonCreateModif = (Button)scene.lookup("#buttonCreateModif"); Button buttonUp = (Button)scene.lookup("#buttonUp"); Button buttonLeft = (Button)scene.lookup("#buttonLeft"); Button buttonRight = (Button)scene.lookup("#buttonRight"); @@ -42,7 +41,6 @@ public class FormMap extends Application{ double flowPaneHeight = flowPane.getHeight(); double buttonCreateHeight = buttonCreate.getHeight(); - double buttonCreateWidth = buttonCreate.getWidth(); double rootWidth = root.getWidth(); double rootHeight = root.getHeight(); double rootPadding = 10.0; @@ -53,9 +51,8 @@ public class FormMap extends Application{ flowPane.setPrefWidth(rootWidth); canvas.setHeight(rootHeight - flowPaneHeight); flowPane.setTranslateY(rootHeight - flowPaneHeight); - buttonCreateModif.setTranslateX(rootPadding + buttonCreateWidth + distanceBetweenButtons); - buttonCreate.setTranslateY(rootHeight - flowPaneHeight - buttonCreateHeight - rootPadding); - buttonCreateModif.setTranslateY(rootHeight - flowPaneHeight - buttonCreateHeight - rootPadding); + + buttonCreate.setTranslateY(rootHeight - flowPaneHeight - rootPadding - buttonCreateHeight); buttonUp.setTranslateY(rootHeight - flowPaneHeight - buttonMoveSize * 2.0 - rootPadding - distanceBetweenButtons); diff --git a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/MapWithSetArmoredCarsGeneric.java b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/MapWithSetArmoredCarsGeneric.java new file mode 100644 index 0000000..7a3fa5b --- /dev/null +++ b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/MapWithSetArmoredCarsGeneric.java @@ -0,0 +1,126 @@ +package com.example.antiaircraftgun; + +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class MapWithSetArmoredCarsGeneric +{ + private final int _pictureWidth; + private final int _pictureHeight; + private final int _placeSizeWidth = 150; + private final int _placeSizeHeight = 90; + private final SetArmoredCarsGeneric _setArmoredCars; + private GraphicsContext _graphicsContext = null; + private final U _map; + + public MapWithSetArmoredCarsGeneric(int picWidth, int picHeight, U map, Class objectT, GraphicsContext gc) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setArmoredCars = new SetArmoredCarsGeneric(width * height, objectT); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + _graphicsContext = gc; + } + + public int Add(T armoredCar) + { + return _setArmoredCars.Insert(armoredCar); + } + + public T Delete(int position) + { + return _setArmoredCars.Remove(position); + } + + public void ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setArmoredCars.GetCount(); i++) + { + var armoredCar = _setArmoredCars.Get(i); + if (armoredCar != null) + { + _map.CreateMap(_pictureWidth, _pictureHeight, armoredCar, _graphicsContext); + return; + } + } + } + + public void ShowSet() + { + DrawBackground(); + DrawArmoredCar(); + } + + public void MoveObject(Direction direction) + { + if (_map != null) + { + _map.MoveObject(direction); + } + } + + private void Shaking() + { + int j = _setArmoredCars.GetCount() - 1; + for (int i = 0; i < _setArmoredCars.GetCount(); i++) + { + if (_setArmoredCars.Get(i) == null) + { + for (; j > i; j--) + { + var car = _setArmoredCars.Get(j); + if (car != null) + { + _setArmoredCars.Insert(car, i); + _setArmoredCars.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground() + { + _graphicsContext.setFill(Color.GREEN); + _graphicsContext.fillRect(0, 0, _pictureWidth, _pictureHeight); + + _graphicsContext.setFill(Color.LIGHTGRAY); + + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++) + { + _graphicsContext.fillRect(i * _placeSizeWidth + _placeSizeWidth / 10, j * _placeSizeHeight + _placeSizeHeight / 10, _placeSizeWidth * 8 / 10, _placeSizeHeight * 8 / 10); + } + } + } + + private void DrawArmoredCar() + { + int xPositionInStorage = _placeSizeWidth / 10; + int yPositionInStorage = _placeSizeHeight / 10; + + for (int i = 0; i < _setArmoredCars.GetCount(); i++) + { + if (_setArmoredCars.Get(i) != null) + { + _setArmoredCars.Get(i).SetObject(xPositionInStorage, yPositionInStorage, _pictureWidth, _pictureHeight); + _setArmoredCars.Get(i).DrawingObject(_graphicsContext); + xPositionInStorage += _placeSizeWidth; + } + if (xPositionInStorage > _pictureWidth - _placeSizeWidth) + { + xPositionInStorage = _placeSizeWidth / 10; + yPositionInStorage += _placeSizeHeight; + } + } + } +} diff --git a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/SetArmoredCarsGeneric.java b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/SetArmoredCarsGeneric.java new file mode 100644 index 0000000..cefc4d8 --- /dev/null +++ b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/SetArmoredCarsGeneric.java @@ -0,0 +1,87 @@ +package com.example.antiaircraftgun; + +import java.lang.reflect.Array; + +public class SetArmoredCarsGeneric +{ + private final T[] _places; + public int GetCount() + { + return _places.length; + } + + public SetArmoredCarsGeneric(int count, Class objectT) + { + @SuppressWarnings("unchecked") + final T[] places = (T[])Array.newInstance(objectT, count); + _places = places; + } + + public int Insert(T armoredCar) + { + return Insert(armoredCar, 0); + } + + public int Insert(T armoredCar, int position) + { + if (position < 0 || position > _places.length) + { + return -1; + } + int emptyCellIndex = -1; + if (_places[position] != null) + { + for (int i = position + 1; i < GetCount(); i++) + { + if (_places[i] == null) + { + emptyCellIndex = i; + break; + } + } + if (emptyCellIndex != -1) + { + for (int i = emptyCellIndex; i > position; i--) + { + _places[i] = _places[i - 1]; + } + } + } + else + { + _places[position] = armoredCar; + return position; + } + + if (emptyCellIndex == -1) + { + return -1; + } + else + { + _places[position] = armoredCar; + return position; + } + } + + public T Remove(int position) + { + if (position >= GetCount() || position < 0) + { + return null; + } + + T removedObject = _places[position]; + _places[position] = null; + return removedObject; + } + + public T Get(int position) + { + if (position >= GetCount() || position < 0) + { + return null; + } + return _places[position]; + } +} diff --git a/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/SetMixedArmoredCarsGeneric.java b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/SetMixedArmoredCarsGeneric.java new file mode 100644 index 0000000..1501bb8 --- /dev/null +++ b/AntiAircraftGun/src/main/java/com/example/antiaircraftgun/SetMixedArmoredCarsGeneric.java @@ -0,0 +1,68 @@ +package com.example.antiaircraftgun; + +import java.lang.reflect.Array; +import java.util.Random; + +public class SetMixedArmoredCarsGeneric +{ + private final T[] _armoredCars; + private final U[] _additionalElements; + private final int _maxCount; + private int _armoredCarsCount; + private int _additionalElementsCount; + + public SetMixedArmoredCarsGeneric(int maxCount, Class objectT, Class objectU) + { + @SuppressWarnings("unchecked") + final T[] armoredCars = (T[]) Array.newInstance(objectT, maxCount); + _armoredCars = armoredCars; + @SuppressWarnings("unchecked") + final U[] additionalElements = (U[]) Array.newInstance(objectU, maxCount); + _additionalElements = additionalElements; + _maxCount = maxCount; + _armoredCarsCount = 0; + _additionalElementsCount = 0; + } + + public boolean AddArmoredCar(T armoredCar) + { + if (_armoredCarsCount == _maxCount) + { + return false; + } + else + { + _armoredCars[_armoredCarsCount] = armoredCar; + _armoredCarsCount++; + return true; + } + } + + public boolean AddAdditionalElement(U additionalElement) + { + if (_additionalElementsCount == _maxCount) + { + return false; + } + else + { + _additionalElements[_additionalElementsCount] = additionalElement; + _additionalElementsCount++; + return true; + } + } + + public DrawingArmoredCar GetRandomArmoredCar() + { + if (_armoredCarsCount == 0 || _additionalElementsCount == 0) + { + return null; + } + + Random random = new Random(); + T randomArmoredCar = _armoredCars[random.nextInt(0, _armoredCarsCount)]; + U randomAdditionalElement = _additionalElements[random.nextInt(0, _additionalElementsCount)]; + + return new DrawingArmoredCar(randomArmoredCar, randomAdditionalElement); + } +} diff --git a/AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-armored-vehicle-view.fxml b/AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-armored-vehicle-view.fxml index 505116d..a5e245c 100644 --- a/AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-armored-vehicle-view.fxml +++ b/AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-armored-vehicle-view.fxml @@ -11,11 +11,11 @@ - + + - + @@ -31,9 +31,11 @@ - diff --git a/AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-map-with-set-armored-vehicles-view.fxml b/AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-map-with-set-armored-vehicles-view.fxml new file mode 100644 index 0000000..6212d4d --- /dev/null +++ b/AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-map-with-set-armored-vehicles-view.fxml @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-map-view.fxml b/AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-set-mixed-armored-vehicles-view.fxml similarity index 62% rename from AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-map-view.fxml rename to AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-set-mixed-armored-vehicles-view.fxml index f267870..818fb79 100644 --- a/AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-map-view.fxml +++ b/AntiAircraftGun/src/main/resources/com/example/antiaircraftgun/form-set-mixed-armored-vehicles-view.fxml @@ -5,13 +5,10 @@ - - - - + @@ -33,46 +30,10 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -