Compare commits

...

3 Commits

Author SHA1 Message Date
ityurner02@mail.ru
970a1b77b3 Добавление деталей корпуса 2022-11-04 20:27:50 +04:00
ityurner02@mail.ru
025940e303 Избавление от лишнего класса 2022-11-04 20:21:06 +04:00
ityurner02@mail.ru
0f2092b72c Первая усложненная лабораторная работа 2022-11-04 20:16:30 +04:00
13 changed files with 594 additions and 37 deletions

View File

@ -0,0 +1,27 @@
package com.example.antiaircraftgun;
public enum Direction {
Up,
Down,
Left,
Right;
public static Direction FromInteger(int intValue)
{
switch(intValue)
{
case 1:
return Up;
case 2:
return Down;
case 3:
return Left;
case 4:
return Right;
default:
System.out.println("Error: incorrect value!");
return Up;
}
}
}

View File

@ -0,0 +1,133 @@
package com.example.antiaircraftgun;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class DrawingArmoredCar
{
private EntityArmoredCar _armoredCar;
private DrawingRollers _drawingRollers;
private float _startPosX;
private float _startPosY;
private Integer _pictureWidth;
private Integer _pictureHeight;
private int _armoredCarWidth = 90;
private int _armoredCarHeight = 40;
public EntityArmoredCar GetArmoredCar()
{
return _armoredCar;
}
public DrawingRollers GetDrawningRollers()
{
return _drawingRollers;
}
public void Init(int speed, float weight, Color bodyColor)
{
_armoredCar = new EntityArmoredCar();
_armoredCar.Init(speed, weight, bodyColor);
_drawingRollers = new DrawingRollers();
_drawingRollers.Init(bodyColor);
}
public void SetPosition(int x, int y, int width, int height)
{
if (x < 0 || y < 0) {
return;
}
if (x + _armoredCarWidth > width || y + _armoredCarHeight > height) {
return;
}
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
public void MoveTransport(Direction direction)
{
if (_pictureWidth == null || _pictureHeight == null) {
return;
}
switch (direction) {
case Up:
if (_startPosY - _armoredCar.GetStep() > 0) {
_startPosY -= _armoredCar.GetStep();
}
break;
case Down:
if (_startPosY + _armoredCarHeight + _armoredCar.GetStep() < _pictureHeight) {
_startPosY += _armoredCar.GetStep();
}
break;
case Left:
if (_startPosX - _armoredCar.GetStep() > 0) {
_startPosX -= _armoredCar.GetStep();
}
break;
case Right:
if (_startPosX + _armoredCarWidth + _armoredCar.GetStep() < _pictureWidth) {
_startPosX += _armoredCar.GetStep();
}
break;
default:
break;
}
}
public void DrawTransport(GraphicsContext gc)
{
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
return;
}
// Гусеницы
gc.setFill(Color.GRAY);
gc.fillRect(_startPosX + 10, _startPosY + 20, 70, 20);
gc.fillOval(_startPosX, _startPosY + 20, 20, 20);
gc.fillOval(_startPosX + 70, _startPosY + 20, 20, 20);
_drawingRollers.DrawRollers(gc, _startPosX, _startPosY);
// Броня
gc.setStroke(Color.BLACK);
gc.setLineWidth(2);
gc.setFill(_armoredCar.GetBodyColor());
gc.fillRect(_startPosX + 25, _startPosY, 40, 10);
gc.fillRect(_startPosX + 5, _startPosY + 10, 80, 10);
gc.strokeRect(_startPosX + 25, _startPosY, 40, 10);
gc.strokeRect(_startPosX + 5, _startPosY + 10, 80, 10);
gc.fillOval(_startPosX + 27, _startPosY + 16, 8, 8);
gc.fillOval(_startPosX + 40, _startPosY + 16, 8, 8);
gc.fillOval(_startPosX + 53, _startPosY + 16, 8, 8);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _armoredCarWidth || _pictureHeight <= _armoredCarHeight) {
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _armoredCarWidth > _pictureWidth) {
_startPosX = _pictureWidth - _armoredCarWidth;
}
if (_startPosY + _armoredCarHeight > _pictureHeight) {
_startPosY = _pictureHeight - _armoredCarHeight;
}
}
}

View File

