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);
if (_mapBusesCollectionGeneric.add(bus) != -1) {
if (ControllerMapWithSetBus.AddNewBus(bus) != -1) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("SelectBus");
alert.setContentText("Вы создали объект");

View File

@ -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<DrawingObjectBus, AbstractMap> _mapBusesCollectionGeneric;
private HashMap<String, AbstractMap> _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<String> countOfMap = FXCollections.observableArrayList("Простая карта", "Водная карта");
@ -58,18 +74,32 @@ public class ControllerMapWithSetBus {
@FXML
private TextField textBoxPosition;
@FXML
private ListView<String> listViewMaps;
@FXML
private TextField TextFieldMap;
@FXML
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.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;
private void FirstIncome() {
if (_mapBusesCollectionGeneric == null) {
_mapBusesCollectionGeneric = new MapWithSetBusesGeneric<DrawingObjectBus, AbstractMap>(
(int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight(), map);
}
gc = canvasBus.getGraphicsContext2D();
if (comboBoxSelectorMap.getValue() != map_name) {
map_name = comboBoxSelectorMap.getValue();
@ -77,9 +107,31 @@ public class ControllerMapWithSetBus {
case "Простая карта" -> map = new SimpleMap();
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
private void ButtonAddBus_Click(ActionEvent event) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormBus.fxml"));
@ -90,6 +142,39 @@ 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<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
private void ButtonRemoveBus_Click(ActionEvent event)
@ -116,7 +201,7 @@ public class ControllerMapWithSetBus {
return;
}
if (_mapBusesCollectionGeneric.remove(pos) != null)
if (_mapsCollection.GetId(selected).remove(pos) != null)
{
alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("RemoveBus");
@ -136,30 +221,30 @@ public class ControllerMapWithSetBus {
private void ButtonShowStorage_Click(ActionEvent event)
{
FirstIncome();
if (_mapBusesCollectionGeneric == null)
if (selected == null)
{
return;
}
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight());
_mapBusesCollectionGeneric.ShowSet(gc);
_mapsCollection.GetId(selected).ShowSet(gc);
}
@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 +257,11 @@ public class ControllerMapWithSetBus {
case "buttonLeft" -> dir = Direction.Left;
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
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");

View File

@ -46,9 +46,8 @@ public class MapWithSetBusesGeneric<T extends IDrawingObject, U extends Abstract
public void ShowOnMap(GraphicsContext gc)
{
Shaking();
for (int i = 0; i < _setBuses.Count(); i++)
for (var bus: _setBuses.GetBuses())
{
var bus = _setBuses.Get(i);
if (bus != null)
{
_map.CreateMap(_pictureWidth, _pictureHeight, bus, gc);
@ -111,15 +110,15 @@ public class MapWithSetBusesGeneric<T extends IDrawingObject, U extends Abstract
int currentWidth = width - 1;
int currentHeight = 0;
for (int i = 0; i < _setBuses.Count(); i++) {
if (_setBuses.Get(i) == null){
for (var bus: _setBuses.GetBuses()) {
if (bus == null){
continue;
}
_setBuses.Get(i).SetObject(currentWidth * _placeSizeWidth,
bus.SetObject(currentWidth * _placeSizeWidth,
currentHeight * _placeSizeHeight,
_pictureWidth, _pictureHeight);
_setBuses.Get(i).DrawingObject(gc);
bus.DrawingObject(gc);
if (currentWidth > 0)
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;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
class SetBusesGeneric<T> {
private Object[] _places;
private ArrayList<T> _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,42 @@ class SetBusesGeneric<T> {
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<T> GetBuses() {
ArrayList<T> result = new ArrayList<>();
for (var bus: _places) {
if (bus != null){
result.add(bus);
}
else {
break;
}
}
return result;
}
public int Count() {
return _places.length;
return _places.size();
}
}

View File

@ -4,6 +4,7 @@
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
@ -12,7 +13,7 @@
<?import javafx.scene.layout.GridPane?>
<?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 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" />
@ -23,7 +24,7 @@
<children>
<AnchorPane fx:id="pictureBoxBus" prefHeight="9.9999999E7" prefWidth="9.9999999E7">
<children>
<Canvas fx:id="canvasBus" height="463.0" width="537.0" />
<Canvas fx:id="canvasBus" height="745.0" width="573.0" />
</children>
</AnchorPane>
<Group GridPane.columnIndex="1" />
@ -34,6 +35,8 @@
<rowConstraints>
<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="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="269.0" minHeight="10.0" prefHeight="65.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>
<children>
<ChoiceBox fx:id="comboBoxSelectorMap" prefHeight="50.0" prefWidth="9.9999999E7" />
<Button fx:id="buttonAddBus" mnemonicParsing="false" onAction="#ButtonAddBus_Click" prefHeight="50.0" prefWidth="9.999999999E9" text="Добавить автобус" GridPane.rowIndex="2" />
<TextField fx:id="textBoxPosition" GridPane.rowIndex="3" />
<Button fx:id="buttonRemoveCar" mnemonicParsing="false" onAction="#ButtonRemoveBus_Click" prefHeight="63.0" prefWidth="9.999999999E9" text="Удалить автобус" GridPane.rowIndex="4" />
<Button fx:id="buttonShowMap" mnemonicParsing="false" onAction="#ButtonShowOnMap_Click" prefHeight="63.0" prefWidth="9.99999999999E11" text="Посмотреть карту" GridPane.rowIndex="7" />
<AnchorPane prefHeight="157.0" prefWidth="332.0" GridPane.rowIndex="8">
<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="5" />
<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="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="10">
<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">
<graphic>
@ -88,7 +91,11 @@
</Button>
</children>
</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>
</GridPane>
</children>