LabWork from base

This commit is contained in:
shadowik 2022-11-15 02:24:45 +04:00
parent dee0586664
commit 393c030e88
7 changed files with 201 additions and 48 deletions

View File

@ -143,7 +143,7 @@ public class ControllerBus {
DrawingObjectBus bus = new DrawingObjectBus(SelectedBus); DrawingObjectBus bus = new DrawingObjectBus(SelectedBus);
if (_mapBusesCollectionGeneric.add(bus) != -1) { if (ControllerMapWithSetBus.AddNewBus(bus) != -1) {
Alert alert = new Alert(Alert.AlertType.INFORMATION); Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("SelectBus"); alert.setTitle("SelectBus");
alert.setContentText("Вы создали объект"); alert.setContentText("Вы создали объект");

View File

@ -1,4 +1,6 @@
package com.example.doubledeckerbus; package com.example.doubledeckerbus;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
@ -12,13 +14,27 @@ import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
public class ControllerMapWithSetBus { public class ControllerMapWithSetBus {
static MapWithSetBusesGeneric<DrawingObjectBus, AbstractMap> _mapBusesCollectionGeneric; static MapWithSetBusesGeneric<DrawingObjectBus, AbstractMap> _mapBusesCollectionGeneric;
private HashMap<String, AbstractMap> _mapsDict = new HashMap<>();
{
_mapsDict.put("Простая карта", new SimpleMap());
_mapsDict.put("Водная карта", new WaterMap());
}
AbstractMap map = new SimpleMap(); 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 = "Простая карта"; static String map_name = "Простая карта";
protected ObservableList<String> countOfMap = FXCollections.observableArrayList("Простая карта", "Водная карта"); protected ObservableList<String> countOfMap = FXCollections.observableArrayList("Простая карта", "Водная карта");
@ -58,18 +74,32 @@ public class ControllerMapWithSetBus {
@FXML @FXML
private TextField textBoxPosition; private TextField textBoxPosition;
@FXML
private ListView<String> listViewMaps;
@FXML
private TextField TextFieldMap;
@FXML @FXML
private void initialize(){ private void initialize(){
// canvasBus.setWidth(pictureBoxBus.getWidth());
// canvasBus.setWidth(pictureBoxBus.getHeight());
if (_mapsCollection == null)
_mapsCollection = new MapsCollection((int) canvasBus.getWidth(), (int) canvasBus.getHeight());
comboBoxSelectorMap.setItems(countOfMap); comboBoxSelectorMap.setItems(countOfMap);
comboBoxSelectorMap.setValue(map_name); comboBoxSelectorMap.setValue(map_name);
listViewMaps.getSelectionModel().selectedItemProperty()
.addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observableValue, String s, String t1) {
selected = t1;
}
});
listViewMaps.setItems(_mapsCollection.toObserveList());
} }
GraphicsContext gc; GraphicsContext gc;
private void FirstIncome() { private void FirstIncome() {
if (_mapBusesCollectionGeneric == null) {
_mapBusesCollectionGeneric = new MapWithSetBusesGeneric<DrawingObjectBus, AbstractMap>(
(int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight(), map);
}
gc = canvasBus.getGraphicsContext2D(); gc = canvasBus.getGraphicsContext2D();
if (comboBoxSelectorMap.getValue() != map_name) { if (comboBoxSelectorMap.getValue() != map_name) {
map_name = comboBoxSelectorMap.getValue(); map_name = comboBoxSelectorMap.getValue();
@ -77,9 +107,31 @@ public class ControllerMapWithSetBus {
case "Простая карта" -> map = new SimpleMap(); case "Простая карта" -> map = new SimpleMap();
case "Водная карта" -> map = new WaterMap(); case "Водная карта" -> map = new WaterMap();
} }
_mapBusesCollectionGeneric.changeMap(map);
} }
} }
private void ReloadMaps()
{
int index = listViewMaps.getSelectionModel().getSelectedIndex();
ObservableList<String> 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 @FXML
private void ButtonAddBus_Click(ActionEvent event) throws IOException { private void ButtonAddBus_Click(ActionEvent event) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormBus.fxml")); FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormBus.fxml"));
@ -90,6 +142,39 @@ public class ControllerMapWithSetBus {
FirstIncome(); 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<ButtonType> option = alert.showAndWait();
return;
}
if (!_mapsDict.containsKey(comboBoxSelectorMap.getValue()))
{
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("SelectMap");
alert.setContentText("Нет такой карты");
Optional<ButtonType> option = alert.showAndWait();
return;
}
_mapsCollection.AddMap(TextFieldMap.getText(), _mapsDict.get(comboBoxSelectorMap.getValue()));
ReloadMaps();
}
@FXML
private void ButtonDeleteMap_Click(ActionEvent event)
{
if (listViewMaps.getSelectionModel().selectedItemProperty().isNull().get())
{
return;
}
_mapsCollection.DelMap(listViewMaps.getSelectionModel().getSelectedItem());
ReloadMaps();
}
@FXML @FXML
private void ButtonRemoveBus_Click(ActionEvent event) private void ButtonRemoveBus_Click(ActionEvent event)
@ -116,7 +201,7 @@ public class ControllerMapWithSetBus {
return; return;
} }
if (_mapBusesCollectionGeneric.remove(pos) != null) if (_mapsCollection.GetId(selected).remove(pos) != null)
{ {
alert = new Alert(Alert.AlertType.WARNING); alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("RemoveBus"); alert.setTitle("RemoveBus");
@ -136,30 +221,30 @@ public class ControllerMapWithSetBus {
private void ButtonShowStorage_Click(ActionEvent event) private void ButtonShowStorage_Click(ActionEvent event)
{ {
FirstIncome(); FirstIncome();
if (_mapBusesCollectionGeneric == null) if (selected == null)
{ {
return; return;
} }
gc.setFill(Color.WHITE); gc.setFill(Color.WHITE);
gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight()); gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight());
_mapBusesCollectionGeneric.ShowSet(gc); _mapsCollection.GetId(selected).ShowSet(gc);
} }
@FXML @FXML
private void ButtonShowOnMap_Click(ActionEvent event) { private void ButtonShowOnMap_Click(ActionEvent event) {
FirstIncome(); FirstIncome();
if (_mapBusesCollectionGeneric == null) { if (selected == null) {
return; return;
} }
gc.setFill(Color.WHITE); gc.setFill(Color.WHITE);
gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight()); gc.fillRect(0, 0, canvasBus.getWidth(), canvasBus.getHeight());
_mapBusesCollectionGeneric.ShowOnMap(gc); _mapsCollection.GetId(selected).ShowOnMap(gc);
} }
@FXML @FXML
private void ButtonMove_Click(ActionEvent event) private void ButtonMove_Click(ActionEvent event)
{ {
if (_mapBusesCollectionGeneric == null) if (selected == null)
{ {
return; return;
} }
@ -172,8 +257,11 @@ public class ControllerMapWithSetBus {
case "buttonLeft" -> dir = Direction.Left; case "buttonLeft" -> dir = Direction.Left;
case "buttonRight" -> dir = Direction.Right; case "buttonRight" -> dir = Direction.Right;
} }
_mapBusesCollectionGeneric.MoveObject(dir); _mapsCollection.GetId(selected).MoveObject(dir);
} }
} }

