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;
@FXML
private void ButtonStrategy(){
private void buttonStrategy(){
if (drawningWarPlane == null)
return;
if (!comboBoxStrategy.isDisabled()) {
@ -54,7 +54,7 @@ public class FormAirFighter extends Application {
return;
comboBoxStrategy.setDisable(true);
strategy.makeStep();
Draw();
draw();
if (strategy.getStatus() == StrategyStatus.Finish){
comboBoxStrategy.setDisable(false);
strategy = null;
@ -68,7 +68,7 @@ public class FormAirFighter extends Application {
Button btn = (Button) event.getSource();
DirectionType direction = DirectionType.valueOf(btn.getId().toUpperCase());
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();
DirectionType direction = DirectionType.valueOf(btn.getId().toUpperCase());
boolean result = drawningWarPlane.moveTransport(direction);
if (result) Draw();
if (result) draw();
}
// Функция отрисовки объекта
private void Draw() {
private void draw() {
if (drawningWarPlane == null) return;
GraphicsContext gc = canvasAirFighter.getGraphicsContext2D();
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());
comboBoxStrategy.setDisable(false);
strategy = null;
Draw();
draw();
}
@FXML

View File

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

View File

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

View File

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

View File

@ -28,5 +28,5 @@
</font>
</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" />
<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>

View File

@ -5,30 +5,18 @@
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<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">
<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">
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="698.0" prefWidth="965.0">
<Canvas fx:id="canvasWarPlane" height="700.0" width="1005.0" />
</AnchorPane>
<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"
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="DrawningWarPlane" contentDisplay="CENTER" layoutX="7.0" layoutY="140.0"
mnemonicParsing="false" onAction="#createWarPlane" prefHeight="45.0" prefWidth="224.0"
text="Добавление военного самолета"/>
<ComboBox fx:id="comboBox" layoutX="6.0" layoutY="26.0" onAction="#comboBoxSelectorCompany" 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="DrawningWarPlane" contentDisplay="CENTER" layoutX="7.0" layoutY="140.0" mnemonicParsing="false" onAction="#createWarPlane" prefHeight="45.0" prefWidth="224.0" text="Добавление военного самолета" />
<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"/>
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="6.0" layoutY="295.0" mnemonicParsing="false" onAction="#buttonRemovePlaneClicked" prefHeight="42.0" prefWidth="224.0" text="Удалить истребитель" />
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="8.0" layoutY="407.0" mnemonicParsing="false" onAction="#buttonGoToCheck" prefHeight="42.0" prefWidth="224.0" text="Передать на тесты" />
<Button alignment="CENTER" 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>
</SplitPane>