diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java index 3dc9d8f..2bfd57b 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java @@ -4,6 +4,8 @@ import javafx.beans.InvalidationListener; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; +import javafx.event.Event; +import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; @@ -21,7 +23,7 @@ import static com.example.doubledeckerbus.ControllerMapWithSetBus._mapBusesColle public class ControllerBus { - protected DrawingBus _bus; + public static DrawingBus _bus; public DrawingBus SelectedBus; protected ObservableList countOfDoors = FXCollections.observableArrayList(3, 4, 5); @FXML @@ -60,21 +62,31 @@ public class ControllerBus { @FXML protected ChoiceBox choiceDoors; + @FXML + private ColorPicker bodyColorPicker; + + @FXML + private ColorPicker extraColorPicker; + + @FXML void ButtonCreate_Click(ActionEvent event) { pictureBoxBus.widthProperty().addListener(listener); pictureBoxBus.heightProperty().addListener(listener); Random rnd = new Random(); + _bus = new DrawingBus(rnd.nextInt(100, 300), rnd.nextFloat(1000, 2000), - Color.rgb(rnd.nextInt(256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), choiceDoors.getValue()); - _bus.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), (int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight()); + bodyColorPicker.getValue(), choiceDoors.getValue()); + _bus.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), + (int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight()); BorderChanged(); } void SetData() { Random rnd = new Random(); - _bus.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), (int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight()); + _bus.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), + (int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight()); statusSpeed.setText("Скорость: %s".formatted(_bus.Bus.Speed)); statusWeight.setText("Вес: %s".formatted(_bus.Bus.Weight)); statusColor.setText("Цвет: %s".formatted(_bus.Bus.BodyColor)); @@ -82,6 +94,9 @@ public class ControllerBus { @FXML void ButtonMove_Click(ActionEvent event) { + pictureBoxBus.widthProperty().addListener(listener); + pictureBoxBus.heightProperty().addListener(listener); + if (_bus == null) return; String name = ((Button) event.getSource()).getId(); switch (name) { @@ -97,6 +112,9 @@ public class ControllerBus { public void initialize() { choiceDoors.setItems(countOfDoors); choiceDoors.setValue(3); + if (_bus != null) { + Draw(); + } } InvalidationListener listener = o -> BorderChanged(); @@ -122,10 +140,8 @@ public class ControllerBus { pictureBoxBus.heightProperty().addListener(listener); Random rnd = new Random(); _bus = new DrawingDDB(rnd.nextInt(300), rnd.nextInt(2000), - Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - choiceDoors.getValue(), - Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - rnd.nextBoolean(), rnd.nextBoolean()); + bodyColorPicker.getValue(), choiceDoors.getValue(), + extraColorPicker.getValue(), rnd.nextBoolean(), rnd.nextBoolean()); SetData(); BorderChanged(); } @@ -143,7 +159,7 @@ public class ControllerBus { DrawingObjectBus bus = new DrawingObjectBus(SelectedBus); - if (_mapBusesCollectionGeneric.add(bus) != -1) { + if (ControllerMapWithSetBus.AddNewBus(bus) != -1) { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("SelectBus"); alert.setContentText("Вы создали объект"); @@ -156,11 +172,11 @@ public class ControllerBus { } } + _bus = null; FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormMapWithSetBus.fxml")); Scene scene = new Scene(fxmlLoader.load()); Form.myStage.setTitle("DoubleDeckerBus"); Form.myStage.setScene(scene); Form.myStage.show(); - } } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java index 9562343..caa7a48 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerMapWithSetBus.java @@ -1,4 +1,6 @@ package com.example.doubledeckerbus; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; @@ -12,13 +14,27 @@ import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; import java.io.IOException; +import java.util.HashMap; import java.util.Objects; import java.util.Optional; public class ControllerMapWithSetBus { static MapWithSetBusesGeneric _mapBusesCollectionGeneric; + private HashMap _mapsDict = new HashMap<>(); + { + _mapsDict.put("Простая карта", new SimpleMap()); + _mapsDict.put("Водная карта", new WaterMap()); + } AbstractMap map = new SimpleMap(); + + public static int AddNewBus (DrawingObjectBus bus) { + return _mapsCollection.GetId(selected).add(bus); + } + static public MapsCollection _mapsCollection; + + static String selected; + static String map_name = "Простая карта"; protected ObservableList countOfMap = FXCollections.observableArrayList("Простая карта", "Водная карта"); @@ -58,28 +74,66 @@ public class ControllerMapWithSetBus { @FXML private TextField textBoxPosition; + @FXML + private ListView listViewMaps; + + @FXML + private TextField TextFieldMap; + @FXML private void initialize(){ + gc = canvasBus.getGraphicsContext2D(); + if (selected != null) { + showStorage(); + } + if (_mapsCollection == null) + _mapsCollection = new MapsCollection((int) canvasBus.getWidth(), (int) canvasBus.getHeight()); comboBoxSelectorMap.setItems(countOfMap); comboBoxSelectorMap.setValue(map_name); + listViewMaps.getSelectionModel().selectedItemProperty() + .addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observableValue, String s, String t1) { + selected = t1; + showStorage(); + } + }); + listViewMaps.setItems(_mapsCollection.toObserveList()); } GraphicsContext gc; private void FirstIncome() { - if (_mapBusesCollectionGeneric == null) { - _mapBusesCollectionGeneric = new MapWithSetBusesGeneric( - (int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight(), map); - } - gc = canvasBus.getGraphicsContext2D(); if (comboBoxSelectorMap.getValue() != map_name) { map_name = comboBoxSelectorMap.getValue(); switch (map_name) { case "Простая карта" -> map = new SimpleMap(); case "Водная карта" -> map = new WaterMap(); } - _mapBusesCollectionGeneric.changeMap(map); } } + + private void ReloadMaps() + { + int index = listViewMaps.getSelectionModel().getSelectedIndex(); + + ObservableList listMaps = FXCollections.observableArrayList(); + + for (int i = 0; i < _mapsCollection.Keys().size(); i++) + { + listMaps.add(_mapsCollection.Keys().get(i)); + } + listViewMaps.setItems(listMaps); + + if (listMaps.size() > 0 && (index == -1 || index >= listMaps.size())) + { + listViewMaps.getSelectionModel().select(0); + } + else if (listMaps.size() > 0 && index > -1 && index < listMaps.size()) + { + listViewMaps.getSelectionModel().select(index); + } + } + @FXML private void ButtonAddBus_Click(ActionEvent event) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormBus.fxml")); @@ -90,6 +144,57 @@ public class ControllerMapWithSetBus { FirstIncome(); } + @FXML + private void ButtonAddMap_Click(ActionEvent event) + { + if ((Objects.equals(TextFieldMap.getText(), ""))) + { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("SelectMap"); + alert.setContentText("Не все данные заполнены"); + Optional option = alert.showAndWait(); + return; + } + if (!_mapsDict.containsKey(comboBoxSelectorMap.getValue())) + { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("SelectMap"); + alert.setContentText("Нет такой карты"); + Optional option = alert.showAndWait(); + return; + } + _mapsCollection.AddMap(TextFieldMap.getText(), _mapsDict.get(comboBoxSelectorMap.getValue())); + ReloadMaps(); + showStorage(); + } + @FXML + private void ButtonDeleteMap_Click(ActionEvent event) + { + if (listViewMaps.getSelectionModel().selectedItemProperty().isNull().get()) + { + return; + } + _mapsCollection.DelMap(listViewMaps.getSelectionModel().getSelectedItem()); + ReloadMaps(); + showStorage(); + } + + @FXML + private void ButtonDeleteEditBus_Click(ActionEvent event) throws IOException { + if (selected == null) + { + return; + } + DrawingObjectBus deleteBus = _mapsCollection.get(selected).getDeletedBus(); + if (deleteBus != null) { + ControllerBus._bus = deleteBus.getBus(); + FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormBus.fxml")); + Scene scene = new Scene(fxmlLoader.load()); + Form.myStage.setTitle("DoubleDeckerBus"); + Form.myStage.setScene(scene); + Form.myStage.show(); + } + } @FXML private void ButtonRemoveBus_Click(ActionEvent event) @@ -111,12 +216,13 @@ public class ControllerMapWithSetBus { int pos; try { pos = Integer.parseInt(textBoxPosition.getText()); + if (pos < 1 || pos > _mapsCollection.GetId(selected).getCount()) return; } catch (Exception e) { return; } - if (_mapBusesCollectionGeneric.remove(pos) != null) + if (_mapsCollection.GetId(selected).remove(pos) != null) { alert = new Alert(Alert.AlertType.WARNING); alert.setTitle("RemoveBus"); @@ -130,36 +236,31 @@ public class ControllerMapWithSetBus { alert.setContentText("Не удалось удалить объект"); option = alert.showAndWait(); } + showStorage(); } @FXML private void ButtonShowStorage_Click(ActionEvent event) { FirstIncome(); - if (_mapBusesCollectionGeneric == null) - { - return; - } - gc.setFill(Color.WHITE); - gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight()); - _mapBusesCollectionGeneric.ShowSet(gc); + showStorage(); } @FXML private void ButtonShowOnMap_Click(ActionEvent event) { FirstIncome(); - if (_mapBusesCollectionGeneric == null) { + if (selected == null) { return; } gc.setFill(Color.WHITE); - gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight()); - _mapBusesCollectionGeneric.ShowOnMap(gc); + gc.fillRect(0, 0, canvasBus.getWidth(), canvasBus.getHeight()); + _mapsCollection.GetId(selected).ShowOnMap(gc); } @FXML private void ButtonMove_Click(ActionEvent event) { - if (_mapBusesCollectionGeneric == null) + if (selected == null) { return; } @@ -172,8 +273,19 @@ public class ControllerMapWithSetBus { case "buttonLeft" -> dir = Direction.Left; case "buttonRight" -> dir = Direction.Right; } - _mapBusesCollectionGeneric.MoveObject(dir); + _mapsCollection.GetId(selected).MoveObject(dir); } + + private void showStorage() { + if (selected == null) + { + return; + } + gc.setFill(Color.WHITE); + gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight()); + _mapsCollection.GetId(selected).ShowSet(gc); + } + } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java index dc37f2d..3abfd35 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingObjectBus.java @@ -39,4 +39,8 @@ public class DrawingObjectBus implements IDrawingObject { { _bus.DrawTransport(gc); } + + public DrawingBus getBus() { + return _bus; + } } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java index 7372e67..eb0b4d1 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java @@ -12,7 +12,7 @@ public class Form extends Application { @Override public void start(Stage stage) throws IOException { - FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormPolymorph.fxml")); + FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormMapWithSetBus.fxml")); Scene scene = new Scene(fxmlLoader.load()); myStage = stage; myStage.setTitle("DoubleDeckerBus"); diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java index a94d632..43074d1 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapWithSetBusesGeneric.java @@ -3,7 +3,7 @@ package com.example.doubledeckerbus; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; -import java.util.Collection; +import java.util.Stack; public class MapWithSetBusesGeneric { private final int _pictureWidth; @@ -11,6 +11,7 @@ public class MapWithSetBusesGeneric _setBuses; + private final Stack _deletedBuses; private U _map; public MapWithSetBusesGeneric(int picWidth, int picHeight, U map) @@ -21,6 +22,7 @@ public class MapWithSetBusesGeneric(); } public void changeMap(U map) { @@ -34,7 +36,15 @@ public class MapWithSetBusesGeneric 0) currentWidth--; @@ -130,5 +139,13 @@ public class MapWithSetBusesGeneric height) return; } } + + public int getCount() { + return _setBuses.Count(); + } + + public T getBus(int ind){ + return _setBuses.Get(ind); + } } diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapsCollection.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapsCollection.java new file mode 100644 index 0000000..b802b7d --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/MapsCollection.java @@ -0,0 +1,65 @@ +package com.example.doubledeckerbus; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; + +import java.util.HashMap; +import java.util.List; + +public class MapsCollection { + HashMap> _mapStorages; + + public List Keys() { + return _mapStorages.keySet().stream().toList(); + } + + private int _pictureWidth; + + private int _pictureHeight; + + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new HashMap<>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddMap(String name, AbstractMap map) + { + if (Keys().contains(name)) return; + _mapStorages.put(name, new MapWithSetBusesGeneric(_pictureWidth, _pictureHeight, map)); + } + + public void DelMap(String name) + { + _mapStorages.remove(name); + } + + public MapWithSetBusesGeneric GetId(String id) + { + return _mapStorages.get(id); + } + + public ObservableList toObserveList() { + ObservableList result = FXCollections.observableArrayList(); + result.addAll(_mapStorages.keySet()); + return result; + } + + public MapWithSetBusesGeneric get(String id) { + if (_mapStorages.containsKey(id)) + { + return _mapStorages.get(id); + } + return null; + } + + public DrawingObjectBus get(String name, int id) { + if (_mapStorages.containsKey(name)) + { + return _mapStorages.get(name).getBus(id); + } + return null; + } + +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java index c6e3c6c..983a1c5 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/SetBusesGeneric.java @@ -1,15 +1,20 @@ package com.example.doubledeckerbus; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Objects; class SetBusesGeneric { - private Object[] _places; + private ArrayList _places; private int BusyPlaces = 0; + private int _maxCount; + public SetBusesGeneric(int count) { - _places = new Object[count]; + _maxCount = count; + _places = new ArrayList<>(); } public int Insert(T bus) @@ -19,37 +24,39 @@ class SetBusesGeneric { public int Insert(T bus, int position) { - if (position < 0 || position >= _places.length|| BusyPlaces == _places.length) return -1; + if (position < 0 || position >= _maxCount || BusyPlaces == _maxCount) return -1; BusyPlaces++; - while (_places[position] != null) { - for (int i = _places.length - 1; i > 0; --i) { - if (_places[i] == null && _places[i - 1] != null) { - _places[i] = _places[i - 1]; - _places[i - 1] = null; - } - } - } - _places[position] = bus; + _places.add(position, bus); return position; } public T Remove(int position) { - if (position < 0 || position >= _places.length) return null; - T savedBus = (T)_places[position]; - _places[position] = null; + if (position < 0 || position >= _maxCount) return null; + T savedBus = _places.get(position - 1); + _places.set(position - 1, null); return savedBus; } public T Get(int position) { - if (position < 0 || position >= _places.length) return null; - return (T)_places[position]; + if (position < 0 || position >= _maxCount) return null; + return _places.get(position); } + + public ArrayList GetBuses() { + ArrayList result = new ArrayList<>(); + for (var bus: _places) { + if (bus != null){ + result.add(bus); + } + } + return result; + } public int Count() { - return _places.length; + return _places.size(); } } diff --git a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBus.fxml b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBus.fxml index 49bbe9c..23b7112 100644 --- a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBus.fxml +++ b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormBus.fxml @@ -4,6 +4,7 @@ + @@ -27,13 +28,15 @@