View File

@ -12,7 +12,7 @@ public class Form extends Application {
@Override @Override
public void start(Stage stage) throws IOException { 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()); Scene scene = new Scene(fxmlLoader.load());
myStage = stage; myStage = stage;
myStage.setTitle("DoubleDeckerBus"); myStage.setTitle("DoubleDeckerBus");

View File

@ -46,9 +46,8 @@ public class MapWithSetBusesGeneric<T extends IDrawingObject, U extends Abstract
public void ShowOnMap(GraphicsContext gc) public void ShowOnMap(GraphicsContext gc)
{ {
Shaking(); Shaking();
for (int i = 0; i < _setBuses.Count(); i++) for (var bus: _setBuses.GetBuses())
{ {
var bus = _setBuses.Get(i);
if (bus != null) if (bus != null)
{ {
_map.CreateMap(_pictureWidth, _pictureHeight, bus, gc); _map.CreateMap(_pictureWidth, _pictureHeight, bus, gc);
@ -111,15 +110,15 @@ public class MapWithSetBusesGeneric<T extends IDrawingObject, U extends Abstract
int currentWidth = width - 1; int currentWidth = width - 1;
int currentHeight = 0; int currentHeight = 0;
for (int i = 0; i < _setBuses.Count(); i++) { for (var bus: _setBuses.GetBuses()) {
if (_setBuses.Get(i) == null){ if (bus == null){
continue; continue;
} }
_setBuses.Get(i).SetObject(currentWidth * _placeSizeWidth, bus.SetObject(currentWidth * _placeSizeWidth,
currentHeight * _placeSizeHeight, currentHeight * _placeSizeHeight,
_pictureWidth, _pictureHeight); _pictureWidth, _pictureHeight);
_setBuses.Get(i).DrawingObject(gc); bus.DrawingObject(gc);
if (currentWidth > 0) if (currentWidth > 0)
currentWidth--; currentWidth--;

View File

@ -0,0 +1,49 @@
package com.example.doubledeckerbus;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.util.HashMap;
import java.util.List;
public class MapsCollection {
HashMap<String, MapWithSetBusesGeneric<DrawingObjectBus, AbstractMap>> _mapStorages;
public List<String> 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<DrawingObjectBus, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
public void DelMap(String name)
{
_mapStorages.remove(name);
}
public MapWithSetBusesGeneric<DrawingObjectBus, AbstractMap> GetId(String id)
{
return _mapStorages.get(id);
}
public ObservableList<String> toObserveList() {
ObservableList<String> result = FXCollections.observableArrayList();
result.addAll(_mapStorages.keySet());
return result;
}
}

View File

@ -1,15 +1,20 @@
package com.example.doubledeckerbus; package com.example.doubledeckerbus;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Objects; import java.util.Objects;
class SetBusesGeneric<T> { class SetBusesGeneric<T> {
private Object[] _places; private ArrayList<T> _places;
private int BusyPlaces = 0; private int BusyPlaces = 0;
private int _maxCount;
public SetBusesGeneric(int count) { public SetBusesGeneric(int count) {
_places = new Object[count]; _maxCount = count;
_places = new ArrayList<>();
} }
public int Insert(T bus) public int Insert(T bus)
@ -19,37 +24,42 @@ class SetBusesGeneric<T> {
public int Insert(T bus, int position) 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++; BusyPlaces++;
while (_places[position] != null) { _places.add(position, bus);
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;
return position; return position;
} }
public T Remove(int position) public T Remove(int position)
{ {
if (position < 0 || position >= _places.length) return null; if (position < 0 || position >= _maxCount) return null;
T savedBus = (T)_places[position]; T savedBus = _places.get(position - 1);
_places[position] = null; _places.set(position - 1, null);
return savedBus; return savedBus;
} }
public T Get(int position) public T Get(int position)
{ {
if (position < 0 || position >= _places.length) return null; if (position < 0 || position >= _maxCount) return null;
return (T)_places[position]; return _places.get(position);
} }
public ArrayList<T> GetBuses() {
ArrayList<T> result = new ArrayList<>();
for (var bus: _places) {
if (bus != null){
result.add(bus);
}
else {
break;
}
}
return result;
}
public int Count() { public int Count() {
return _places.length; return _places.size();
} }
} }

View File

@ -4,6 +4,7 @@
<?import javafx.scene.canvas.Canvas?> <?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?> <?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?> <?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?> <?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?> <?import javafx.scene.image.ImageView?>
@ -12,7 +13,7 @@
<?import javafx.scene.layout.GridPane?> <?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?> <?import javafx.scene.layout.RowConstraints?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="463.0" prefWidth="779.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.doubledeckerbus.ControllerMapWithSetBus"> <GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="745.0" prefWidth="819.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.doubledeckerbus.ControllerMapWithSetBus">
<columnConstraints> <columnConstraints>
<ColumnConstraints hgrow="ALWAYS" maxWidth="463.0" minWidth="10.0" percentWidth="70.0" prefWidth="423.0" /> <ColumnConstraints hgrow="ALWAYS" maxWidth="463.0" minWidth="10.0" percentWidth="70.0" prefWidth="423.0" />
<ColumnConstraints hgrow="NEVER" maxWidth="294.0" minWidth="10.0" percentWidth="30.0" prefWidth="177.0" /> <ColumnConstraints hgrow="NEVER" maxWidth="294.0" minWidth="10.0" percentWidth="30.0" prefWidth="177.0" />
@ -23,7 +24,7 @@
<children> <children>
<AnchorPane fx:id="pictureBoxBus" prefHeight="9.9999999E7" prefWidth="9.9999999E7"> <AnchorPane fx:id="pictureBoxBus" prefHeight="9.9999999E7" prefWidth="9.9999999E7">
<children> <children>
<Canvas fx:id="canvasBus" height="463.0" width="537.0" /> <Canvas fx:id="canvasBus" height="745.0" width="573.0" />
</children> </children>
</AnchorPane> </AnchorPane>
<Group GridPane.columnIndex="1" /> <Group GridPane.columnIndex="1" />
@ -34,6 +35,8 @@
<rowConstraints> <rowConstraints>
<RowConstraints maxHeight="120.0" minHeight="0.0" prefHeight="34.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="120.0" minHeight="0.0" prefHeight="34.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="200.0" minHeight="10.0" prefHeight="24.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="200.0" minHeight="10.0" prefHeight="24.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="200.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="200.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="200.0" minHeight="8.0" prefHeight="27.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="200.0" minHeight="8.0" prefHeight="27.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="269.0" minHeight="10.0" prefHeight="65.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="269.0" minHeight="10.0" prefHeight="65.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="250.0" minHeight="0.0" prefHeight="44.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="250.0" minHeight="0.0" prefHeight="44.0" vgrow="SOMETIMES" />
@ -43,12 +46,12 @@
<RowConstraints maxHeight="272.0" minHeight="10.0" prefHeight="134.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="272.0" minHeight="10.0" prefHeight="134.0" vgrow="SOMETIMES" />
</rowConstraints> </rowConstraints>
<children> <children>
<ChoiceBox fx:id="comboBoxSelectorMap" prefHeight="50.0" prefWidth="9.9999999E7" /> <ChoiceBox fx:id="comboBoxSelectorMap" prefHeight="50.0" prefWidth="9.9999999E7" GridPane.rowIndex="1" />
<Button fx:id="buttonAddBus" mnemonicParsing="false" onAction="#ButtonAddBus_Click" prefHeight="50.0" prefWidth="9.999999999E9" text="Добавить автобус" GridPane.rowIndex="2" /> <Button fx:id="buttonAddBus" mnemonicParsing="false" onAction="#ButtonAddBus_Click" prefHeight="50.0" prefWidth="9.999999999E9" text="Добавить автобус" GridPane.rowIndex="5" />
<TextField fx:id="textBoxPosition" GridPane.rowIndex="3" /> <TextField fx:id="textBoxPosition" GridPane.rowIndex="6" />
<Button fx:id="buttonRemoveCar" mnemonicParsing="false" onAction="#ButtonRemoveBus_Click" prefHeight="63.0" prefWidth="9.999999999E9" text="Удалить автобус" GridPane.rowIndex="4" /> <Button fx:id="buttonRemoveCar" mnemonicParsing="false" onAction="#ButtonRemoveBus_Click" prefHeight="63.0" prefWidth="9.999999999E9" text="Удалить автобус" GridPane.rowIndex="7" />
<Button fx:id="buttonShowMap" mnemonicParsing="false" onAction="#ButtonShowOnMap_Click" prefHeight="63.0" prefWidth="9.99999999999E11" text="Посмотреть карту" GridPane.rowIndex="7" /> <Button fx:id="buttonShowMap" mnemonicParsing="false" onAction="#ButtonShowOnMap_Click" prefHeight="63.0" prefWidth="9.99999999999E11" text="Посмотреть карту" GridPane.rowIndex="9" />
<AnchorPane prefHeight="157.0" prefWidth="332.0" GridPane.rowIndex="8"> <AnchorPane prefHeight="157.0" prefWidth="332.0" GridPane.rowIndex="10">
<children> <children>
<Button fx:id="buttonLeft" layoutX="38.0" layoutY="52.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#ButtonMove_Click" prefHeight="30.0" prefWidth="30.0" AnchorPane.bottomAnchor="52.0" AnchorPane.rightAnchor="111.0"> <Button fx:id="buttonLeft" layoutX="38.0" layoutY="52.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#ButtonMove_Click" prefHeight="30.0" prefWidth="30.0" AnchorPane.bottomAnchor="52.0" AnchorPane.rightAnchor="111.0">
<graphic> <graphic>
@ -88,7 +91,11 @@
</Button> </Button>
</children> </children>
</AnchorPane> </AnchorPane>
<Button fx:id="buttonShowStorage" mnemonicParsing="false" onAction="#ButtonShowStorage_Click" prefHeight="50.0" prefWidth="1.0E16" text="Посмотреть хранилище" GridPane.rowIndex="6" /> <Button fx:id="buttonShowStorage" mnemonicParsing="false" onAction="#ButtonShowStorage_Click" prefHeight="50.0" prefWidth="1.0E16" text="Посмотреть хранилище" GridPane.rowIndex="8" />
<TextField fx:id="TextFieldMap" />
<Button fx:id="Map" mnemonicParsing="false" onAction="#ButtonAddMap_Click" prefHeight="50.0" prefWidth="9.999999999E9" text="Добавить карту" GridPane.rowIndex="2" />
<ListView fx:id="listViewMaps" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="3" />
<Button fx:id="buttonDeleteMap" mnemonicParsing="false" onAction="#ButtonDeleteMap_Click" prefHeight="50.0" prefWidth="9.999999999E9" text="Удалить карту" GridPane.rowIndex="4" />
</children> </children>
</GridPane> </GridPane>
</children> </children>