refactored code

This commit is contained in:
ZakenChannel 2024-04-10 14:10:09 +04:00
parent f32a1dc88d
commit d50c303c9f
6 changed files with 47 additions and 59 deletions

View File

@ -30,7 +30,7 @@ public class FormAirFighter extends Application {
private ComboBox<String> comboBoxStrategy; private ComboBox<String> comboBoxStrategy;
@FXML @FXML
private void ButtonStrategy(){ private void buttonStrategy(){
if (drawningWarPlane == null) if (drawningWarPlane == null)
return; return;
if (!comboBoxStrategy.isDisabled()) { if (!comboBoxStrategy.isDisabled()) {
@ -54,7 +54,7 @@ public class FormAirFighter extends Application {
return; return;
comboBoxStrategy.setDisable(true); comboBoxStrategy.setDisable(true);
strategy.makeStep(); strategy.makeStep();
Draw(); draw();
if (strategy.getStatus() == StrategyStatus.Finish){ if (strategy.getStatus() == StrategyStatus.Finish){
comboBoxStrategy.setDisable(false); comboBoxStrategy.setDisable(false);
strategy = null; strategy = null;
@ -68,7 +68,7 @@ public class FormAirFighter extends Application {
Button btn = (Button) event.getSource(); Button btn = (Button) event.getSource();
DirectionType direction = DirectionType.valueOf(btn.getId().toUpperCase()); DirectionType direction = DirectionType.valueOf(btn.getId().toUpperCase());
boolean result = drawningWarPlane.moveTransport(direction); boolean result = drawningWarPlane.moveTransport(direction);
if (result) Draw(); if (result) draw();
} }
// Нажатие кнопок двжиения клавиатурой // Нажатие кнопок двжиения клавиатурой
@ -78,11 +78,11 @@ public class FormAirFighter extends Application {
Button btn = (Button) event.getSource(); Button btn = (Button) event.getSource();
DirectionType direction = DirectionType.valueOf(btn.getId().toUpperCase()); DirectionType direction = DirectionType.valueOf(btn.getId().toUpperCase());
boolean result = drawningWarPlane.moveTransport(direction); boolean result = drawningWarPlane.moveTransport(direction);
if (result) Draw(); if (result) draw();
} }
// Функция отрисовки объекта // Функция отрисовки объекта
private void Draw() { private void draw() {
if (drawningWarPlane == null) return; if (drawningWarPlane == null) return;
GraphicsContext gc = canvasAirFighter.getGraphicsContext2D(); GraphicsContext gc = canvasAirFighter.getGraphicsContext2D();
gc.clearRect(0, 0, canvasAirFighter.getWidth(), canvasAirFighter.getHeight()); gc.clearRect(0, 0, canvasAirFighter.getWidth(), canvasAirFighter.getHeight());
@ -94,7 +94,7 @@ public class FormAirFighter extends Application {
this.drawningWarPlane.setPictureSize((int) canvasAirFighter.getWidth(), (int) canvasAirFighter.getHeight()); this.drawningWarPlane.setPictureSize((int) canvasAirFighter.getWidth(), (int) canvasAirFighter.getHeight());
comboBoxStrategy.setDisable(false); comboBoxStrategy.setDisable(false);
strategy = null; strategy = null;
Draw(); draw();
} }
@FXML @FXML

View File

@ -96,7 +96,7 @@ public class FormWarPlaneCollection extends Application {
} }
@FXML @FXML
private void ButtonRefresh(ActionEvent event){ private void buttonRefresh(ActionEvent event){
if (company == null) return; if (company == null) return;
company.show(canvasWarPlane); company.show(canvasWarPlane);
@ -126,7 +126,7 @@ public class FormWarPlaneCollection extends Application {
} }
@FXML @FXML
private void ButtonGoToCheck(ActionEvent event){ private void buttonGoToCheck(ActionEvent event){
if (company == null) return; if (company == null) return;
DrawningWarPlane warPlane = null; DrawningWarPlane warPlane = null;

View File

@ -7,39 +7,39 @@ import javafx.scene.canvas.GraphicsContext;
import java.util.Random; import java.util.Random;
public abstract class AbstractCompany { public abstract class AbstractCompany {
protected final int _placeSizeWidth = 210; protected final int placeSizeWidth = 210;
protected final int _placeSizeHeight = 150; protected final int placeSizeHeight = 150;
protected final int _pictureWidth; protected final int pictureWidth;
protected final int _pictureHeight; protected final int pictureHeight;
protected ICollectionGenericObjects<DrawningWarPlane> _collection = null; protected ICollectionGenericObjects<DrawningWarPlane> collection = null;
private int getMaxCount() { private int getMaxCount() {
return _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight); return pictureWidth * pictureHeight / (placeSizeWidth * placeSizeHeight);
} }
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawningWarPlane> collection) { public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawningWarPlane> collection) {
_pictureWidth = picWidth; pictureWidth = picWidth;
_pictureHeight = picHeight; pictureHeight = picHeight;
_collection = collection; this.collection = collection;
_collection.setMaxCount(getMaxCount()); this.collection.setMaxCount(getMaxCount());
} }
public int addPlane(DrawningWarPlane plane) { public int addPlane(DrawningWarPlane plane) {
if (_collection == null) { if (collection == null) {
return -1; return -1;
} }
return _collection.insert(plane); return collection.insert(plane);
} }
public DrawningWarPlane removePlane(int position) { public DrawningWarPlane removePlane(int position) {
return _collection != null ? _collection.remove(position) : null; return collection != null ? collection.remove(position) : null;
} }
public DrawningWarPlane getRandomPlane() { public DrawningWarPlane getRandomPlane() {
Random rnd = new Random(); Random rnd = new Random();
return _collection != null ? _collection.get(rnd.nextInt(getMaxCount())) : null; return collection != null ? collection.get(rnd.nextInt(getMaxCount())) : null;
} }
public void show(Canvas canvas) { public void show(Canvas canvas) {
@ -48,8 +48,8 @@ public abstract class AbstractCompany {
drawBackground(graphicsContext); drawBackground(graphicsContext);
setObjectsPosition(); setObjectsPosition();
for (int i = 0; i < (_collection != null ? _collection.getCount() : 0); i++) { for (int i = 0; i < (collection != null ? collection.getCount() : 0); i++) {
DrawningWarPlane obj = _collection.get(i); DrawningWarPlane obj = collection.get(i);
if (obj != null) obj.drawTransport(graphicsContext); if (obj != null) obj.drawTransport(graphicsContext);
} }
} }

View File

@ -15,27 +15,27 @@ public class Hangar extends AbstractCompany {
gc.setLineWidth(3); gc.setLineWidth(3);
int posX = 0; int posX = 0;
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { for (int i = 0; i < pictureWidth / placeSizeWidth; i++) {
int posY = 0; int posY = 0;
gc.strokeLine(posX, posY, posX, posY + _placeSizeHeight * (_pictureHeight / _placeSizeHeight)); gc.strokeLine(posX, posY, posX, posY + placeSizeHeight * (pictureHeight / placeSizeHeight));
for (int j = 0; j <= _pictureHeight / _placeSizeHeight; j++) { for (int j = 0; j <= pictureHeight / placeSizeHeight; j++) {
gc.strokeLine(posX, posY, posX + _placeSizeWidth - 30, posY); gc.strokeLine(posX, posY, posX + placeSizeWidth - 30, posY);
posY += _placeSizeHeight; posY += placeSizeHeight;
} }
posX += _placeSizeWidth; posX += placeSizeWidth;
} }
} }
@Override @Override
protected void setObjectsPosition() { protected void setObjectsPosition() {
int counter = 0; int counter = 0;
int valPlaceY = _pictureHeight / _placeSizeHeight; int valPlaceY = pictureHeight / placeSizeHeight;
int valPlaceX = _pictureWidth / _placeSizeWidth; int valPlaceX = pictureWidth / placeSizeWidth;
for (int y = ((valPlaceY - 1) * _placeSizeHeight) + 8; y >= 0; y -= _placeSizeHeight) { for (int y = ((valPlaceY - 1) * placeSizeHeight) + 8; y >= 0; y -= placeSizeHeight) {
for (int x = ((valPlaceX - 1) * _placeSizeWidth) + 10; x >= 0; x -= _placeSizeWidth) { for (int x = ((valPlaceX - 1) * placeSizeWidth) + 10; x >= 0; x -= placeSizeWidth) {
DrawningWarPlane plane = _collection != null ? _collection.get(counter) : null; DrawningWarPlane plane = collection != null ? collection.get(counter) : null;
if (plane != null) { if (plane != null) {
plane.setPictureSize(_pictureWidth, _pictureHeight); plane.setPictureSize(pictureWidth, pictureHeight);
plane.setPosition(x, y); plane.setPosition(x, y);
} }
counter++; counter++;

View File

@ -28,5 +28,5 @@
</font> </font>
</Button> </Button>
<ComboBox fx:id="comboBoxStrategy" layoutX="499.0" layoutY="9.0" prefHeight="25.0" prefWidth="99.0" visibleRowCount="2" AnchorPane.rightAnchor="9.0" AnchorPane.topAnchor="9.0" /> <ComboBox fx:id="comboBoxStrategy" layoutX="499.0" layoutY="9.0" prefHeight="25.0" prefWidth="99.0" visibleRowCount="2" AnchorPane.rightAnchor="9.0" AnchorPane.topAnchor="9.0" />
<Button layoutX="695.0" layoutY="42.0" mnemonicParsing="false" onAction="#ButtonStrategy" onKeyPressed="#ButtonStrategy" prefHeight="25.0" prefWidth="53.0" text="Шаг" AnchorPane.rightAnchor="9.0" AnchorPane.topAnchor="42.0" /> <Button layoutX="695.0" layoutY="42.0" mnemonicParsing="false" onAction="#buttonStrategy" onKeyPressed="#buttonStrategy" prefHeight="25.0" prefWidth="53.0" text="Шаг" AnchorPane.rightAnchor="9.0" AnchorPane.topAnchor="42.0" />
</AnchorPane> </AnchorPane>

View File

@ -5,30 +5,18 @@
<?import javafx.scene.layout.*?> <?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?> <?import javafx.scene.text.*?>
<SplitPane dividerPositions="0.8076923076923077" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" <SplitPane dividerPositions="0.8076923076923077" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="1250.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.projectairfighter.FormWarPlaneCollection">
minWidth="-Infinity" prefHeight="700.0" prefWidth="1250.0" xmlns="http://javafx.com/javafx/17.0.2-ea"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.projectairfighter.FormWarPlaneCollection">
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="698.0" prefWidth="965.0"> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="698.0" prefWidth="965.0">
<Canvas fx:id="canvasWarPlane" height="700.0" width="1005.0"/> <Canvas fx:id="canvasWarPlane" height="700.0" width="1005.0" />
</AnchorPane> </AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="196.0"> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="196.0">
<ComboBox fx:id="comboBox" layoutX="6.0" layoutY="26.0" onAction="#comboBoxSelectorCompany" <ComboBox fx:id="comboBox" layoutX="6.0" layoutY="26.0" onAction="#comboBoxSelectorCompany" prefHeight="25.0" prefWidth="224.0" />
prefHeight="25.0" prefWidth="224.0"/> <Button fx:id="DrawningAirFighter" contentDisplay="CENTER" layoutX="6.0" layoutY="75.0" mnemonicParsing="false" onAction="#createAiFighter" prefHeight="42.0" prefWidth="224.0" text="Добавление истребителя" />
<Button fx:id="DrawningAirFighter" contentDisplay="CENTER" layoutX="6.0" layoutY="75.0" <Button fx:id="DrawningWarPlane" contentDisplay="CENTER" layoutX="7.0" layoutY="140.0" mnemonicParsing="false" onAction="#createWarPlane" prefHeight="45.0" prefWidth="224.0" text="Добавление военного самолета" />
mnemonicParsing="false" onAction="#createAiFighter" prefHeight="42.0" prefWidth="224.0" <TextField fx:id="textBox" layoutX="5.0" layoutY="261.0" prefHeight="25.0" prefWidth="224.0" />
text="Добавление истребителя"/> <Button alignment="CENTER" contentDisplay="CENTER" layoutX="6.0" layoutY="295.0" mnemonicParsing="false" onAction="#buttonRemovePlaneClicked" prefHeight="42.0" prefWidth="224.0" text="Удалить истребитель" />
<Button fx:id="DrawningWarPlane" contentDisplay="CENTER" layoutX="7.0" layoutY="140.0" <Button alignment="CENTER" contentDisplay="CENTER" layoutX="8.0" layoutY="407.0" mnemonicParsing="false" onAction="#buttonGoToCheck" prefHeight="42.0" prefWidth="224.0" text="Передать на тесты" />
mnemonicParsing="false" onAction="#createWarPlane" prefHeight="45.0" prefWidth="224.0" <Button alignment="CENTER" contentDisplay="CENTER" layoutX="8.0" layoutY="642.0" mnemonicParsing="false" onAction="#buttonRefresh" prefHeight="42.0" prefWidth="224.0" text="Обновить" />
text="Добавление военного самолета"/> <Text layoutX="14.0" layoutY="19.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Инструменты" AnchorPane.leftAnchor="14.0" AnchorPane.topAnchor="6.0" />
<TextField fx:id="textBox" layoutX="5.0" layoutY="261.0" prefHeight="25.0" prefWidth="224.0"/>
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="6.0" layoutY="295.0" mnemonicParsing="false"
onAction="#buttonRemovePlaneClicked" prefHeight="42.0" prefWidth="224.0"
text="Удалить истребитель"/>
<Button contentDisplay="CENTER" layoutX="8.0" layoutY="407.0" mnemonicParsing="false"
onAction="#ButtonGoToCheck" prefHeight="42.0" prefWidth="224.0" text="Передать на тесты"/>
<Button contentDisplay="CENTER" layoutX="8.0" layoutY="642.0" mnemonicParsing="false"
onAction="#ButtonRefresh" prefHeight="42.0" prefWidth="224.0" text="Обновить"/>
<Text layoutX="14.0" layoutY="19.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Инструменты"
AnchorPane.leftAnchor="14.0" AnchorPane.topAnchor="6.0"/>
</AnchorPane> </AnchorPane>
</SplitPane> </SplitPane>