@ -0,0 +1,58 @@
package com.example.antiaircraftgun;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class DrawingRollers {
private Color _RollersColor;
private NumberRollers _numRollers;
public void SetNumberRollers(int numberRollers)
{
_numRollers = NumberRollers.FromInteger(numberRollers);
}
public void Init(Color trackRollersColor)
{
_RollersColor = trackRollersColor;
}
public void DrawRollers(GraphicsContext gc, float startPosX, float startPosY) {
if (_RollersColor == null) {
return;
}
gc.setFill(_RollersColor);
switch (_numRollers) {
case Four:
DrawRoller1(gc, startPosX, startPosY);
break;
case Five:
DrawRoller1(gc, startPosX, startPosY);
DrawRoller2(gc, startPosX, startPosY);
break;
case Six:
DrawRoller1(gc, startPosX, startPosY);
DrawRoller2(gc, startPosX, startPosY);
DrawRoller3(gc, startPosX, startPosY);
break;
}
}
void DrawRoller1(GraphicsContext gc, float startPosX, float startPosY) {
gc.setFill(_RollersColor);
gc.fillOval(startPosX +1, startPosY + 21, 18, 18);
gc.fillOval(startPosX + 69, startPosY + 21, 18, 18);
gc.fillOval(startPosX + 19, startPosY + 30, 11, 11);
gc.fillOval(startPosX + 58, startPosY + 30, 11, 11);
}
void DrawRoller2(GraphicsContext gc, float startPosX, float startPosY) {
gc.setFill(_RollersColor);
gc.fillOval(startPosX + 45, startPosY + 30, 11, 11);
}
void DrawRoller3(GraphicsContext gc, float startPosX, float startPosY) {
gc.setFill(_RollersColor);
gc.fillOval(startPosX + 32, startPosY + 30, 11, 11);
}
}

View File

@ -0,0 +1,38 @@
package com.example.antiaircraftgun;
import javafx.scene.paint.Color;
import java.util.Random;
public class EntityArmoredCar
{
private int _speed;
private float _weight;
private Color _bodyColor;
public int GetSpeed()
{
return _speed;
}
public float GetWeight()
{
return _weight;
}
public Color GetBodyColor()
{
return _bodyColor;
}
public float GetStep()
{
return _speed * 100 / _weight;
}
public void Init(int speed, float weight, Color bodyColor)
{
Random random = new Random();
_speed = speed <= 0 ? random.nextInt(100) + 50 : speed;
_weight = weight <= 0 ? random.nextInt(30) + 40 : weight;
_bodyColor = bodyColor;
}
}

View File

