diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DoubleDeckerBus/pom.xml b/DoubleDeckerBus/pom.xml new file mode 100644 index 0000000..effcc0d --- /dev/null +++ b/DoubleDeckerBus/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + + com.example + DoubleDeckerBus + 1.0-SNAPSHOT + DoubleDeckerBus + + + UTF-8 + 5.8.2 + + + + + org.openjfx + javafx-controls + 18 + + + org.openjfx + javafx-fxml + 18 + + + org.controlsfx + controlsfx + 11.1.1 + + + org.kordamp.bootstrapfx + bootstrapfx-core + 0.4.0 + + + org.junit.jupiter + junit-jupiter-api + ${junit.version} + test + + + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + 18 + 18 + + + + org.openjfx + javafx-maven-plugin + 0.0.8 + + + + default-cli + + com.example.doubledeckerbus/com.example.doubledeckerbus.FormBus + + app + app + app + true + true + true + + + + + + + \ No newline at end of file diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java new file mode 100644 index 0000000..561e009 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/ControllerBus.java @@ -0,0 +1,92 @@ +package com.example.doubledeckerbus; + +import javafx.beans.InvalidationListener; +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.Label; +import javafx.scene.layout.AnchorPane; +import javafx.scene.paint.Color; + +import java.util.Random; + +public class ControllerBus { + + private DrawingBus _bus; + @FXML + private Button buttonCreate; + + @FXML + private Button buttonDown; + + @FXML + private Button buttonLeft; + + @FXML + private Button buttonRight; + + @FXML + private Button buttonUp; + + @FXML + private Canvas canvasBus; + + @FXML + private AnchorPane pictureBoxBus; + + @FXML + private Label statusColor; + + @FXML + private Label statusSpeed; + + @FXML + private Label statusWeight; + + InvalidationListener listener = o -> BorderChanged(); + + private void Draw() + { + GraphicsContext gc = canvasBus.getGraphicsContext2D(); + _bus.DrawTransport(gc); + } + + @FXML + void ButtonCreate_Click(ActionEvent event) { + pictureBoxBus.widthProperty().addListener(listener); + pictureBoxBus.heightProperty().addListener(listener); + + 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))); + _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)); + BorderChanged(); + } + + @FXML + void ButtonMove_Click(ActionEvent event) { + if (_bus == null) return; + String name = ((Button) event.getSource()).getId(); + switch (name) { + case "buttonUp" -> _bus.MoveTransport(Direction.Up); + case "buttonDown" -> _bus.MoveTransport(Direction.Down); + case "buttonLeft" -> _bus.MoveTransport(Direction.Left); + case "buttonRight" -> _bus.MoveTransport(Direction.Right); + } + Draw(); + } + + void BorderChanged() { + canvasBus.setWidth(pictureBoxBus.getWidth()); + canvasBus.setHeight(pictureBoxBus.getHeight()); + _bus.ChangeBorders((int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight()); + Draw(); + } + +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Direction.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Direction.java new file mode 100644 index 0000000..7449ed2 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/Direction.java @@ -0,0 +1,5 @@ +package com.example.doubledeckerbus; + +public enum Direction { + Up, Down, Left, Right +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java new file mode 100644 index 0000000..9c62b37 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/DrawingBus.java @@ -0,0 +1,119 @@ +package com.example.doubledeckerbus; + +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class DrawingBus { + public EntityBus Bus; + + public EntityBus getBus() { + return Bus; + } + + private static final int _null = -1000; + private float _startPosX; + private float _startPosY; + private int _pictureWidth; + private int _pictureHeight; + private static final int _busWidth = 100; + private static final int _busHeight = 50; + + public void Init(int speed, float weight, Color bodyColor) { + Bus = new EntityBus(); + Bus.Init(speed, weight, bodyColor); + } + + public void SetPosition(int x, int y, int width, int height) { + if (_pictureWidth <= _busWidth || _pictureHeight <= _busHeight) { + _pictureWidth = _null; + _pictureHeight = _null; + return; + } + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + + public void MoveTransport(Direction direction) + { + if (_pictureWidth == _null || _pictureHeight == _null) + { + return; + } + + switch (direction) + { + case Right: + if (_startPosX + _busWidth + Bus.Step < _pictureWidth) { + _startPosX += Bus.Step; + } + break; + case Left: + if (_startPosX - Bus.Step >= 0) { + _startPosX -= Bus.Step; + } + break; + case Up: + if (_startPosY - Bus.Step >= 0) { + _startPosY -= Bus.Step; + } + break; + case Down: + if (_startPosY + _busHeight + Bus.Step < _pictureHeight) { + _startPosY += Bus.Step; + } + break; + default: + break; + } + } + + public void DrawTransport(GraphicsContext gc) + { + if (_startPosX < 0 || _startPosY < 0 + || _pictureHeight == _null || _pictureWidth == _null) + { + return; + } + + gc.clearRect(0, 0, _pictureWidth, _pictureHeight); + gc.setFill(Bus.BodyColor); + gc.fillRect(_startPosX, _startPosY + 10, 100, 30); + + //Дверь + gc.setFill(Color.BLACK); + gc.fillRect(_startPosX + 30, _startPosY + 20, 10, 20); + + //Колеса + gc.fillOval(_startPosX + 7, _startPosY + 35, 10, 10); + gc.fillOval(_startPosX + 77, _startPosY + 35, 10, 10); + + //окна + gc.setFill(Color.BLUE); + gc.fillOval(_startPosX + 10, _startPosY + 15, 10, 15); + gc.fillOval(_startPosX + 50, _startPosY + 15, 10, 15); + gc.fillOval(_startPosX + 70, _startPosY + 15, 10, 15); + gc.fillOval(_startPosX + 90, _startPosY + 15, 10, 15); + + } + + public void ChangeBorders(int width, int height) { + _pictureWidth = width; + _pictureHeight = height; + + if (_pictureWidth <= _busWidth || _pictureHeight <= _busHeight){ + _pictureWidth = _null; + _pictureHeight = _null; + return; + } + + if (_startPosX + _busWidth > _pictureWidth) { + _startPosX = _pictureWidth - _busWidth; + + } + if (_startPosY + _busHeight > _pictureHeight) { + _startPosY = _pictureHeight - _busHeight; + } + } +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/EntityBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/EntityBus.java new file mode 100644 index 0000000..832cff3 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/EntityBus.java @@ -0,0 +1,38 @@ +package com.example.doubledeckerbus; + +import javafx.scene.paint.Color; + +import java.util.Random; + +public class EntityBus { + public int Speed; + public float Weight; + public Color BodyColor; + public float Step; + + public void Init(int speed, float weight, Color bodyColor) + { + Random rnd = new Random(); + Speed = (speed <= 0) ? rnd.nextInt(50, 150): speed; + Weight = (weight <= 0) ? rnd.nextFloat(50, 70) : weight; + BodyColor = bodyColor; + Step = Speed * 100 / Weight; + } + + public int getSpeed() { + return Speed; + } + + public float getWeight() { + return Weight; + } + + public Color getBodyColor() { + return BodyColor; + } + + public float getStep() { + return Step; + } + +} diff --git a/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/FormBus.java b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/FormBus.java new file mode 100644 index 0000000..1b9206a --- /dev/null +++ b/DoubleDeckerBus/src/main/java/com/example/doubledeckerbus/FormBus.java @@ -0,0 +1,23 @@ +package com.example.doubledeckerbus; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.stage.Stage; + +import java.io.IOException; + +public class FormBus extends Application { + @Override + 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.setScene(scene); + stage.show(); + } + + public static void main(String[] args) { + launch(); + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/src/main/java/module-info.java b/DoubleDeckerBus/src/main/java/module-info.java new file mode 100644 index 0000000..3e43a04 --- /dev/null +++ b/DoubleDeckerBus/src/main/java/module-info.java @@ -0,0 +1,10 @@ +module com.example.doubledeckerbus { + requires javafx.controls; + requires javafx.fxml; + + requires org.controlsfx.controls; + requires org.kordamp.bootstrapfx.core; + + opens com.example.doubledeckerbus to javafx.fxml; + exports com.example.doubledeckerbus; +} \ No newline at end of file diff --git a/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/hello-view.fxml b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/hello-view.fxml new file mode 100644 index 0000000..72ce5be --- /dev/null +++ b/DoubleDeckerBus/src/main/resources/com/example/doubledeckerbus/hello-view.fxml @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +