Compare commits

...

7 Commits

Author SHA1 Message Date
yu.shlyapin
f33808a992 lab2 correct 2025-02-17 17:54:43 +04:00
yu.shlyapin
f372f7e616 modified folder strucure 2025-02-17 17:53:20 +04:00
yu.shlyapin
c1652c191a lab2 release 2025-02-17 17:53:20 +04:00
yu.shlyapin
f40d570442 lab2 v 0.2 2025-02-17 17:53:06 +04:00
yu.shlyapin
fd6752a9b4 lab1 correct 2025-02-17 11:59:04 +04:00
yu.shlyapin
d9d73ad660 lab1 release 2025-02-07 20:22:55 +04:00
yu.shlyapin
116675a1e3 lab1 v 0.1 2025-02-05 20:56:06 +04:00
32 changed files with 984 additions and 34 deletions

7
.idea/encodings.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

124
.idea/uiDesigner.xml generated Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

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

View File

@ -60,7 +60,7 @@
<!-- Default configuration for running with: mvn clean javafx:run --> <!-- Default configuration for running with: mvn clean javafx:run -->
<id>default-cli</id> <id>default-cli</id>
<configuration> <configuration>
<mainClass>com.example.aircraftcarrier/com.example.aircraftcarrier.HelloApplication <mainClass>com.example.aircraftcarrier/AircraftCarrierApplication
</mainClass> </mainClass>
<launcher>app</launcher> <launcher>app</launcher>
<jlinkZipName>app</jlinkZipName> <jlinkZipName>app</jlinkZipName>

View File

@ -1,17 +1,19 @@
package com.example.aircraftcarrier; package com.example.aircraftcarrier;
import javafx.application.Application; import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.io.IOException; import java.io.IOException;
public class HelloApplication extends Application { public class AircraftCarrierApplication extends Application {
@Override @Override
public void start(Stage stage) throws IOException { public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); FXMLLoader fxmlLoader = new FXMLLoader(AircraftCarrierApplication.class.getResource("/com/example/aircraftcarrier/AircraftCarrierView.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240); Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Hello!"); stage.setTitle("Hello!");
stage.setScene(scene); stage.setScene(scene);
stage.show(); stage.show();

View File

@ -0,0 +1,179 @@
package com.example.aircraftcarrier;
import com.example.aircraftcarrier.Logic.Aircraft.*;
import com.example.aircraftcarrier.Logic.Block.DrawingBlock;
import com.example.aircraftcarrier.Logic.Block.DrawingBlockCircle;
import com.example.aircraftcarrier.Logic.Block.DrawingBlockTriangle;
import com.example.aircraftcarrier.Logic.Block.IDrawingBlock;
import com.example.aircraftcarrier.Logic.Enum.DirectionType;
import com.example.aircraftcarrier.Logic.MovementStategy.*;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
public class AircraftCarrierController implements Initializable {
Random random = new Random();
DrawingSimpleAircraftCarrier drawingAircraftCarrier;
AbstractStrategy strategy;
@FXML
private AnchorPane Root;
@FXML
private Canvas canvas;
@FXML
private TextField inputCountBlocks;
@FXML
private ChoiceBox<String> choiceBoxStrategy;
private String[] strategyText = new String[]{"к центру", "к углу"};
@FXML
private ChoiceBox<String> choiceBoxShape;
private String[] shapeText = new String[]{"квадрат", "треугольник", "круг"};
@FXML
protected void ButtonStrategy_Click() {
if (drawingAircraftCarrier == null) return;
if (!choiceBoxStrategy.isDisable()) {
strategy = switch (choiceBoxStrategy.getValue()) {
case "к центру" -> new MoveToCenter();
case "к углу" -> new MoveToCorner();
default -> null;
};
if (strategy == null) return;
strategy.SetDate(new MoveableCarrier(drawingAircraftCarrier),
(int)Root.getWidth(),
(int)Root.getHeight()
);
}
if (strategy==null) return;
choiceBoxStrategy.setDisable(true);
strategy.MakeStep();
drawingAircraftCarrier.Draw(canvas.getGraphicsContext2D());
if (strategy.GetStatus() == StrategyStatus.Finish) {
choiceBoxStrategy.setDisable(false);
strategy = null;
}
}
@FXML
protected void ButtonCreateSimple_Click() {
drawingAircraftCarrier = new DrawingSimpleAircraftCarrier(
random.nextInt(100) + 100,
random.nextInt(1000) + 1000,
Color.rgb(
random.nextInt(256),
random.nextInt(256),
random.nextInt(256)
)
);
drawingAircraftCarrier.setCanvasWidth((int) canvas.getWidth());
drawingAircraftCarrier.setCanvasHeight((int) canvas.getHeight());
if (
drawingAircraftCarrier.setPosition(
random.nextInt(50) + 10,
random.nextInt(50) + 10
)
) {
drawingAircraftCarrier.Draw(canvas.getGraphicsContext2D());
}
}
@FXML
protected void ButtonCreateAdvanced_Click() {
int inputCount;
try {
inputCount = Integer.parseInt(inputCountBlocks.getText());
} catch (Exception ignore) {
inputCount = 0;
}
drawingAircraftCarrier = new DrawingAircraftCarrier(
random.nextInt(100) + 100,
random.nextInt(1000) + 1000,
Color.rgb(
random.nextInt(256),
random.nextInt(256),
random.nextInt(256)
),
Color.rgb(
random.nextInt(256),
random.nextInt(256),
random.nextInt(256)
),
random.nextBoolean(),
random.nextBoolean(),
inputCount,
choiceBoxShape.getValue()
);
drawingAircraftCarrier.setCanvasWidth((int) canvas.getWidth());
drawingAircraftCarrier.setCanvasHeight((int) canvas.getHeight());
if (
drawingAircraftCarrier.setPosition(
random.nextInt(50) + 10,
random.nextInt(50) + 10
)
) {
drawingAircraftCarrier.Draw(canvas.getGraphicsContext2D());
}
}
@FXML
protected void MoveEvent(Event e) {
if (drawingAircraftCarrier == null) return;
DirectionType directionType;
if (e instanceof KeyEvent keyEvent) {
directionType = switch (keyEvent.getCode()) {
case KeyCode.W -> DirectionType.Up;
case KeyCode.S -> DirectionType.Down;
case KeyCode.A -> DirectionType.Left;
case KeyCode.D -> DirectionType.Right;
default -> DirectionType.Unknown;
};
} else {
String direction = ((Button)e.getSource()).getId();
directionType = switch (direction) {
case "up" -> DirectionType.Up;
case "down" -> DirectionType.Down;
case "left" -> DirectionType.Left;
case "right" -> DirectionType.Right;
default -> DirectionType.Unknown;
};
}
if (directionType == DirectionType.Unknown) return;
drawingAircraftCarrier.Move(directionType);
drawingAircraftCarrier.Draw(canvas.getGraphicsContext2D());
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
System.out.println("start");
canvas.widthProperty().bind(Root.widthProperty());
canvas.heightProperty().bind(Root.heightProperty());
choiceBoxStrategy.getItems().addAll(strategyText);
choiceBoxShape.getItems().addAll(shapeText);
}
}

View File

@ -1,14 +0,0 @@
package com.example.aircraftcarrier;
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,64 @@
package com.example.aircraftcarrier.Logic.Aircraft;
import com.example.aircraftcarrier.Logic.Block.DrawingBlock;
import com.example.aircraftcarrier.Logic.Block.DrawingBlockCircle;
import com.example.aircraftcarrier.Logic.Block.DrawingBlockTriangle;
import com.example.aircraftcarrier.Logic.Block.IDrawingBlock;
import com.example.aircraftcarrier.Logic.Entity.EntityAircraftCarrier;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class DrawingAircraftCarrier extends DrawingSimpleAircraftCarrier {
public IDrawingBlock drawingBlock;
public DrawingAircraftCarrier(
int speed,
int weight,
Color primaryColor,
Color secondaryColor,
boolean hasDeck,
boolean hasСabin,
int countBlock,
String shape
) {
super();
aircraftCarrier = new EntityAircraftCarrier(speed,
weight,
primaryColor,
secondaryColor,
hasDeck,
hasСabin);
if (shape != null) {
drawingBlock = switch (shape) {
case "квадрат" -> new DrawingBlock(countBlock);
case "треугольник" -> new DrawingBlockTriangle(countBlock);
case "круг" -> new DrawingBlockCircle(countBlock);
default -> null;
};
} else {
drawingBlock = null;
}
}
@Override
public void Draw(GraphicsContext gc) {
if (aircraftCarrier == null) return;
if (aircraftCarrier instanceof EntityAircraftCarrier entityAircraftCarrier) {
super.Draw(gc);
if (entityAircraftCarrier.isHasDeck()) {
gc.setFill(entityAircraftCarrier.getPrimaryColor());
gc.fillRect(x + 50, y + 40, 55, 20);
}
if (entityAircraftCarrier.isHasСabin()) {
gc.setFill(entityAircraftCarrier.getSecondaryColor());
gc.fillRect(x + 105, y + 25, 30, 50);
}
if (drawingBlock == null) return;
drawingBlock.Draw(gc, x, y, entityAircraftCarrier.getSecondaryColor());
}
}
}

View File

@ -0,0 +1,103 @@
package com.example.aircraftcarrier.Logic.Aircraft;
import com.example.aircraftcarrier.Logic.Entity.EntitySimpleAircraftCarrier;
import com.example.aircraftcarrier.Logic.Enum.DirectionType;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class DrawingSimpleAircraftCarrier {
public EntitySimpleAircraftCarrier aircraftCarrier;
protected int x;
protected int y;
private int widthCanvas;
private int heightCanvas;
private static final int widthPicture = 250;
private static final int heightPicture = 100;
public DrawingSimpleAircraftCarrier() {}
public DrawingSimpleAircraftCarrier(
int speed,
int weight,
Color primaryColor
) {
aircraftCarrier = new EntitySimpleAircraftCarrier(
speed,
weight,
primaryColor
);
}
public void setCanvasWidth(int width) {
if (x+widthPicture > width);
widthCanvas = width;
}
public void setCanvasHeight(int height) {
if (y+heightPicture > height) return;
heightCanvas = height;
}
public boolean setPosition(int newX, int newY) {
if (newX < 0 || newY < 0 || newX + widthPicture > widthCanvas || newY + heightPicture > heightCanvas) return false;
x = newX;
y = newY;
return true;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidthPicture() {
return widthPicture;
}
public int getHeightPicture() {
return heightPicture;
}
public void Draw(GraphicsContext gc) {
gc.clearRect(0,0,widthCanvas,heightCanvas);
double[] X = new double[]{x,x+200,x+250,x+200,x};
double[] Y = new double[]{y,y,y+50,y+100,y+100};
gc.strokePolygon(X, Y,5);
gc.strokeOval(x+175,y+35,30,30);
}
public boolean Move(DirectionType direction) {
switch (direction) {
case Up:
if (y - aircraftCarrier.getStep() > 0) {
y -= aircraftCarrier.getStep();
return true;
}
return false;
case Down:
if (y + aircraftCarrier.getStep() + heightPicture < heightCanvas) {
y += aircraftCarrier.getStep();
return true;
}
return false;
case Right:
if (x + aircraftCarrier.getStep() + widthPicture < widthCanvas) {
x += aircraftCarrier.getStep();
return true;
}
return false;
case Left:
if (x - aircraftCarrier.getStep() > 0) {
x -= aircraftCarrier.getStep();
return true;
}
return false;
default: return false;
}
}
}

View File

@ -0,0 +1,35 @@
package com.example.aircraftcarrier.Logic.Block;
import com.example.aircraftcarrier.Logic.Enum.BlockCount;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class DrawingBlock implements IDrawingBlock {
private BlockCount blockCount;
public DrawingBlock(int blockCountValue) {
this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue);
}
public void setBlockCount(int blockCountValue) {
this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue);
}
private void DrawTwoBlock(GraphicsContext gc, int x, int y, Color color) {
gc.setFill(color);
gc.fillRect(x,y,10,10);
gc.fillRect(x+15,y,10,10);
}
public void Draw(GraphicsContext gc, int x, int y, Color color) {
switch (blockCount) {
case Six:
DrawTwoBlock(gc,x+20,y+50,color);
case Four:
DrawTwoBlock(gc,x+20,y+35,color);
case Two:
DrawTwoBlock(gc,x+20,y+20,color);
break;
}
}
}

View File

@ -0,0 +1,35 @@
package com.example.aircraftcarrier.Logic.Block;
import com.example.aircraftcarrier.Logic.Enum.BlockCount;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class DrawingBlockCircle implements IDrawingBlock {
private BlockCount blockCount;
public DrawingBlockCircle(int blockCountValue) {
this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue);
}
public void setBlockCount(int blockCountValue) {
this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue);
}
private void DrawTwoBlock(GraphicsContext gc, int x, int y, Color color) {
gc.setFill(color);
gc.fillOval(x,y,10,10);
gc.fillOval(x+15,y,10,10);
}
public void Draw(GraphicsContext gc, int x, int y, Color color) {
switch (blockCount) {
case Six:
DrawTwoBlock(gc,x+20,y+50,color);
case Four:
DrawTwoBlock(gc,x+20,y+35,color);
case Two:
DrawTwoBlock(gc,x+20,y+20,color);
break;
}
}
}