@ -0,0 +1,305 @@
package com.example.antiaircraftgun;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.Random;
public class FormArmoredCar extends Application
{
private Pane _root;
private Scene _scene;
private Canvas _canvas;
private HBox _hBox;
private Button _buttonCreate;
private Button _buttonUp;
private Button _buttonDown;
private Button _buttonLeft;
private Button _buttonRight;
private Label _labelSpeedValue;
private Label _labelWeightValue;
private Label _labelBodyColorValue;
private ComboBox<String> _comboBoxNumRollers;
private double _hBoxHeight;
private double _buttonCreateHeight;
private final int _buttonMoveWidth = 30;
private final int _buttonMoveHeight = 30;
private final int _distanceBetweenMoveButtons = 5;
private final double _buttonMargin = 10.0;
private DrawingArmoredCar _armoredCar;
@Override
public void start(Stage stage) throws IOException
{
FXMLLoader fxmlLoader = new FXMLLoader(FormArmoredCar.class.getResource("hello-view.fxml"));
_scene = new Scene(fxmlLoader.load(), 600, 400);
_root = (Pane) _scene.lookup("#root");
_root.setStyle("-fx-background-color: #31374c;");
_canvas = new Canvas();
_root.getChildren().add(_canvas);
InitHBox();
InitButtonCreate();
InitMoveButtons();
stage.setTitle("AntiAircraftGun");
stage.setScene(_scene);
_scene.widthProperty().addListener((obs, oldVal, newVal) -> {
UpdateGUI();
if (_armoredCar != null)
{
_armoredCar.ChangeBorders((int) _canvas.getWidth(), (int) _canvas.getHeight());
}
Draw();
});
_scene.heightProperty().addListener((obs, oldVal, newVal) -> {
UpdateGUI();
if (_armoredCar != null)
{
_armoredCar.ChangeBorders((int) _canvas.getWidth(), (int) _canvas.getHeight());
}
Draw();
});
_root.applyCss();
_root.layout();
stage.setOnShowing(event -> {
_hBoxHeight = _hBox.getHeight();
_buttonCreateHeight = _buttonCreate.getHeight();
UpdateGUI();
Draw();
});
stage.show();
}
private void InitHBox()
{
_hBox = new HBox();
_hBox.setStyle("-fx-background-color: #31374c;");
_root.getChildren().add(_hBox);
Label labelSpeed = new Label("Speed:");
labelSpeed.setStyle("-fx-text-fill: #b8becc; -fx-padding: 5 5 5 5;");
_hBox.getChildren().add(labelSpeed);
_labelSpeedValue = new Label("-");
_labelSpeedValue.setStyle("-fx-text-fill: #ffffff; -fx-padding: 5 5 5 0; -fx-font-weight: bold;");
_hBox.getChildren().add(_labelSpeedValue);
Label labelWeight = new Label("Weight:");
labelWeight.setStyle("-fx-text-fill: #b8becc; -fx-padding: 5 5 5 5;");
_hBox.getChildren().add(labelWeight);
_labelWeightValue = new Label("-");
_labelWeightValue.setStyle("-fx-text-fill: #ffffff; -fx-padding: 5 5 5 0; -fx-font-weight: bold;");
_hBox.getChildren().add(_labelWeightValue);
Label labelHullColor = new Label("Color:");
labelHullColor.setStyle("-fx-text-fill: #b8becc; -fx-padding: 5 5 5 5;");
_hBox.getChildren().add(labelHullColor);
_labelBodyColorValue = new Label("-");
_labelBodyColorValue.setStyle("-fx-text-fill: #ffffff; -fx-padding: 5 5 5 0; -fx-font-weight: bold;");
_hBox.getChildren().add(_labelBodyColorValue);
Label labelNumRollers = new Label("Rollers:");
labelNumRollers.setStyle("-fx-text-fill: #b8becc; -fx-padding: 5 5 5 5;");
_hBox.getChildren().add(labelNumRollers);
ObservableList<String> optionsForNumRollers = FXCollections.observableArrayList("4", "5", "6");
_comboBoxNumRollers = new ComboBox<>(optionsForNumRollers);
_comboBoxNumRollers.setValue("4");
_comboBoxNumRollers.setOnAction(e -> {
if (_armoredCar != null) {
_armoredCar.GetDrawningRollers().SetNumberRollers(Integer.parseInt(_comboBoxNumRollers.getValue()));
Draw();
}
});
_hBox.getChildren().add(_comboBoxNumRollers);
}
private void InitButtonCreate()
{
_buttonCreate = new Button("Create");
_buttonCreate.setTranslateX(_buttonMargin);
_root.getChildren().add(_buttonCreate);
_buttonCreate.setOnAction(e -> {
Random rnd = new Random();
_armoredCar = new DrawingArmoredCar();
_armoredCar.Init(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
_armoredCar.SetPosition(rnd.nextInt(90) + 10, rnd.nextInt(90)+10,
(int) _canvas.getWidth(), (int) _canvas.getHeight());
_armoredCar.GetDrawningRollers().SetNumberRollers(Integer.parseInt(_comboBoxNumRollers.getValue()));
_labelSpeedValue.setText(Integer.toString(_armoredCar.GetArmoredCar().GetSpeed()));
_labelWeightValue.setText(Double.toString(_armoredCar.GetArmoredCar().GetWeight()));
_labelBodyColorValue.setText(_armoredCar.GetArmoredCar().GetBodyColor().toString());
Draw();
});
}
private void InitMoveButtons()
{
Image img;
ImageView view;
// Button "Up"
_buttonUp = new Button();
img = new Image("arrowUp.png");
view = new ImageView(img);
view.setFitHeight(14);
view.setFitWidth(14);
_buttonUp.setGraphic(view);
_buttonUp.setPrefWidth(_buttonMoveWidth);
_buttonUp.setPrefHeight(_buttonMoveHeight);
_buttonUp.setOnAction(e -> {
if (_armoredCar != null) {
_armoredCar.MoveTransport(Direction.Up);
Draw();
}
});
_root.getChildren().add(_buttonUp);
// Button "Down"
_buttonDown = new Button();
img = new Image("arrowDown.png");
view = new ImageView(img);
view.setFitHeight(14);
view.setFitWidth(14);
_buttonDown.setGraphic(view);
_buttonDown.setPrefWidth(_buttonMoveWidth);
_buttonDown.setPrefHeight(_buttonMoveHeight);
_buttonDown.setOnAction(e -> {
if (_armoredCar != null) {
_armoredCar.MoveTransport(Direction.Down);
Draw();
}
});
_root.getChildren().add(_buttonDown);
// Button "Left"
_buttonLeft = new Button();
img = new Image("arrowLeft.png");
view = new ImageView(img);
view.setFitHeight(14);
view.setFitWidth(14);
_buttonLeft.setGraphic(view);
_buttonLeft.setPrefWidth(_buttonMoveWidth);
_buttonLeft.setPrefHeight(_buttonMoveHeight);
_buttonLeft.setOnAction(e -> {
if (_armoredCar != null) {
_armoredCar.MoveTransport(Direction.Left);
Draw();
}
});
_root.getChildren().add(_buttonLeft);
// Button "Right"
_buttonRight = new Button();
img = new Image("arrowRight.png");
view = new ImageView(img);
view.setFitHeight(14);
view.setFitWidth(14);
_buttonRight.setGraphic(view);
_buttonRight.setPrefWidth(_buttonMoveWidth);
_buttonRight.setPrefHeight(_buttonMoveHeight);
_buttonRight.setOnAction(e -> {
if (_armoredCar != null) {
_armoredCar.MoveTransport(Direction.Right);
Draw();
}
});
_root.getChildren().add(_buttonRight);
}
private void UpdateGUI()
{
double sceneWidth = _scene.getWidth();
double sceneHeight = _scene.getHeight();
_canvas.setWidth(sceneWidth);
_hBox.setPrefWidth(sceneWidth);
_canvas.setHeight(sceneHeight - _hBoxHeight);
_hBox.setTranslateY(sceneHeight - _hBoxHeight);
_buttonCreate.setTranslateY(sceneHeight - _hBoxHeight - _buttonCreateHeight - _buttonMargin);
_buttonUp.setTranslateY(sceneHeight - _hBoxHeight - _buttonMoveHeight * 2.0 - _buttonMargin -
_distanceBetweenMoveButtons);
_buttonUp.setTranslateX(sceneWidth - _buttonMargin - _buttonMoveWidth * 2.0 - _distanceBetweenMoveButtons);
_buttonDown.setTranslateY(sceneHeight - _hBoxHeight - _buttonMoveHeight - _buttonMargin);
_buttonDown.setTranslateX(sceneWidth- _buttonMargin - _buttonMoveWidth * 2.0 - _distanceBetweenMoveButtons);
_buttonLeft.setTranslateY(sceneHeight - _hBoxHeight - _buttonMoveHeight - _buttonMargin);
_buttonLeft.setTranslateX(sceneWidth - _buttonMargin - _buttonMoveWidth * 3.0 -
_distanceBetweenMoveButtons * 2.0);
_buttonRight.setTranslateY(sceneHeight - _hBoxHeight - _buttonMoveHeight - _buttonMargin);
_buttonRight.setTranslateX(sceneWidth - _buttonMargin - _buttonMoveWidth);
}
private void Draw()
{
GraphicsContext gc = _canvas.getGraphicsContext2D();
gc.clearRect(0.0, 0.0, _canvas.getWidth(), _canvas.getHeight());
gc.setFill(Color.WHITE);
gc.fillRect(0.0, 0.0, _canvas.getWidth(), _canvas.getHeight());
gc.setStroke(Color.BLACK);
gc.setLineWidth(4);
gc.strokeRect(0.0, 0.0, _canvas.getWidth(), _canvas.getHeight());
if (_armoredCar != null)
{
_armoredCar.DrawTransport(gc);
}
}
public static void main(String[] args)
{
launch();
}
}

View File

@ -1,23 +0,0 @@
package com.example.antiaircraftgun;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}

View File

@ -1,14 +0,0 @@
package com.example.antiaircraftgun;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class HelloController {
@FXML
private Label welcomeText;
@FXML
protected void onHelloButtonClick() {
welcomeText.setText("Welcome to JavaFX Application!");
}
}

View File

@ -0,0 +1,27 @@
package com.example.antiaircraftgun;
public enum NumberRollers
{
Four,
Five,
Six;
public static NumberRollers FromInteger(int intValue)
{
switch(intValue)
{
case 4:
return Four;
case 5:
return Five;
case 6:
return Six;
default:
System.out.println("Error: incorrect value!");
return Four;
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 257 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 254 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 234 B

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.Pane?>
<Pane xmlns:fx="http://javafx.com/fxml" fx:id="root">
</Pane>