Origin from base

This commit is contained in:
Mark Zaharchenko 2022-09-11 22:28:57 +04:00
parent e563700cc4
commit 59b1e80ad4
13 changed files with 452 additions and 0 deletions

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

86
DoubleDeckerBus/pom.xml Normal file
View File

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>DoubleDeckerBus</artifactId>
<version>1.0-SNAPSHOT</version>
<name>DoubleDeckerBus</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.8.2</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>18</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>18</version>
</dependency>
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>11.1.1</version>
</dependency>
<dependency>
<groupId>org.kordamp.bootstrapfx</groupId>
<artifactId>bootstrapfx-core</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>18</source>
<target>18</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<executions>
<execution>
<!-- Default configuration for running with: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>com.example.doubledeckerbus/com.example.doubledeckerbus.FormBus
</mainClass>
<launcher>app</launcher>
<jlinkZipName>app</jlinkZipName>
<jlinkImageName>app</jlinkImageName>
<noManPages>true</noManPages>
<stripDebug>true</stripDebug>
<noHeaderFiles>true</noHeaderFiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -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();
}
}

View File

@ -0,0 +1,5 @@
package com.example.doubledeckerbus;
public enum Direction {
Up, Down, Left, Right
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane alignment="CENTER" prefHeight="691.0" prefWidth="1041.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.doubledeckerbus.ControllerBus">
<columnConstraints>
<ColumnConstraints hgrow="ALWAYS" minWidth="10.0" percentWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints fillHeight="false" maxHeight="1.7976931348623157E308" percentHeight="90.0" vgrow="ALWAYS" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="-Infinity" percentHeight="10.0" prefHeight="31.0" vgrow="NEVER" />
</rowConstraints>
<children>
<HBox alignment="CENTER_LEFT" prefHeight="57.0" prefWidth="777.0" spacing="20.0" GridPane.rowIndex="1">
<children>
<Label fx:id="statusSpeed" text="Скорость:" />
<Label fx:id="statusWeight" text="Вес:" />
<Label fx:id="statusColor" text="Цвет:" />
</children>
</HBox>
<AnchorPane fx:id="pictureBoxBus" maxHeight="2000000.0" maxWidth="2000000.0" minHeight="0.0" minWidth="0.0" prefHeight="603.0" prefWidth="1020.0">
<children>
<Canvas fx:id="canvasBus" layoutY="38.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" />
<Button fx:id="buttonLeft" layoutX="975.0" layoutY="427.0" mnemonicParsing="false" onAction="#ButtonMove_Click" prefHeight="0.0" prefWidth="0.0" AnchorPane.bottomAnchor="40.0" AnchorPane.rightAnchor="60.0">
<graphic>
<ImageView fitHeight="27.0" fitWidth="10.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@images/LeftArrow.png" />
</image>
</ImageView>
</graphic></Button>
<Button fx:id="buttonDown" layoutX="1005.0" layoutY="457.0" mnemonicParsing="false" onAction="#ButtonMove_Click" prefHeight="0.0" prefWidth="0.0" AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="30.0">
<graphic>
<ImageView fitHeight="27.0" fitWidth="10.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@images/DownArrow.png" />
</image>
</ImageView>
</graphic></Button>
<Button fx:id="buttonRight" layoutX="1035.0" layoutY="427.0" mnemonicParsing="false" onAction="#ButtonMove_Click" prefHeight="0.0" prefWidth="0.0" AnchorPane.bottomAnchor="40.0" AnchorPane.rightAnchor="0.0">
<graphic>
<ImageView fitHeight="27.0" fitWidth="10.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@images/RightArrow.png" />
</image>
</ImageView>
</graphic></Button>
<Button fx:id="buttonUp" layoutX="1005.0" layoutY="397.0" mnemonicParsing="false" onAction="#ButtonMove_Click" prefHeight="0.0" prefWidth="0.0" AnchorPane.bottomAnchor="70.0" AnchorPane.rightAnchor="30.0">
<graphic>
<ImageView fitHeight="27.0" fitWidth="10.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@images/UpArrow.png" />
</image>
</ImageView>
</graphic></Button>
<Button fx:id="buttonCreate" layoutY="455.0" mnemonicParsing="false" onAction="#ButtonCreate_Click" text="Создать" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="0.0" />
</children>
</AnchorPane>
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</GridPane>

Binary file not shown.

After

Width:  |  Height:  |  Size: 414 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 439 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 377 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 439 B