View File

@ -0,0 +1,39 @@
package com.example.aircraftcarrier.Logic.Block;
import com.example.aircraftcarrier.Logic.Enum.BlockCount;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class DrawingBlockTriangle implements IDrawingBlock {
private BlockCount blockCount;
public DrawingBlockTriangle(int blockCountValue) {
this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue);
}
public void setBlockCount(int blockCountValue) {
this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue);
}
private void DrawTwoBlock(GraphicsContext gc, int x, int y, Color color) {
gc.setFill(color);
double[] X_first = new double[]{x+5,x+10,x};
double[] Y_first = new double[]{y,y+10,y+10};
double[] X_second = new double[]{x+15+5,x+15+10,x+15};
double[] Y_second = Y_first;
gc.fillPolygon(X_first, Y_first,3);
gc.fillPolygon(X_second, Y_second,3);
}
public void Draw(GraphicsContext gc, int x, int y, Color color) {
switch (blockCount) {
case Six:
DrawTwoBlock(gc,x+20,y+50,color);
case Four:
DrawTwoBlock(gc,x+20,y+35,color);
case Two:
DrawTwoBlock(gc,x+20,y+20,color);
break;
}
}
}

View File

@ -0,0 +1,10 @@
package com.example.aircraftcarrier.Logic.Block;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public interface IDrawingBlock {
void setBlockCount(int blockCountValue);
void Draw(GraphicsContext gc, int x, int y, Color color);
}

