diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java index 07b4d56..3dc9d8f 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java @@ -121,8 +121,6 @@ public class ControllerBus { pictureBoxBus.widthProperty().addListener(listener); pictureBoxBus.heightProperty().addListener(listener); Random rnd = new Random(); - ColorPicker colorPicker = new ColorPicker(); - ObservableList a = colorPicker.getCustomColors(); _bus = new DrawingDDB(rnd.nextInt(300), rnd.nextInt(2000), Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), choiceDoors.getValue(), diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerPolymorph.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerPolymorph.java new file mode 100644 index 0000000..6ecb833 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerPolymorph.java @@ -0,0 +1,165 @@ +package com.example.doubledeckerbus; + +import javafx.beans.InvalidationListener; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.control.*; +import javafx.scene.layout.AnchorPane; +import javafx.scene.paint.Color; + +import java.io.IOException; +import java.util.Optional; +import java.util.Random; + +import static com.example.doubledeckerbus.ControllerMapWithSetBus._mapBusesCollectionGeneric; + +public class ControllerPolymorph { + protected DrawingBus _bus; + protected DrawingPolymorphBus polymorphBus; + public DrawingBus SelectedBus; + protected ObservableList countOfDoors = FXCollections.observableArrayList(3, 4, 5); + + @FXML + protected Button buttonCreate; + + @FXML + protected Canvas canvasBus; + + @FXML + protected AnchorPane pictureBoxBus; + + @FXML + protected Label statusColor; + + @FXML + protected Label statusSpeed; + + @FXML + protected Label statusWeight; + + @FXML + protected ChoiceBox choiceDoors; + + @FXML + void ButtonCreate_Click(ActionEvent event) { + pictureBoxBus.widthProperty().addListener(listener); + pictureBoxBus.heightProperty().addListener(listener); + + Random rnd = new Random(); + IDrawingDoors door = null; + switch (rnd.nextInt(3)) { + case (0) -> door = new DrawingDoors(); + case (1) -> door = new DrawingEllipsoidDoors(); + case (2) -> door = new DrawingTriangleDoors(); + } + door.setCountOfDoors(choiceDoors.getValue()); + + EntityBus bus = new EntityBus(rnd.nextInt(300), rnd.nextInt(2000), + Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + + polymorphBus.AddDoors(door); + polymorphBus.AddEntity(bus); + + _bus = polymorphBus.CreateBus(); + SetData(); + BorderChanged(); + } + + void SetData() { + Random rnd = new Random(); + _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)); + } + + @FXML + public void initialize() { + polymorphBus = new DrawingPolymorphBus<>(100, 100); + choiceDoors.setItems(countOfDoors); + choiceDoors.setValue(3); + + } + InvalidationListener listener = o -> BorderChanged(); + + private void Draw() + { + GraphicsContext gc = canvasBus.getGraphicsContext2D(); + gc.setFill(Color.WHITE); + gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight()); + _bus.DrawTransport(gc); + } + + void BorderChanged() { + canvasBus.setWidth(pictureBoxBus.getWidth()); + canvasBus.setHeight(pictureBoxBus.getHeight()); + _bus.ChangeBorders((int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight()); + Draw(); + } + + @FXML + private void ButtonCreateExtra_Click(ActionEvent event) { + pictureBoxBus.widthProperty().addListener(listener); + pictureBoxBus.heightProperty().addListener(listener); + Random rnd = new Random(); + IDrawingDoors door = null; + switch (rnd.nextInt(3)) { + case (0) -> door = new DrawingDoors(); + case (1) -> door = new DrawingEllipsoidDoors(); + case (2) -> door = new DrawingTriangleDoors(); + } + door.setCountOfDoors(choiceDoors.getValue()); + + EntityDDB ddb_bus = new EntityDDB(rnd.nextInt(300), rnd.nextInt(2000), + Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextBoolean(), rnd.nextBoolean()); + + polymorphBus.AddDoors(door); + polymorphBus.AddEntity(ddb_bus); + + _bus = polymorphBus.CreateBus(); + SetData(); + BorderChanged(); + } + @FXML + private void ButtonSelectBus_Click(ActionEvent event) throws IOException { + SelectedBus = _bus; + + if (SelectedBus == null) { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("SelectBus"); + alert.setContentText("Вы не создали объект"); + Optional option = alert.showAndWait(); + } + else { + + DrawingObjectBus bus = new DrawingObjectBus(SelectedBus); + + if (_mapBusesCollectionGeneric.add(bus) != -1) { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("SelectBus"); + alert.setContentText("Вы создали объект"); + Optional option = alert.showAndWait(); + } else { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("SelectBus"); + alert.setContentText("Не удалось добавить объект"); + Optional option = alert.showAndWait(); + } + } + + 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/DrawingBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java index cb372b3..d5f3158 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java @@ -30,7 +30,15 @@ public class DrawingBus { case 2 -> Doors = new DrawingEllipsoidDoors(); } Doors.setCountOfDoors(countOfDoors); + } + public DrawingBus(int speed, float weight, Color bodyColor) { + Bus = new EntityBus(speed, weight, bodyColor); + } + + public DrawingBus(EntityBus bus, IDrawingDoors doors) { + Bus = bus; + Doors = doors; } public void SetPosition(int x, int y, int width, int height) { diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java index 8b9c17f..0c76b40 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDDB.java @@ -9,6 +9,15 @@ public class DrawingDDB extends DrawingBus{ Bus = new EntityDDB(speed, weight, bodyColor, extraColor, ladder, secondStage); } + public DrawingDDB(int speed, float weight, Color bodyColor, Color extraColor, boolean ladder, boolean secondStage) { + super(speed, weight, bodyColor); + Bus = new EntityDDB(speed, weight, bodyColor, extraColor, ladder, secondStage); + } + + public DrawingDDB(EntityBus bus, IDrawingDoors doors) { + super(bus, doors); + } + @Override public void DrawTransport(GraphicsContext gc) { diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingPolymorphBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingPolymorphBus.java new file mode 100644 index 0000000..9dc1725 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingPolymorphBus.java @@ -0,0 +1,45 @@ +package com.example.doubledeckerbus; + +import java.util.Random; + +public class DrawingPolymorphBus { + Object[] buses; + Object[] doors; + + int busesCount = 0; + int doorsCount = 0; + + public DrawingPolymorphBus(int busesCount, int doorsCount) { + buses = new Object[busesCount]; + doors = new Object[doorsCount]; + } + + public int AddEntity(T boat) + { + if(busesCount < buses.length){ + buses[busesCount] = boat; + return busesCount++; + } + return -1; + } + + public int AddDoors(U paddle) + { + if(doorsCount < doors.length){ + doors[doorsCount]=paddle; + return doorsCount++; + } + return -1; + } + + public DrawingBus CreateBus() { + int idBus = new Random().nextInt(busesCount); + int idDoor = new Random().nextInt(doorsCount); + T selectedBus = (T)buses[idBus]; + U selectedDoors = (U)doors[idDoor]; + if (selectedBus instanceof EntityDDB) { + return new DrawingDDB(selectedBus, selectedDoors); + } + return new DrawingBus(selectedBus, selectedDoors); + } +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Form.java index eb0b4d1..7372e67 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("FormMapWithSetBus.fxml")); + FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormPolymorph.fxml")); Scene scene = new Scene(fxmlLoader.load()); myStage = stage; myStage.setTitle("DoubleDeckerBus"); diff --git a/DoubleDeckerBus/src/main/java/module-info.java b/DoubleDeckerBus/src/main/java/module-info.java index 3e43a04..4039eb7 100644 --- a/DoubleDeckerBus/src/main/java/module-info.java +++ b/DoubleDeckerBus/src/main/java/module-info.java @@ -4,6 +4,7 @@ module com.example.doubledeckerbus { requires org.controlsfx.controls; requires org.kordamp.bootstrapfx.core; + requires javafx.graphics; opens com.example.doubledeckerbus to javafx.fxml; exports com.example.doubledeckerbus; diff --git a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormPolymorph.fxml b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormPolymorph.fxml new file mode 100644 index 0000000..b5059d8 --- /dev/null +++ b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/FormPolymorph.fxml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +