diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java index 561e009..ada3280 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java @@ -1,11 +1,14 @@ 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.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.control.Button; +import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; @@ -15,6 +18,7 @@ import java.util.Random; public class ControllerBus { private DrawingBus _bus; + ObservableList countOfDoors = FXCollections.observableArrayList(3, 4, 5); @FXML private Button buttonCreate; @@ -45,13 +49,8 @@ public class ControllerBus { @FXML private Label statusWeight; - InvalidationListener listener = o -> BorderChanged(); - - private void Draw() - { - GraphicsContext gc = canvasBus.getGraphicsContext2D(); - _bus.DrawTransport(gc); - } + @FXML + private ChoiceBox choiceDoors; @FXML void ButtonCreate_Click(ActionEvent event) { @@ -61,7 +60,7 @@ public class ControllerBus { Random rnd = new Random(); _bus = new DrawingBus(); _bus.Init(rnd.nextInt(100, 300), rnd.nextFloat(1000, 2000), - Color.rgb(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))); + Color.rgb(rnd.nextInt(0, 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()); statusSpeed.setText("Скорость: %s".formatted(_bus.Bus.Speed)); statusWeight.setText("Вес: %s".formatted(_bus.Bus.Weight)); @@ -82,6 +81,20 @@ public class ControllerBus { Draw(); } + @FXML + public void initialize() { + choiceDoors.setItems(countOfDoors); + choiceDoors.setValue(3); + } + + InvalidationListener listener = o -> BorderChanged(); + + private void Draw() + { + GraphicsContext gc = canvasBus.getGraphicsContext2D(); + _bus.DrawTransport(gc); + } + void BorderChanged() { canvasBus.setWidth(pictureBoxBus.getWidth()); canvasBus.setHeight(pictureBoxBus.getHeight()); diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/CountOfDoors.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/CountOfDoors.java new file mode 100644 index 0000000..a6fd2b2 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/CountOfDoors.java @@ -0,0 +1,18 @@ +package com.example.doubledeckerbus; + +public enum CountOfDoors { + + ThreeDoors(3), + FourDoors(4), + FiveDoors(5); + + private final int id; + CountOfDoors(int value){ + id = value; + } + + public int getId() { + return id; + } + +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Direction.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Direction.java index 7449ed2..66ea455 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Direction.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Direction.java @@ -1,5 +1,11 @@ package com.example.doubledeckerbus; public enum Direction { - Up, Down, Left, Right + Up(1), + Down(2), + Left(3), + Right(4); + Direction(int value){} + } + diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java index 9c62b37..29f30a7 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java @@ -6,6 +6,8 @@ import javafx.scene.paint.Color; public class DrawingBus { public EntityBus Bus; + public DrawingDoors Doors; + public EntityBus getBus() { return Bus; } @@ -18,9 +20,11 @@ public class DrawingBus { private static final int _busWidth = 100; private static final int _busHeight = 50; - public void Init(int speed, float weight, Color bodyColor) { + public void Init(int speed, float weight, Color bodyColor, int countOfDoors) { Bus = new EntityBus(); Bus.Init(speed, weight, bodyColor); + Doors = new DrawingDoors(); + Doors.setCountOfDoors(countOfDoors); } public void SetPosition(int x, int y, int width, int height) { @@ -81,11 +85,8 @@ public class DrawingBus { gc.setFill(Bus.BodyColor); gc.fillRect(_startPosX, _startPosY + 10, 100, 30); - //Дверь - gc.setFill(Color.BLACK); - gc.fillRect(_startPosX + 30, _startPosY + 20, 10, 20); - //Колеса + gc.setFill(Color.BLACK); gc.fillOval(_startPosX + 7, _startPosY + 35, 10, 10); gc.fillOval(_startPosX + 77, _startPosY + 35, 10, 10); @@ -96,6 +97,8 @@ public class DrawingBus { gc.fillOval(_startPosX + 70, _startPosY + 15, 10, 15); gc.fillOval(_startPosX + 90, _startPosY + 15, 10, 15); + //Дверь + Doors.DrawDoors(gc, (int)_startPosX, (int)_startPosY); } public void ChangeBorders(int width, int height) { diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDoors.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDoors.java new file mode 100644 index 0000000..7c902d7 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingDoors.java @@ -0,0 +1,31 @@ +package com.example.doubledeckerbus; + +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class DrawingDoors { + private CountOfDoors _countOfDoors; + + public void DrawDoors(GraphicsContext gc, int _startPosX, int _startPosY) { + gc.setFill(Color.BLACK); + gc.fillRect(_startPosX, _startPosY + 20, 10, 20); + gc.fillRect(_startPosX + 20, _startPosY + 20, 10, 20); + gc.fillRect(_startPosX + 40, _startPosY + 20, 10, 20); + if (_countOfDoors.getId() >= 4) { + gc.fillRect(_startPosX + 60, _startPosY + 20, 10, 20); + } + if (_countOfDoors.getId() >= 5) { + gc.fillRect(_startPosX + 80, _startPosY + 20, 10, 20); + } + } + + public void setCountOfDoors(int number) { + for (CountOfDoors item: CountOfDoors.values()) { + if (item.getId() == number) { + _countOfDoors = item; + return; + } + } + + } +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/FormBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/FormBus.java index 1b9206a..56cb9d8 100644 --- a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/FormBus.java +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/FormBus.java @@ -12,7 +12,7 @@ public class FormBus extends Application { public void start(Stage stage) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(FormBus.class.getResource("hello-view.fxml")); Scene scene = new Scene(fxmlLoader.load()); - stage.setTitle("Hello!"); + stage.setTitle("DoubleDeckerBus"); stage.setScene(scene); stage.show(); } diff --git a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/hello-view.fxml b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/hello-view.fxml index 72ce5be..cb431c8 100644 --- a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/hello-view.fxml +++ b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/hello-view.fxml @@ -3,6 +3,7 @@ + @@ -26,6 +27,8 @@