View File

@ -0,0 +1,36 @@
package com.example.aircraftcarrier.Logic.Entity;
import javafx.scene.paint.Color;
public class EntityAircraftCarrier extends EntitySimpleAircraftCarrier {
private Color SecondaryColor;
private boolean HasDeck;
private boolean HasСabin;
public EntityAircraftCarrier(
int speed,
int weight,
Color primaryColor,
Color secondaryColor,
boolean hasDeck,
boolean hasСabin
){
super(speed,weight,primaryColor);
this.SecondaryColor = secondaryColor;
this.HasDeck = hasDeck;
this.HasСabin = hasСabin;
}
public Color getSecondaryColor() {
return SecondaryColor;
}
public boolean isHasDeck() {
return HasDeck;
}
public boolean isHasСabin() {
return HasСabin;
}
}

View File

@ -0,0 +1,35 @@
package com.example.aircraftcarrier.Logic.Entity;
import javafx.scene.paint.Color;
public class EntitySimpleAircraftCarrier {
private int Speed;
private double Weight;
private Color PrimaryColor;
public EntitySimpleAircraftCarrier(
int speed,
double weight,
Color primaryColor
) {
this.Speed = speed;
this.Weight = weight;
this.PrimaryColor = primaryColor;
}
public int getSpeed() {
return Speed;
}
public double getWeight() {
return Weight;
}
public Color getPrimaryColor() {
return PrimaryColor;
}
public int getStep() {
return (int) (Speed * 100 / Weight);
}
}

View File

@ -0,0 +1,31 @@
package com.example.aircraftcarrier.Logic.Enum;
public enum BlockCount {
Two(2),
Four(4),
Six(6),
Other();
private Integer count;
BlockCount() {
this.count = null;
}
BlockCount(int count) {
this.count = count;
}
public static BlockCount ConvertToBlockCount(int count) {
return switch (count) {
case 2 -> Two;
case 4 -> Four;
case 6 -> Six;
default -> Other;
};
}
public Integer getCount() {
return count;
}
}

View File

@ -0,0 +1,9 @@
package com.example.aircraftcarrier.Logic.Enum;
public enum DirectionType {
Unknown,
Up,
Down,
Right,
Left;
}

View File

@ -0,0 +1,72 @@
package com.example.aircraftcarrier.Logic.MovementStategy;
public abstract class AbstractStrategy {
private IMoveableObject moveableObject;
private StrategyStatus status = StrategyStatus.NotInit;
protected int FieldWidth;
protected int FieldHeight;
public int getFieldWidth() {
return FieldWidth;
}
public int getFieldHeight() {
return FieldHeight;
}
public StrategyStatus GetStatus() {
return status;
}
public void SetDate(IMoveableObject moveableObject,
int width,
int height) {
if (moveableObject == null) {
status = StrategyStatus.NotInit;
return;
}
status = StrategyStatus.InProgress;
this.moveableObject = moveableObject;
this.FieldWidth = width;
this.FieldHeight = height;
}
public void MakeStep() {
if(status != StrategyStatus.InProgress) return;
if (IsTargetDestination()) {
status = StrategyStatus.Finish;
return;
}
MoveToTarget();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestination();
protected boolean MoveLeft() {
return MoveTo(MovementDirection.Left);
}
protected boolean MoveRight() {
return MoveTo(MovementDirection.Right);
}
protected boolean MoveUp() {
return MoveTo(MovementDirection.Up);
}
protected boolean MoveDown() {
return MoveTo(MovementDirection.Down);
}
protected ObjectParameters GetObjectParameters() {
return moveableObject.GetObjectPosition();
}
protected Integer GetStep() {
if (status != StrategyStatus.InProgress) return null;
return moveableObject.GetStep();
}
private boolean MoveTo(MovementDirection direction) {
if (moveableObject==null || status != StrategyStatus.InProgress) return false;
return moveableObject.TryMoveObject(direction);
}
}

View File

@ -0,0 +1,7 @@
package com.example.aircraftcarrier.Logic.MovementStategy;
public interface IMoveableObject {
ObjectParameters GetObjectPosition();
int GetStep();
boolean TryMoveObject(MovementDirection direction);
}

View File

@ -0,0 +1,32 @@
package com.example.aircraftcarrier.Logic.MovementStategy;
public class MoveToCenter extends AbstractStrategy {
@Override
protected void MoveToTarget() {
ObjectParameters objectParameters = GetObjectParameters();
if (objectParameters == null) return;
int diffX = objectParameters.CenterX() - getFieldWidth()/2;
if (Math.abs(diffX) > GetStep()) {
if (diffX > 0) {
MoveLeft();
} else { MoveRight(); }
}
int diffY = objectParameters.CenterY() - getFieldHeight()/2;
if (Math.abs(diffY) > GetStep()) {
if (diffY > 0) {
MoveUp();
} else { MoveDown(); }
}
}
@Override
protected boolean IsTargetDestination() {
ObjectParameters objectParameters = GetObjectParameters();
if (objectParameters == null) return false;
return objectParameters.CenterX() - GetStep() <= getFieldWidth()/2 &&
objectParameters.CenterX() + GetStep() >= getFieldWidth()/2 &&
objectParameters.CenterY() - GetStep() <= getFieldHeight()/2 &&
objectParameters.CenterY() + GetStep() >= getFieldHeight()/2;
}
}

View File

@ -0,0 +1,25 @@
package com.example.aircraftcarrier.Logic.MovementStategy;
public class MoveToCorner extends AbstractStrategy {
@Override
protected void MoveToTarget() {
ObjectParameters objectParameters = GetObjectParameters();
if (objectParameters == null) return;
int diffX = objectParameters.RightBorder() - getFieldWidth();
if (Math.abs(diffX) > GetStep()) {
MoveRight();
}
int diffY = objectParameters.BottomBorder() - getFieldHeight();
if (Math.abs(diffY) > GetStep()) {
MoveDown();
}
}
@Override
protected boolean IsTargetDestination() {
ObjectParameters objectParameters = GetObjectParameters();
if (objectParameters == null) return false;
return objectParameters.RightBorder() + GetStep() >= getFieldWidth() &&
objectParameters.RightBorder() + GetStep() >= getFieldHeight();
}
}

View File

@ -0,0 +1,42 @@
package com.example.aircraftcarrier.Logic.MovementStategy;
import com.example.aircraftcarrier.Logic.Aircraft.DrawingSimpleAircraftCarrier;
import com.example.aircraftcarrier.Logic.Enum.DirectionType;
public class MoveableCarrier implements IMoveableObject {
private final DrawingSimpleAircraftCarrier drawingSimpleAircraftCarrier;
public MoveableCarrier(DrawingSimpleAircraftCarrier drawingSimpleAircraftCarrier) {
this.drawingSimpleAircraftCarrier = drawingSimpleAircraftCarrier;
}
@Override
public ObjectParameters GetObjectPosition() {
if (drawingSimpleAircraftCarrier == null || drawingSimpleAircraftCarrier.aircraftCarrier == null) {
return null;
}
return new ObjectParameters(drawingSimpleAircraftCarrier);
}
@Override
public int GetStep() {
return drawingSimpleAircraftCarrier.aircraftCarrier.getStep();
}
@Override
public boolean TryMoveObject(MovementDirection direction) {
if (drawingSimpleAircraftCarrier == null || drawingSimpleAircraftCarrier.aircraftCarrier == null)
return false;
return drawingSimpleAircraftCarrier.Move(GetDirectionType(direction));
}
private static DirectionType GetDirectionType(MovementDirection direction) {
return switch (direction) {
case Up -> DirectionType.Up;
case Down -> DirectionType.Down;
case Left -> DirectionType.Left;
case Right -> DirectionType.Right;
default -> DirectionType.Unknown;
};
}
}

View File

@ -0,0 +1,8 @@
package com.example.aircraftcarrier.Logic.MovementStategy;
public enum MovementDirection {
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,43 @@
package com.example.aircraftcarrier.Logic.MovementStategy;
import com.example.aircraftcarrier.Logic.Aircraft.DrawingSimpleAircraftCarrier;
public class ObjectParameters {
private final int x;
private final int y;
private final int width;
private final int height;
public ObjectParameters(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public ObjectParameters(DrawingSimpleAircraftCarrier drawingSimpleAircraftCarrier) {
this.x = drawingSimpleAircraftCarrier.getX();
this.y = drawingSimpleAircraftCarrier.getY();
this.width = drawingSimpleAircraftCarrier.getWidthPicture();
this.height = drawingSimpleAircraftCarrier.getHeightPicture();
}
public int LeftBorder() {
return x;
}
public int RightBorder() {
return x+width;
}
public int TopBorder() {
return y;
}
public int BottomBorder() {
return y+height;
}
public int CenterX() {
return x+width / 2;
}
public int CenterY() {
return y+height/2;
}
}

View File

@ -0,0 +1,7 @@
package com.example.aircraftcarrier.Logic.MovementStategy;
public enum StrategyStatus {
NotInit,
InProgress,
Finish
}

View File

@ -5,4 +5,12 @@ module com.example.aircraftcarrier {
opens com.example.aircraftcarrier to javafx.fxml; opens com.example.aircraftcarrier to javafx.fxml;
exports com.example.aircraftcarrier; exports com.example.aircraftcarrier;
exports com.example.aircraftcarrier.Logic.Aircraft;
opens com.example.aircraftcarrier.Logic.Aircraft to javafx.fxml;
exports com.example.aircraftcarrier.Logic.Entity;
opens com.example.aircraftcarrier.Logic.Entity to javafx.fxml;
exports com.example.aircraftcarrier.Logic.Enum;
opens com.example.aircraftcarrier.Logic.Enum to javafx.fxml;
exports com.example.aircraftcarrier.Logic.Block;
opens com.example.aircraftcarrier.Logic.Block to javafx.fxml;
} }

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane fx:id="Root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onKeyPressed="#MoveEvent" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.aircraftcarrier.AircraftCarrierController">
<children>
<Canvas fx:id="canvas" height="400.0" width="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
<Button layoutX="14.0" layoutY="361.0" mnemonicParsing="false" onAction="#ButtonCreateSimple_Click" text="Создать простую" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" />
<Button layoutX="130.0" layoutY="354.0" mnemonicParsing="false" onAction="#ButtonCreateAdvanced_Click" text="Создать сложную" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="135.0" />
<Button fx:id="up" layoutX="524.4" layoutY="324.4" minHeight="25.0" minWidth="25.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url(&quot;up-arrow.png&quot;); -fx-background-size: stretch;" AnchorPane.bottomAnchor="50.0" AnchorPane.rightAnchor="50.0" />
<Button fx:id="right" layoutX="554.4" layoutY="354.4" minHeight="25.0" minWidth="25.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url(&quot;right-arrow.png&quot;); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="20.0" />
<Button fx:id="down" layoutX="524.4" layoutY="354.4" minHeight="25.0" minWidth="25.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url(&quot;down-arrow.png&quot;); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="50.0" />
<Button fx:id="left" layoutX="494.4" layoutY="354.4" minHeight="25.0" minWidth="25.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url(&quot;left-arrow.png&quot;); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="80.0" />
<TextField fx:id="inputCountBlocks" layoutX="446.0" layoutY="14.0" promptText="количество блоков" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="20.0" />
<ChoiceBox fx:id="choiceBoxStrategy" layoutX="430.0" layoutY="46.0" prefHeight="26.0" prefWidth="110.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="50.0" />
<Button layoutX="524.0" layoutY="83.0" mnemonicParsing="false" onAction="#ButtonStrategy_Click" text="шаг" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="80.0" />
<ChoiceBox fx:id="choiceBoxShape" layoutX="469.0" layoutY="116.0" prefHeight="26.0" prefWidth="110.0" AnchorPane.rightAnchor="20.599999999999994" AnchorPane.topAnchor="110.0" />
</children>
</AnchorPane>

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml"
fx:controller="com.example.aircraftcarrier.HelloController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
</padding>
<Label fx:id="welcomeText"/>
<Button text="Hello!" onAction="#onHelloButtonClick"/>
</VBox>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB