From 3235141828592a208961f42365c86bca0eb619b1 Mon Sep 17 00:00:00 2001 From: Zyzf Date: Mon, 5 Dec 2022 11:23:19 +0400 Subject: [PATCH] TODO xmls --- .../ControllerBoat.java | 61 ++- .../ControllerMapWithSetBoats.java | 360 ++++++++++++++++++ .../ControllerRemovedBoat.java | 146 +++++++ .../ControllerSetMixedBoats.java | 155 ++++++++ .../Direction.java | 1 + .../DrawningBoat.java | 4 + .../DrawningObjectBoat.java | 4 + .../FormMapWithSetBoats.java | 131 +++++++ .../{FormMap.java => FormSetMixedBoats.java} | 19 +- .../MapWithSetBoatsGeneric.java | 126 ++++++ .../MapsCollection.java | 47 +++ .../SetBoatsGeneric.java | 51 +++ .../SetMixedBoatsGeneric.java | 54 +++ .../form-boat-view.fxml | 27 +- 14 files changed, 1147 insertions(+), 39 deletions(-) create mode 100644 src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerMapWithSetBoats.java create mode 100644 src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerRemovedBoat.java create mode 100644 src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerSetMixedBoats.java create mode 100644 src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/FormMapWithSetBoats.java rename src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/{FormMap.java => FormSetMixedBoats.java} (79%) create mode 100644 src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/MapWithSetBoatsGeneric.java create mode 100644 src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/MapsCollection.java create mode 100644 src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/SetBoatsGeneric.java create mode 100644 src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/SetMixedBoatsGeneric.java diff --git a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerBoat.java b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerBoat.java index 27659c7..a4575b3 100644 --- a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerBoat.java +++ b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerBoat.java @@ -4,6 +4,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; @@ -41,16 +42,27 @@ public class ControllerBoat { private Button buttonUp; @FXML private Button buttonDown; + @FXML + private Button buttonSelectBoat; + @FXML + private ColorPicker bodyColorPicker; + @FXML + private ColorPicker dopColorPicker; private final double rootPadding = 10.0; private DrawningBoat _boat; + private boolean bodyColorPickerChanged = false; + private boolean dopColorPickerChanged = false; + public DrawningBoat GetSelectedBoat() + { + return _boat; + } @FXML public void initialize() { buttonCreate.setTranslateX(rootPadding); root.widthProperty().addListener((obs, oldVal, newVal) -> { UpdateGUI(); - if (_boat != null) - { + if (_boat != null) { _boat.ChangeBorders((int) canvas.getWidth(), (int) canvas.getHeight()); } Draw(); @@ -66,6 +78,9 @@ public class ControllerBoat { @FXML void ButtonCreate_Click() { Random rnd = new Random(); + if (!bodyColorPickerChanged) { + bodyColorPicker.setValue(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + } _boat = new DrawningBoat(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); SetData(); @@ -74,6 +89,12 @@ public class ControllerBoat { @FXML 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))); + } _boat = new DrawningSpeedboat(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)), @@ -96,27 +117,37 @@ public class ControllerBoat { Draw(); } @FXML - private void ComboBoxOrnamentType_Changed() { + private void ComboBoxType_Changed() { if (_boat != null) { ChangeDrawningOars(); Draw(); } } @FXML - private void ComboBoxNumOfRollers_Changed() { + private void ComboBoxNumOars_Changed() { if (_boat != null) { _boat.GetDrawningOars().SetNumberOars(Integer.parseInt(comboBoxNumOars.getValue())); Draw(); } } + @FXML + private void BodyColorPicker_Changed() + { + bodyColorPickerChanged = true; + } + @FXML + private void DopColorPicker_Changed() + { + dopColorPickerChanged = true; + } private void ChangeDrawningOars() { if (_boat != null) { IDrawningAdditionalElement newDrawningOars = switch (comboBoxOarsType.getValue()) { - case "None" -> new DrawningOars(_boat.GetBoat().GetBodyColor()); - case "Slim" -> new DrawningSlimOars(_boat.GetBoat().GetBodyColor()); - case "Large" -> new DrawningLargeOars(_boat.GetBoat().GetBodyColor()); - default -> null; - }; + case "None" -> new DrawningOars(_boat.GetBoat().GetBodyColor()); + case "Slim" -> new DrawningSlimOars(_boat.GetBoat().GetBodyColor()); + case "Large" -> new DrawningLargeOars(_boat.GetBoat().GetBodyColor()); + default -> null; + }; if (newDrawningOars != null) { _boat.SetDrawningOars(newDrawningOars); _boat.GetDrawningOars().SetNumberOars(Integer.parseInt(comboBoxNumOars.getValue())); @@ -153,17 +184,14 @@ public class ControllerBoat { buttonRight.setTranslateY(rootHeight - flowPaneHeight - buttonMoveSize - rootPadding); buttonRight.setTranslateX(rootWidth - rootPadding - buttonMoveSize); + + buttonSelectBoat.setTranslateY(rootHeight - flowPaneHeight - buttonCreateHeight - rootPadding); + buttonSelectBoat.setTranslateX(rootWidth - rootPadding - buttonMoveSize * 3.0 - + distanceBetweenButtons * 3.0 - buttonSelectBoat.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 (_boat != null) { _boat.DrawTransport(gc); @@ -176,6 +204,5 @@ public class ControllerBoat { (int) canvas.getWidth(), (int) canvas.getHeight()); labelSpeedValue.setText(Integer.toString(_boat.GetBoat().GetSpeed())); labelWeightValue.setText(Double.toString(_boat.GetBoat().GetWeight())); - labelBodyColorValue.setText(_boat.GetBoat().GetBodyColor().toString()); } } diff --git a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerMapWithSetBoats.java b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerMapWithSetBoats.java new file mode 100644 index 0000000..845609f --- /dev/null +++ b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerMapWithSetBoats.java @@ -0,0 +1,360 @@ +package com.example.pibd22_kalyshev_y_v_motorboat_hard; + +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.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Stack; + +public class ControllerMapWithSetBoats { + @FXML + private Pane root; + @FXML + private Canvas canvas; + @FXML + private Pane paneRight; + @FXML + private ComboBox comboBoxSelectorMap; + @FXML + private TextField textFieldPosition; + @FXML + private TextField textFieldNewMapName; + @FXML + private Button buttonLeft; + @FXML + private Button buttonRight; + @FXML + private Button buttonUp; + @FXML + private Button buttonDown; + @FXML + private ListView listViewMaps; + private final HashMap _mapsDict = new HashMap(); + private MapsCollection _mapsCollection; + private Stack _removedBoats; + public void SetMapsCollection(MapsCollection mapsCollection) + { + _mapsCollection = mapsCollection; + } + @FXML + public void initialize() { + _mapsDict.put("Simple map", new SimpleMap()); + _mapsDict.put("My second map", new MySecondMap()); + + _mapsCollection = new MapsCollection((int) canvas.getWidth(), (int) canvas.getHeight(), canvas.getGraphicsContext2D()); + comboBoxSelectorMap.getItems().clear(); + for (Map.Entry map : _mapsDict.entrySet()) { + comboBoxSelectorMap.getItems().add(map.getKey()); + } + + _removedBoats = new Stack<>(); + + 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(); + }); + + listViewMaps.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { + String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem(); + if (selectedMapName != null) { + _mapsCollection.Get(selectedMapName).ShowSet(); + } + }); + } + @FXML + public void ButtonAddBoat_Click() throws IOException { + if (listViewMaps.getSelectionModel().getSelectedItem() == null) { + return; + } + + Stage stageBoat = new Stage(); + FXMLLoader fxmlLoader = new FXMLLoader(FormBoat.class.getResource("form-boat-view.fxml")); + Scene sceneBoat = new Scene(fxmlLoader.load(), 320, 240); + + FirstUpdateBoatGUI(sceneBoat, stageBoat, fxmlLoader, false); + + stageBoat.setTitle("Boat"); + stageBoat.setScene(sceneBoat); + stageBoat.show(); + } + @FXML + private void ButtonRemoveBoat_Click() { + if (listViewMaps.getSelectionModel().getSelectedIndex() == -1) { + return; + } + + 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) { + String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem(); + int pos = Integer.parseInt(stringPosition); + DrawningObjectBoat removedBoat = _mapsCollection.Get(selectedMapName, pos); + if (selectedMapName != null && removedBoat != null) { + _mapsCollection.Get(selectedMapName).Delete(pos); + infoAlert = new Alert(Alert.AlertType.INFORMATION, "Object removed", ButtonType.OK); + _removedBoats.add(removedBoat.GetBoat()); + _mapsCollection.Get(selectedMapName).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 (listViewMaps.getSelectionModel().getSelectedIndex() == -1) { + return; + } + + String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem(); + if (selectedMapName != null) { + _mapsCollection.Get(selectedMapName).ShowSet(); + } + } + @FXML + private void ButtonShowOnMap_Click() { + if (listViewMaps.getSelectionModel().getSelectedIndex() == -1) { + return; + } + + String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem(); + if (selectedMapName != null) { + _mapsCollection.Get(selectedMapName).ShowOnMap(); + } + } + @FXML + private void ButtonMove_Click(ActionEvent event) { + if (listViewMaps.getSelectionModel().getSelectedIndex() == -1) { + 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; + }; + + String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem(); + if (selectedMapName != null) { + _mapsCollection.Get(selectedMapName).MoveObject(dir); + } + } + @FXML + private void ButtonAddMap_Click() + { + Alert addMapAlert; + if (comboBoxSelectorMap.getSelectionModel().getSelectedIndex() == -1 || textFieldNewMapName.getText() == null + || textFieldNewMapName.getText().length() == 0) { + addMapAlert = new Alert(Alert.AlertType.ERROR, "Not all information has been entered.", ButtonType.OK); + addMapAlert.showAndWait(); + return; + } + if (!_mapsDict.containsKey(comboBoxSelectorMap.getValue())) { + addMapAlert = new Alert(Alert.AlertType.ERROR, "No such map exists.", ButtonType.OK); + addMapAlert.showAndWait(); + return; + } + _mapsCollection.AddMap(textFieldNewMapName.getText(), _mapsDict.get(comboBoxSelectorMap.getValue())); + ReloadMaps(); + } + @FXML + private void ButtonDeleteMap_Click() { + if (comboBoxSelectorMap.getSelectionModel().getSelectedIndex() == -1) { + return; + } + + Alert deleteMapAlert = new Alert(Alert.AlertType.CONFIRMATION, String.format("Delete map %s?", + listViewMaps.getSelectionModel().getSelectedItem()), ButtonType.YES, ButtonType.NO); + deleteMapAlert.showAndWait(); + + String removableMapName = listViewMaps.getSelectionModel().getSelectedItem(); + if (deleteMapAlert.getResult() == ButtonType.YES && removableMapName != null) { + _mapsCollection.DelMap(removableMapName); + ReloadMaps(); + } + } + @FXML + private void ButtonShowRemovedArmoredVehicle_Click() throws IOException { + if (_removedBoats.empty()) { + Alert addMapAlert = new Alert(Alert.AlertType.ERROR, "Collection of removed objects is empty.", ButtonType.OK); + addMapAlert.showAndWait(); + } + else { + Stage stageArmoredVehicle = new Stage(); + FXMLLoader fxmlLoader = new FXMLLoader(FormBoat.class.getResource("form-removed-armored-boat.fxml")); + Scene sceneArmoredVehicle = new Scene(fxmlLoader.load(), 320, 240); + + FirstUpdateBoatGUI(sceneArmoredVehicle, stageArmoredVehicle, fxmlLoader, true); + + stageArmoredVehicle.setTitle("Removed armored vehicle"); + stageArmoredVehicle.setScene(sceneArmoredVehicle); + stageArmoredVehicle.show(); + + ControllerRemovedBoat controllerRemovedBoat = fxmlLoader.getController(); + controllerRemovedBoat.SetBoat(_removedBoats.pop()); + } + } + + 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 FirstUpdateBoatGUI(Scene scene, Stage stageBoat, FXMLLoader fxmlLoader, boolean formRemovedBoat) { + Pane root = (Pane) scene.lookup("#root"); + Canvas canvas = (Canvas) scene.lookup("#canvas"); + Button buttonCreate = (Button) scene.lookup("#buttonCreate"); + Button buttonUp = (Button) scene.lookup("#buttonUp"); + Button buttonLeft = (Button) scene.lookup("#buttonLeft"); + Button buttonRight = (Button) scene.lookup("#buttonRight"); + Button buttonDown = (Button) scene.lookup("#buttonDown"); + 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); + buttonCreate.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); + + + if (!formRemovedBoat) { + Button buttonCreateModif = (Button) scene.lookup("#buttonCreateModif"); + Button buttonSelectBoat = (Button) scene.lookup("#buttonSelectArmoredVehicle"); + buttonCreateModif.setTranslateX(rootPadding + buttonCreateWidth + distanceBetweenButtons); + buttonCreateModif.setTranslateY(rootHeight - flowPaneHeight - buttonCreateHeight - rootPadding); + + buttonSelectBoat.setTranslateY(rootHeight - flowPaneHeight - buttonCreateHeight - rootPadding); + buttonSelectBoat.setTranslateX(rootWidth - rootPadding - buttonMoveSize * 3.0 - + distanceBetweenButtons * 3.0 - buttonSelectBoat.getWidth()); + + buttonSelectBoat.setOnAction(e -> { + stageBoat.close(); + ControllerBoat controllerBoat = fxmlLoader.getController(); + DrawningObjectBoat boat = new DrawningObjectBoat(controllerBoat.GetSelectedBoat()); + + String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem(); + + Alert alert; + if (selectedMapName != null && selectedMapName.length() != 0 && _mapsCollection.Get(selectedMapName).Add(boat) != -1) { + alert = new Alert(Alert.AlertType.INFORMATION, "Object added", ButtonType.OK); + _mapsCollection.Get(selectedMapName).ShowSet(); + } + else { + alert = new Alert(Alert.AlertType.ERROR, "Failed to add object", ButtonType.OK); + } + alert.showAndWait(); + }); + } + } + + private void ReloadMaps() + { + int index = listViewMaps.getSelectionModel().getSelectedIndex(); + + listViewMaps.getItems().clear(); + for (String key : _mapsCollection.GetKeys()) + { + listViewMaps.getItems().add(key); + } + + if (listViewMaps.getItems().size() > 0 && (index == -1 || index >= listViewMaps.getItems().size())) + { + listViewMaps.getSelectionModel().select(0); + } + else if (listViewMaps.getItems().size() > 0 && index > -1 && index < listViewMaps.getItems().size()) + { + listViewMaps.getSelectionModel().select(index); + } + } +} diff --git a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerRemovedBoat.java b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerRemovedBoat.java new file mode 100644 index 0000000..eff5947 --- /dev/null +++ b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerRemovedBoat.java @@ -0,0 +1,146 @@ +package com.example.pibd22_kalyshev_y_v_motorboat_hard; + +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 ControllerRemovedBoat { + @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 comboBoxNumOars; + @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 DrawningBoat _boat; + public void SetBoat(DrawningBoat boat) { + _boat = boat; + SetData(); + Draw(); + } + @FXML + public void initialize() { + buttonCreate.setTranslateX(rootPadding); + + root.widthProperty().addListener((obs, oldVal, newVal) -> { + UpdateGUI(); + if (_boat != null) { + _boat.ChangeBorders((int) canvas.getWidth(), (int) canvas.getHeight()); + } + Draw(); + }); + root.heightProperty().addListener((obs, oldVal, newVal) -> { + UpdateGUI(); + if (_boat != null) { + _boat.ChangeBorders((int) canvas.getWidth(), (int) canvas.getHeight()); + } + Draw(); + }); + } + @FXML + private void ButtonCreate_Click() { + Random rnd = new Random(); + _boat = new DrawningBoat(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, + Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + + _boat.GetDrawningOars().SetNumberOars(Integer.parseInt(comboBoxNumOars.getValue())); + + SetData(); + Draw(); + } + @FXML + private void ComboBoxNumOars_Changed() { + if (_boat != null) { + _boat.GetDrawningOars().SetNumberOars(Integer.parseInt(comboBoxNumOars.getValue())); + Draw(); + } + } + @FXML + private void ButtonMove_Click(ActionEvent event) { + if (_boat == null) { + return; + } + String buttonName = ((Button)event.getSource()).getId(); + switch (buttonName) { + case "buttonUp" -> _boat.MoveTransport(Direction.Up); + case "buttonDown" -> _boat.MoveTransport(Direction.Down); + case "buttonLeft" -> _boat.MoveTransport(Direction.Left); + case "buttonRight" -> _boat.MoveTransport(Direction.Right); + } + Draw(); + } + private void SetData() { + Random rnd = new Random(); + _boat.SetPosition(rnd.nextInt(90) + 10, rnd.nextInt(90), + (int) canvas.getWidth(), (int) canvas.getHeight()); + + labelSpeedValue.setText(Integer.toString(_boat.GetBoat().GetSpeed())); + labelWeightValue.setText(Double.toString(_boat.GetBoat().GetWeight())); + labelBodyColorValue.setText(_boat.GetBoat().GetBodyColor().toString()); + } + 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 (_boat != null) { + _boat.DrawTransport(gc); + } + } +} diff --git a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerSetMixedBoats.java b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerSetMixedBoats.java new file mode 100644 index 0000000..49d850e --- /dev/null +++ b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/ControllerSetMixedBoats.java @@ -0,0 +1,155 @@ +package com.example.pibd22_kalyshev_y_v_motorboat_hard; + +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 ControllerSetMixedBoats { + @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 DrawningBoat _boat; + private SetMixedBoatsGeneric _setMixedBoats; + @FXML + public void initialize() { + _setMixedBoats = new SetMixedBoatsGeneric<>(40, EntityBoat.class, IDrawningAdditionalElement.class); + + for (int i = 0; i < 3; i++) { + _setMixedBoats.AddBoat(CreateRandomBoat()); + _setMixedBoats.AddAdditionalElement(CreateRandomAdditionalElement()); + } + + buttonCreate.setTranslateX(rootPadding); + + root.widthProperty().addListener((obs, oldVal, newVal) -> { + UpdateGUI(); + if (_boat != null) { + _boat.ChangeBorders((int) canvas.getWidth(), (int) canvas.getHeight()); + } + Draw(); + }); + root.heightProperty().addListener((obs, oldVal, newVal) -> { + UpdateGUI(); + if (_boat != null) { + _boat.ChangeBorders((int) canvas.getWidth(), (int) canvas.getHeight()); + } + Draw(); + }); + } + public EntityBoat CreateRandomBoat() { + Random rnd = new Random(); + return new EntityBoat(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, + Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + } + public IDrawningAdditionalElement CreateRandomAdditionalElement() { + Random rnd = new Random(); + int oarsType = rnd.nextInt(0, 3); + int numOars = rnd.nextInt(4, 7); + IDrawningAdditionalElement additionalElement; + switch (oarsType) { + case 1 -> + additionalElement = new DrawningSlimOars(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + case 2 -> + additionalElement = new DrawningLargeOars(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + default -> + additionalElement = new DrawningOars(Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + } + additionalElement.SetNumberOars(numOars); + + return additionalElement; + } + @FXML + void ButtonCreate_Click() { + _boat = _setMixedBoats.GetRandomBoat(); + SetData(); + Draw(); + } + @FXML + void ButtonMove_Click(ActionEvent event) { + if (_boat == null) { + return; + } + String buttonName = ((Button)event.getSource()).getId(); + switch (buttonName) { + case "buttonUp" -> _boat.MoveTransport(Direction.Up); + case "buttonDown" -> _boat.MoveTransport(Direction.Down); + case "buttonLeft" -> _boat.MoveTransport(Direction.Left); + case "buttonRight" -> _boat.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 (_boat != null) { + _boat.DrawTransport(gc); + } + } + private void SetData() + { + Random rnd = new Random(); + _boat.SetPosition(rnd.nextInt(90) + 10, rnd.nextInt(90), + (int) canvas.getWidth(), (int) canvas.getHeight()); + labelSpeedValue.setText(Integer.toString(_boat.GetBoat().GetSpeed())); + labelWeightValue.setText(Double.toString(_boat.GetBoat().GetWeight())); + labelBodyColorValue.setText(_boat.GetBoat().GetBodyColor().toString()); + } +} diff --git a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/Direction.java b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/Direction.java index 3754719..9aa98d9 100644 --- a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/Direction.java +++ b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/Direction.java @@ -1,6 +1,7 @@ package com.example.pibd22_kalyshev_y_v_motorboat_hard; public enum Direction { + None, Up, Down, Left, diff --git a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/DrawningBoat.java b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/DrawningBoat.java index ec9c70f..2f1f8ba 100644 --- a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/DrawningBoat.java +++ b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/DrawningBoat.java @@ -36,6 +36,10 @@ public class DrawningBoat { _boatHeight = boatHeight; _boatWidth = boatWidth; } + public DrawningBoat(EntityBoat boat, IDrawningAdditionalElement additionalElement) { + _boat = boat; + _drawningOars = additionalElement; + } public void SetPosition(int x, int y, int width, int height) { if (x < 0 || y < 0) { return; diff --git a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/DrawningObjectBoat.java b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/DrawningObjectBoat.java index dcd6573..ab85e57 100644 --- a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/DrawningObjectBoat.java +++ b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/DrawningObjectBoat.java @@ -4,6 +4,10 @@ import javafx.scene.canvas.GraphicsContext; public class DrawningObjectBoat implements IDrawningObject { private DrawningBoat _boat = null; + public DrawningBoat GetBoat() + { + return _boat; + } public DrawningObjectBoat(DrawningBoat boat) { _boat = boat; diff --git a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/FormMapWithSetBoats.java b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/FormMapWithSetBoats.java new file mode 100644 index 0000000..87cfcd6 --- /dev/null +++ b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/FormMapWithSetBoats.java @@ -0,0 +1,131 @@ +package com.example.pibd22_kalyshev_y_v_motorboat_hard; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.canvas.Canvas; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.ListView; +import javafx.scene.control.TextField; +import javafx.scene.layout.Pane; +import javafx.scene.layout.VBox; +import javafx.stage.Stage; + +import java.io.IOException; + +public class FormMapWithSetBoats extends Application { + @Override + public void start(Stage stage) throws IOException { + FXMLLoader fxmlLoader = new FXMLLoader(FormBoat.class.getResource("form-map-with-set-boats-view.fxml")); + Scene scene = new Scene(fxmlLoader.load(), 640, 540); + + FirstUpdateGUI(scene, fxmlLoader); + + stage.setTitle("Map with a set of objects"); + stage.setScene(scene); + + stage.show(); + } + private void FirstUpdateGUI(Scene scene, FXMLLoader fxmlLoader) { + Pane root = (Pane)scene.lookup("#root"); + Canvas canvas = (Canvas)scene.lookup("#canvas"); + Pane paneRight = (Pane)scene.lookup("#paneRight"); + VBox vBoxMaps = (VBox)scene.lookup("#vBoxMaps"); + Label labelTools = (Label)scene.lookup("#labelTools"); + @SuppressWarnings("unchecked") + ListView listViewMaps = (ListView)scene.lookup("#listViewMaps"); + Button buttonAddMap = (Button)scene.lookup("#buttonAddMap"); + Button buttonDeleteMap = (Button)scene.lookup("#buttonDeleteMap"); + Button buttonAddBoat = (Button)scene.lookup("#buttonAddBoat"); + TextField textFieldPosition = (TextField)scene.lookup("#textFieldPosition"); + Button buttonRemoveBoat = (Button)scene.lookup("#buttonRemoveBoat"); + Button buttonShowRemovedBoat = (Button)scene.lookup("#buttonShowRemovedBoat"); + 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 = buttonRemoveBoat.getWidth() + paneRightPadding * 6; + double standardHeight = buttonRemoveBoat.getHeight(); + double paneRightWidth = standardWidth + paneRightPadding * 2; + + canvas.setWidth(rootWidth - paneRightWidth); + canvas.setHeight(rootHeight); + ControllerMapWithSetBoats controller = fxmlLoader.getController(); + controller.SetMapsCollection(new MapsCollection((int)canvas.getWidth(), (int)rootHeight, canvas.getGraphicsContext2D())); + + paneRight.setPrefWidth(paneRightWidth); + paneRight.setPrefHeight(rootHeight); + paneRight.setTranslateX(rootWidth - paneRightWidth); + + double topMargin = labelTools.getHeight(); + + vBoxMaps.setPrefWidth(standardWidth); + vBoxMaps.setTranslateX(paneRightPadding); + vBoxMaps.setTranslateY(topMargin); + topMargin += standardHeight * 7.0 + paneRightPadding * 10.0 + labelTools.getHeight(); + + listViewMaps.setPrefHeight(standardHeight * 3.0); + + buttonAddMap.setPrefWidth(standardWidth); + buttonDeleteMap.setPrefWidth(standardWidth); + + buttonAddBoat.setPrefWidth(standardWidth); + buttonAddBoat.setTranslateX(paneRightPadding); + buttonAddBoat.setTranslateY(topMargin); + topMargin += standardHeight + paneRightPadding; + + textFieldPosition.setPrefWidth(standardWidth); + textFieldPosition.setTranslateX(paneRightPadding); + textFieldPosition.setTranslateY(topMargin); + topMargin += standardHeight + paneRightPadding; + + buttonRemoveBoat.setPrefWidth(standardWidth); + buttonRemoveBoat.setTranslateX(paneRightPadding); + buttonRemoveBoat.setTranslateY(topMargin); + topMargin += standardHeight + paneRightPadding; + + buttonShowRemovedBoat.setPrefWidth(standardWidth); + buttonShowRemovedBoat.setTranslateX(paneRightPadding); + buttonShowRemovedBoat.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(); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/FormMap.java b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/FormSetMixedBoats.java similarity index 79% rename from src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/FormMap.java rename to src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/FormSetMixedBoats.java index 13461d7..09f0c22 100644 --- a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/FormMap.java +++ b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/FormSetMixedBoats.java @@ -11,24 +11,23 @@ import javafx.stage.Stage; import java.io.IOException; -public class FormMap extends Application { +public class FormSetMixedBoats extends Application { @Override public void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(FormMap.class.getResource("form-map-view.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 800, 800); + FXMLLoader fxmlLoader = new FXMLLoader(FormSetMixedBoats.class.getResource("form-set-mixed-boats-view.fxml")); + Scene scene = new Scene(fxmlLoader.load(), 320, 240); - stage.setTitle("Map"); + stage.setTitle("Set mixed boats"); stage.setScene(scene); FirstUpdateGUI(scene); stage.show(); } - - private void FirstUpdateGUI(Scene scene) { + private void FirstUpdateGUI(Scene scene) + { 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"); @@ -40,7 +39,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; @@ -51,9 +49,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/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/MapWithSetBoatsGeneric.java b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/MapWithSetBoatsGeneric.java new file mode 100644 index 0000000..1747b0b --- /dev/null +++ b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/MapWithSetBoatsGeneric.java @@ -0,0 +1,126 @@ +package com.example.pibd22_kalyshev_y_v_motorboat_hard; + +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class MapWithSetBoatsGeneric { + private final int _pictureWidth; + private final int _pictureHeight; + private final int _placeSizeWidth = 210; + private final int _placeSizeHeight = 90; + private final SetBoatsGeneric _setBoats; + private GraphicsContext _graphicsContext = null; + private final U _map; + public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map, GraphicsContext gc) { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setBoats = new SetBoatsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + _graphicsContext = gc; + } + public int Add(T armoredVehicle) + { + return _setBoats.Insert(armoredVehicle); + } + public T Delete(int position) + { + return _setBoats.Remove(position); + } + public T Get(int position) + { + return _setBoats.Get(position); + } + public void ShowOnMap() { + Shaking(); + for (T boat : _setBoats) { + if (boat != null) { + _map.CreateMap(_pictureWidth, _pictureHeight, boat, _graphicsContext); + return; + } + } + } + public void ShowSet() { + DrawBackground(); + DrawArmoredVehicles(); + } + public void MoveObject(Direction direction) { + if (_map != null) { + _map.MoveObject(direction); + } + } + private void Shaking() { + int j = _setBoats.GetCount() - 1; + for (int i = 0; i < _setBoats.GetCount(); i++) { + if (_setBoats.Get(i) == null) { + for (; j > i; j--) { + var boat = _setBoats.Get(j); + if (boat != null) { + _setBoats.Insert(boat, i); + _setBoats.Remove(j); + break; + } + } + if (j <= i) { + return; + } + } + } + } + private void DrawBackground() { + _graphicsContext.setFill(Color.rgb(80, 94, 102)); + _graphicsContext.fillRect(0, 0, _pictureWidth, _pictureHeight); + + _graphicsContext.setStroke(Color.BLACK); + _graphicsContext.setLineWidth(3); + _graphicsContext.setFill(Color.rgb(127, 104, 88)); + + _graphicsContext.beginPath(); + + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) { + _graphicsContext.moveTo(i * _placeSizeWidth, j * _placeSizeHeight); + _graphicsContext.lineTo(i * _placeSizeWidth + _placeSizeWidth / 2.0F, j * _placeSizeHeight); + _graphicsContext.stroke(); + if (j == _pictureHeight / _placeSizeHeight) { + _graphicsContext.fillRect(0, j * _placeSizeHeight + 2, _pictureWidth, _pictureHeight - j * _placeSizeHeight); + } + } + if (i == _pictureWidth / _placeSizeWidth - 1) { + _graphicsContext.fillRect(i * _placeSizeWidth + _placeSizeWidth / 2.0F, 0, _pictureWidth - (i * _placeSizeWidth + _placeSizeWidth / 2.0F), _pictureHeight); + } + else { + _graphicsContext.fillRect(i * _placeSizeWidth + _placeSizeWidth / 2.0F, 0, _placeSizeWidth / 2.0F, _pictureHeight); + } + _graphicsContext.moveTo(i * _placeSizeWidth, 0); + _graphicsContext.lineTo(i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + _graphicsContext.stroke(); + _graphicsContext.closePath(); + } + } + private void DrawArmoredVehicles() { + int yNumOfPlaces = _pictureHeight / _placeSizeHeight; + int xNumOfPlaces = _pictureWidth / _placeSizeWidth; + + int RowIndex = yNumOfPlaces - 1; + int ColumnIndex = 0; + + for (T armoredVehicle : _setBoats) { + if (armoredVehicle != null) { + float[] position = armoredVehicle.GetCurrentPosition(); + armoredVehicle.SetObject(ColumnIndex *_placeSizeWidth, + RowIndex * _placeSizeHeight + (_placeSizeHeight - (int)(position[3] - position[1])), + _pictureWidth, _pictureHeight); + armoredVehicle.DrawningObject(_graphicsContext); + } + if (ColumnIndex == xNumOfPlaces - 1) { + ColumnIndex = 0; + RowIndex--; + } + else { + ColumnIndex++; + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/MapsCollection.java b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/MapsCollection.java new file mode 100644 index 0000000..cdc8fa0 --- /dev/null +++ b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/MapsCollection.java @@ -0,0 +1,47 @@ +package com.example.pibd22_kalyshev_y_v_motorboat_hard; + +import javafx.scene.canvas.GraphicsContext; + +import java.util.HashMap; +import java.util.Set; + +public class MapsCollection { + private final HashMap> _mapStorages; + private final int _pictureWidth; + private final int _pictureHeight; + private final GraphicsContext _graphicsContext; + public Set GetKeys() + { + return _mapStorages.keySet(); + } + public MapsCollection(int pictureWidth, int pictureHeight, GraphicsContext graphicsContext) { + _mapStorages = new HashMap<>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + _graphicsContext = graphicsContext; + } + public void AddMap(String name, AbstractMap map) { + if (_mapStorages.containsKey(name)) { + return; + } + MapWithSetBoatsGeneric newMap = + new MapWithSetBoatsGeneric<>(_pictureWidth, _pictureHeight, map, _graphicsContext); + _mapStorages.put(name, newMap); + } + public void DelMap(String name) + { + _mapStorages.remove(name); + } + public MapWithSetBoatsGeneric Get(String ind) { + if (_mapStorages.containsKey(ind)) { + return _mapStorages.get(ind); + } + return null; + } + public DrawningObjectBoat Get(String ind, int position) { + if (_mapStorages.containsKey(ind)) { + return _mapStorages.get(ind).Get(position); + } + return null; + } +} diff --git a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/SetBoatsGeneric.java b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/SetBoatsGeneric.java new file mode 100644 index 0000000..a4c49dd --- /dev/null +++ b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/SetBoatsGeneric.java @@ -0,0 +1,51 @@ +package com.example.pibd22_kalyshev_y_v_motorboat_hard; + +import java.util.ArrayList; +import java.util.Iterator; + +public class SetBoatsGeneric implements Iterable { + private final ArrayList _places; + private final int _maxCount; + public int GetCount() + { + return _places.size(); + } + public SetBoatsGeneric(int count) { + _maxCount = count; + _places = new ArrayList(); + } + public int Insert(T boat) + { + return Insert(boat, 0); + } + public int Insert(T boat, int position) { + if (position < 0 || position > GetCount() || GetCount() == _maxCount) { + return -1; + } + _places.add(position, boat); + return position; + } + public T Remove(int position) { + if (position < 0 || position >= GetCount()) { + return null; + } + return _places.remove(position); + } + public T Get(int position) { + if (position < 0 || position >= GetCount()) { + return null; + } + return _places.get(position); + } + public void Set(int position, T value) { + if (position < 0 || position >= GetCount()) { + return; + } + Insert(value, position); + } + @Override + public Iterator iterator() + { + return _places.iterator(); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/SetMixedBoatsGeneric.java b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/SetMixedBoatsGeneric.java new file mode 100644 index 0000000..9f83592 --- /dev/null +++ b/src/main/java/com/example/pibd22_kalyshev_y_v_motorboat_hard/SetMixedBoatsGeneric.java @@ -0,0 +1,54 @@ +package com.example.pibd22_kalyshev_y_v_motorboat_hard; + +import java.lang.reflect.Array; +import java.util.Random; + +public class SetMixedBoatsGeneric { + private final T[] _boats; + private final U[] _additionalElements; + private final int _maxCount; + private int _boatsCount; + private int _additionalElementsCount; + public SetMixedBoatsGeneric(int maxCount, Class objectT, Class objectU) { + @SuppressWarnings("unchecked") + final T[] boats = (T[]) Array.newInstance(objectT, maxCount); + _boats = boats; + @SuppressWarnings("unchecked") + final U[] additionalElements = (U[]) Array.newInstance(objectU, maxCount); + _additionalElements = additionalElements; + _maxCount = maxCount; + _boatsCount = 0; + _additionalElementsCount = 0; + } + public boolean AddBoat(T boat) { + if (_boatsCount == _maxCount) { + return false; + } + else { + _boats[_boatsCount] = boat; + _boatsCount++; + return true; + } + } + public boolean AddAdditionalElement(U additionalElement) { + if (_additionalElementsCount == _maxCount) { + return false; + } + else { + _additionalElements[_additionalElementsCount] = additionalElement; + _additionalElementsCount++; + return true; + } + } + public DrawningBoat GetRandomBoat() { + if (_boatsCount == 0 || _additionalElementsCount == 0) { + return null; + } + + Random random = new Random(); + T randomArmoredVehicle = _boats[random.nextInt(0, _boatsCount)]; + U randomAdditionalElement = _additionalElements[random.nextInt(0, _additionalElementsCount)]; + + return new DrawningBoat(randomArmoredVehicle, randomAdditionalElement); + } +} \ No newline at end of file diff --git a/src/main/resources/com/example/pibd22_kalyshev_y_v_motorboat_hard/form-boat-view.fxml b/src/main/resources/com/example/pibd22_kalyshev_y_v_motorboat_hard/form-boat-view.fxml index a7d2fe0..b028c51 100644 --- a/src/main/resources/com/example/pibd22_kalyshev_y_v_motorboat_hard/form-boat-view.fxml +++ b/src/main/resources/com/example/pibd22_kalyshev_y_v_motorboat_hard/form-boat-view.fxml @@ -11,11 +11,11 @@ - + + - + @@ -31,13 +31,15 @@ - + - + + + @@ -49,12 +51,12 @@ - + - - + + @@ -93,4 +95,7 @@ + \ No newline at end of file