This commit is contained in:
root 2022-12-22 22:44:26 +04:00
parent dfde6a71b7
commit 1b8092fb16
12 changed files with 691 additions and 130 deletions

View File

@ -0,0 +1,153 @@
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 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<String> comboBoxNumOars;
@FXML
private ComboBox<String> comboBoxOarsType;
@FXML
private ComboBox<String> 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();
DrawningBoat boat = new DrawningBoat(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
SetData(boat);
}
@FXML
void ButtonCreateModif_Click() {
Random rnd = new Random();
DrawningSpeedboat 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)),
(rnd.nextInt(2) != 0), (rnd.nextInt(2) != 0), (rnd.nextInt(2) != 0));
SetData(boat);
}
@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 "My second map" -> _abstractMap = new MySecondMap();
}
}
private void ChangeDrawningOars(DrawningBoat boat) {
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;
};
if (newDrawningOars != null) {
newDrawningOars.SetNumberOars(Integer.parseInt(comboBoxNumOars.getValue()));
boat.SetDrawningOars(newDrawningOars);
}
}
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(DrawningBoat boat) {
ChangeDrawningOars(boat);
labelSpeedValue.setText(Integer.toString(boat.GetBoat().GetSpeed()));
labelWeightValue.setText(Double.toString(boat.GetBoat().GetWeight()));
labelBodyColorValue.setText(boat.GetBoat().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 DrawningObjectBoat(boat), gc);
}
}

View File

@ -24,14 +24,12 @@ public class ControllerMapWithSetBoats {
@FXML
private Pane paneRight;
@FXML
private Label labelTools;
@FXML
private ComboBox<String> comboBoxSelectorMap;
@FXML
private Button buttonAddBoat;
@FXML
private TextField textFieldPosition;
@FXML
private TextField textFieldNewMapName;
@FXML
private Button buttonLeft;
@FXML
private Button buttonRight;
@ -39,9 +37,28 @@ public class ControllerMapWithSetBoats {
private Button buttonUp;
@FXML
private Button buttonDown;
private MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap> _mapBoatsCollectionGeneric;
@FXML
private ListView<String> listViewMaps;
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<String, AbstractMap>();
private MapsCollection _mapsCollection;
private Stack<DrawningBoat> _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<String, AbstractMap> map : _mapsDict.entrySet()) {
comboBoxSelectorMap.getItems().add(map.getKey());
}
_removedBoats = new Stack<>();
textFieldPosition.textProperty().addListener((ov, oldValue, newValue) -> {
if (Objects.equals(newValue, "")) {
return;
@ -62,32 +79,25 @@ public class ControllerMapWithSetBoats {
root.heightProperty().addListener((obs, oldVal, newVal) -> {
UpdateGUI();
});
}
@FXML
public void ComboBoxSelectorMap_Changed() {
AbstractMap map = switch (comboBoxSelectorMap.getValue()) {
case "Simple map" -> new SimpleMap();
case "My second map" -> new MySecondMap();
default -> null;
};
if (map != null) {
_mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap>(
(int)canvas.getWidth(), (int)canvas.getHeight(), map, DrawningObjectBoat.class, canvas.getGraphicsContext2D());
} else {
_mapBoatsCollectionGeneric = null;
}
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 (_mapBoatsCollectionGeneric == null) {
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);
Scene sceneBoat = new Scene(fxmlLoader.load(), 500, 400);
FirstUpdateBoatGUI(sceneBoat, stageBoat, fxmlLoader);
FirstUpdateBoatGUI(sceneBoat, stageBoat, fxmlLoader, false);
stageBoat.setTitle("Boat");
stageBoat.setScene(sceneBoat);
@ -95,6 +105,10 @@ public class ControllerMapWithSetBoats {
}
@FXML
private void ButtonRemoveBoat_Click() {
if (listViewMaps.getSelectionModel().getSelectedIndex() == -1) {
return;
}
String stringPosition = textFieldPosition.getText();
if (stringPosition == null || stringPosition.length() == 0) {
return;
@ -105,35 +119,50 @@ public class ControllerMapWithSetBoats {
Alert infoAlert;
if (removeAlert.getResult() == ButtonType.YES) {
String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem();
int pos = Integer.parseInt(stringPosition);
if (_mapBoatsCollectionGeneric.Delete(pos) != null) {
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);
_mapBoatsCollectionGeneric.ShowSet();
} else {
_removedBoats.add(removedBoat.GetBoat());
_mapsCollection.Get(selectedMapName).ShowSet();
}
else {
infoAlert = new Alert(Alert.AlertType.INFORMATION, "Failed to remove object", ButtonType.OK);
}
} else {
}
else {
infoAlert = new Alert(Alert.AlertType.INFORMATION, "Remove operation canceled", ButtonType.OK);
}
infoAlert.showAndWait();
}
@FXML
private void ButtonShowStorage_Click() {
if (_mapBoatsCollectionGeneric == null) {
private void ButtonShowStorage_Click()
{
if (listViewMaps.getSelectionModel().getSelectedIndex() == -1) {
return;
}
_mapBoatsCollectionGeneric.ShowSet();
String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem();
if (selectedMapName != null) {
_mapsCollection.Get(selectedMapName).ShowSet();
}
}
@FXML
private void ButtonShowOnMap_Click() {
if (_mapBoatsCollectionGeneric == null) {
if (listViewMaps.getSelectionModel().getSelectedIndex() == -1) {
return;
}
_mapBoatsCollectionGeneric.ShowOnMap();
String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem();
if (selectedMapName != null) {
_mapsCollection.Get(selectedMapName).ShowOnMap();
}
}
@FXML
private void ButtonMove_Click(ActionEvent event) {
if (_mapBoatsCollectionGeneric == null) {
if (listViewMaps.getSelectionModel().getSelectedIndex() == -1) {
return;
}
@ -145,9 +174,69 @@ public class ControllerMapWithSetBoats {
case "buttonRight" -> Direction.Right;
default -> Direction.None;
};
_mapBoatsCollectionGeneric.MoveObject(dir);
String selectedMapName = listViewMaps.getSelectionModel().getSelectedItem();
if (selectedMapName != null) {
_mapsCollection.Get(selectedMapName).MoveObject(dir);
}
}
private void UpdateGUI() {
@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 ButtonShowRemovedBoats_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 stageBoat = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader(FormBoat.class.getResource("form-removed-boat-view.fxml"));
Scene sceneBoat = new Scene(fxmlLoader.load(), 320, 240);
FirstUpdateBoatGUI(sceneBoat, stageBoat, fxmlLoader, true);
stageBoat.setTitle("Removed armored vehicle");
stageBoat.setScene(sceneBoat);
stageBoat.show();
ControllerRemovedBoat controllerRemovedBoat = fxmlLoader.getController();
controllerRemovedBoat.SetBoat(_removedBoats.pop());
}
}
private void UpdateGUI()
{
double rootWidth = root.getWidth();
double rootHeight = root.getHeight();
double rightPaneWidth = paneRight.getWidth();
@ -175,16 +264,14 @@ public class ControllerMapWithSetBoats {
buttonRight.setTranslateY(rootHeight - moveButtonsSize - rightPaneMargin);
buttonRight.setTranslateX(rootWidth - moveButtonsSize - moveButtonsXMargin);
}
private void FirstUpdateBoatGUI(Scene scene, Stage stageBoat, FXMLLoader fxmlLoader) {
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 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 buttonSelectBoat = (Button) scene.lookup("#buttonSelectBoat");
FlowPane flowPane = (FlowPane) scene.lookup("#flowPane");
root.applyCss();
@ -203,9 +290,7 @@ public class ControllerMapWithSetBoats {
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);
@ -220,24 +305,53 @@ public class ControllerMapWithSetBoats {
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());
buttonSelectBoat.setOnAction(e ->
if (!formRemovedBoat) {
Button buttonCreateModif = (Button) scene.lookup("#buttonCreateModif");
Button buttonSelectBoat = (Button) scene.lookup("#buttonSelectBoat");
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())
{
stageBoat.close();
ControllerBoat controllerBoat = fxmlLoader.getController();
DrawningObjectBoat boat = new DrawningObjectBoat(controllerBoat.GetSelectedBoat());
listViewMaps.getItems().add(key);
}
Alert alert;
if (_mapBoatsCollectionGeneric.Add(boat) != -1) {
alert = new Alert(Alert.AlertType.INFORMATION, "Object added", ButtonType.OK);
_mapBoatsCollectionGeneric.ShowSet();
} else {
alert = new Alert(Alert.AlertType.ERROR, "Failed to add object", ButtonType.OK);
}
alert.showAndWait();
});
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);
}
}
}

View File

@ -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<String> 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);
}
}
}

View File

@ -32,7 +32,6 @@ public class FormBoat extends Application {
Button buttonLeft = (Button)scene.lookup("#buttonLeft");
Button buttonRight = (Button)scene.lookup("#buttonRight");
Button buttonDown = (Button)scene.lookup("#buttonDown");
Button buttonSelectBoat = (Button)scene.lookup("#buttonSelectBoat");
FlowPane flowPane = (FlowPane)scene.lookup("#flowPane");
root.applyCss();
@ -67,10 +66,6 @@ public class FormBoat extends Application {
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());
}
public static void main(String[] args)
{

View File

@ -4,8 +4,12 @@ import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.*;
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;
@ -16,24 +20,27 @@ public class FormMapWithSetBoats extends Application {
FXMLLoader fxmlLoader = new FXMLLoader(FormBoat.class.getResource("form-map-with-set-boats-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 1000, 540);
FirstUpdateGUI(scene);
FirstUpdateGUI(scene, fxmlLoader);
stage.setTitle("Map with a set of objects");
stage.setScene(scene);
stage.show();
}
private void FirstUpdateGUI(Scene scene) {
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")
ComboBox<String> comboBoxSelectorMap = (ComboBox<String>)scene.lookup("#comboBoxSelectorMap");
ListView<String> listViewMaps = (ListView<String>)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");
@ -47,21 +54,30 @@ public class FormMapWithSetBoats extends Application {
double paneRightPadding = 5.0;
double rootWidth = root.getWidth();
double rootHeight = root.getHeight();
double standardWidth = buttonRemoveBoat.getWidth();
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();
comboBoxSelectorMap.setTranslateX(paneRightPadding);
comboBoxSelectorMap.setTranslateY(topMargin);
topMargin += standardHeight + paneRightPadding * 3.0;
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);
@ -73,8 +89,14 @@ public class FormMapWithSetBoats extends Application {
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);
@ -102,9 +124,8 @@ public class FormMapWithSetBoats extends Application {
buttonRight.setTranslateY(rootHeight - moveButtonsSize - paneRightPadding);
buttonRight.setTranslateX(rootWidth - moveButtonsSize - moveButtonsXMargin);
}
public static void main(String[] args)
{
launch();
}
}
}

View File

@ -4,6 +4,7 @@ import javafx.scene.canvas.GraphicsContext;
public interface IDrawningObject {
public float GetStep();
public IDrawningAdditionalElement GetDrawningOars();
public void SetObject(int x, int y, int width, int height);
public void MoveObject(Direction direction);
public void DrawningObject(GraphicsContext gc);

View File

@ -11,10 +11,10 @@ public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends Abstra
private final SetBoatsGeneric<T> _setBoats;
private GraphicsContext _graphicsContext = null;
private final U _map;
public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map, Class<T> objectT, GraphicsContext gc) {
public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map, GraphicsContext gc) {
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setBoats = new SetBoatsGeneric<T>(width * height, objectT);
_setBoats = new SetBoatsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
@ -34,10 +34,9 @@ public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends Abstra
}
public void ShowOnMap() {
Shaking();
for (int i = 0; i < _setBoats.GetCount(); i++) {
var armoredVehicle = _setBoats.Get(i);
if (armoredVehicle != null) {
_map.CreateMap(_pictureWidth, _pictureHeight, armoredVehicle, _graphicsContext);
for (T boat : _setBoats) {
if (boat != null) {
_map.CreateMap(_pictureWidth, _pictureHeight, boat, _graphicsContext);
return;
}
}
@ -107,13 +106,13 @@ public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends Abstra
int RowIndex = 0;
int ColumnIndex = 0;
for (int i = 0; i < _setBoats.GetCount(); i++) {
if (_setBoats.Get(i) != null) {
float[] position = _setBoats.Get(i).GetCurrentPosition();
_setBoats.Get(i).SetObject(ColumnIndex *_placeSizeWidth,
for (T boat : _setBoats) {
if (boat != null) {
float[] position = boat.GetCurrentPosition();
boat.SetObject(ColumnIndex *_placeSizeWidth,
RowIndex * _placeSizeHeight + (_placeSizeHeight - (int)(position[3]-position[1])) - 70,
_pictureWidth, _pictureHeight);
_setBoats.Get(i).DrawningObject(_graphicsContext);
boat.DrawningObject(_graphicsContext);
}
if (ColumnIndex == xNumOfPlaces-1) {
ColumnIndex = 0;

View File

@ -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<String, MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap>> _mapStorages;
private final int _pictureWidth;
private final int _pictureHeight;
private final GraphicsContext _graphicsContext;
public Set<String> 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<DrawningObjectBoat, AbstractMap> newMap =
new MapWithSetBoatsGeneric<>(_pictureWidth, _pictureHeight, map, _graphicsContext);
_mapStorages.put(name, newMap);
}
public void DelMap(String name)
{
_mapStorages.remove(name);
}
public MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap> 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;
}
}

View File

@ -1,65 +1,51 @@
package com.example.pibd22_kalyshev_y_v_motorboat_hard;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
public class SetBoatsGeneric<T> {
private final T[] _places;
public class SetBoatsGeneric <T> implements Iterable<T> {
private final ArrayList<T> _places;
private final int _maxCount;
public int GetCount()
{
return _places.length;
return _places.size();
}
public SetBoatsGeneric(int count, Class<T> objectT) {
@SuppressWarnings("unchecked")
final T[] places = (T[]) Array.newInstance(objectT, count);
_places = places;
public SetBoatsGeneric(int count) {
_maxCount = count;
_places = new ArrayList<T>();
}
public int Insert(T boat)
{
return Insert(boat, 0);
}
public int Insert(T boat, int position) {
if (position < 0 || position > _places.length) {
if (position < 0 || position > GetCount() || GetCount() == _maxCount) {
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] = boat;
return position;
}
if (emptyCellIndex == -1) {
return -1;
} else {
_places[position] = boat;
return position;
}
_places.add(position, boat);
return position;
}
public T Remove(int position) {
if (position < 0 || position >= GetCount()) {
return null;
}
T removedObject = _places[position];
_places[position] = null;
return removedObject;
return _places.remove(position);
}
public T Get(int position) {
if (position < 0 || position >= GetCount()) {
return null;
}
return _places[position];
return _places.get(position);
}
public void Set(int position, T value) {
if (position < 0 || position >= GetCount()) {
return;
}
Insert(value, position);
}
@Override
public Iterator<T> iterator()
{
return _places.iterator();
}
}

View File

@ -51,4 +51,4 @@ public class SetMixedBoatsGeneric<T extends EntityBoat, U extends IDrawningAddit
return new DrawningBoat(randomBoat, randomAdditionalElement);
}
}
}

View File

@ -11,6 +11,8 @@
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.ListView?>
<Pane xmlns:fx="http://javafx.com/fxml" fx:id="root" fx:controller="com.example.pibd22_kalyshev_y_v_motorboat_hard.ControllerMapWithSetBoats">
<Canvas fx:id="canvas">
</Canvas>
@ -18,14 +20,28 @@
<Label fx:id="labelTools" style="-fx-text-fill: #b8becc; -fx-padding: 5 5 5 5; -fx-font-weight: bold;">
Tools
</Label>
<ComboBox fx:id="comboBoxSelectorMap" value="Simple map" onAction="#ComboBoxSelectorMap_Changed">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Simple map"/>
<String fx:value="My second map"/>
</FXCollections>
</items>
</ComboBox>
<VBox fx:id="vBoxMaps" style="-fx-border-color: #b8becc; -fx-border-radius: 5; -fx-border-width: 2; -fx-padding: 5 5 5 5;" spacing="5">
<Label style="-fx-text-fill: #b8becc; -fx-padding: 0 0 0 0; -fx-font-weight: bold;">
Maps
</Label>
<TextField fx:id="textFieldNewMapName" />
<ComboBox fx:id="comboBoxSelectorMap" value="Simple map">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Simple map"/>
<String fx:value="My second map"/>
</FXCollections>
</items>
</ComboBox>
<Button fx:id="buttonAddMap" onAction="#ButtonAddMap_Click">
Add map
</Button>
<ListView fx:id="listViewMaps">
</ListView>
<Button fx:id="buttonDeleteMap" onAction="#ButtonDeleteMap_Click">
Delete map
</Button>
</VBox>
<Button fx:id="buttonAddBoat" onAction="#ButtonAddBoat_Click">
Add boat
</Button>
@ -33,6 +49,9 @@
<Button fx:id="buttonRemoveBoat" onAction="#ButtonRemoveBoat_Click">
Remove boat
</Button>
<Button fx:id="buttonShowRemovedBoat" onAction="#ButtonShowRemovedBoats_Click">
Show removed
</Button>
<Button fx:id="buttonShowStorage" onAction="#ButtonShowStorage_Click">
Show storage
</Button>
@ -68,5 +87,4 @@
</ImageView>
</graphic>
</Button>
</Pane>
</Pane>

View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.collections.FXCollections?>
<?import java.lang.String?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>
<Pane xmlns:fx="http://javafx.com/fxml" fx:id="root" fx:controller="com.example.pibd22_kalyshev_y_v_motorboat_hard.ControllerRemovedBoat">
<Canvas fx:id="canvas">
</Canvas>
<FlowPane fx:id="flowPane" style="-fx-background-color: #31374c;">
<Label style="-fx-text-fill: #b8becc; -fx-padding: 5 5 5 5;">
Speed:
</Label>
<Label fx:id="labelSpeedValue" style="-fx-text-fill: #ffffff; -fx-padding: 5 5 5 0; -fx-font-weight: bold;">
-
</Label>
<Label style="-fx-text-fill: #b8becc; -fx-padding: 5 5 5 5;">
Weight:
</Label>
<Label fx:id="labelWeightValue" style="-fx-text-fill: #ffffff; -fx-padding: 5 5 5 0; -fx-font-weight: bold;">
-
</Label>
<Label style="-fx-text-fill: #b8becc; -fx-padding: 5 5 5 5;">
Color:
</Label>
<Label fx:id="labelBodyColorValue" style="-fx-text-fill: #ffffff; -fx-padding: 5 5 5 0; -fx-font-weight: bold;">
-
</Label>
<Label style="-fx-text-fill: #b8becc; -fx-padding: 5 5 5 5;">
Oars:
</Label>
<ComboBox fx:id="comboBoxNumOars" value="5" onAction="#ComboBoxNumOars_Changed">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="1"/>
<String fx:value="2"/>
<String fx:value="3"/>
</FXCollections>
</items>
</ComboBox>
</FlowPane>
<Button fx:id="buttonCreate" onAction="#ButtonCreate_Click">
Create
</Button>
<Button fx:id="buttonLeft" minWidth="30" minHeight="30" onAction="#ButtonMove_Click">
<graphic>
<ImageView fitHeight="14.0" fitWidth="14.0" pickOnBounds="true" preserveRatio="true">
<Image url="@/arrowLeft.png"/>
</ImageView>
</graphic>
</Button>
<Button fx:id="buttonRight" minWidth="30" minHeight="30" onAction="#ButtonMove_Click">
<graphic>
<ImageView fitHeight="14.0" fitWidth="14.0" pickOnBounds="true" preserveRatio="true">
<Image url="@/arrowRight.png"/>
</ImageView>
</graphic>
</Button>
<Button fx:id="buttonUp" minWidth="30" minHeight="30" onAction="#ButtonMove_Click">
<graphic>
<ImageView fitHeight="14.0" fitWidth="14.0" pickOnBounds="true" preserveRatio="true">
<Image url="@/arrowUp.png"/>
</ImageView>
</graphic>
</Button>
<Button fx:id="buttonDown" minWidth="30" minHeight="30" onAction="#ButtonMove_Click">
<graphic>
<ImageView fitHeight="14.0" fitWidth="14.0" pickOnBounds="true" preserveRatio="true">
<Image url="@/arrowDown.png"/>
</ImageView>
</graphic>
</Button>
</Pane>