diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 9617294..fb82612 100644 --- a/pom.xml +++ b/pom.xml @@ -60,7 +60,7 @@ default-cli - com.example.aircraftcarrier/com.example.aircraftcarrier.HelloApplication + com.example.aircraftcarrier/AircraftCarrierApplication app app diff --git a/src/main/java/com/example/aircraftcarrier/HelloApplication.java b/src/main/java/com/example/aircraftcarrier/AircraftCarrierApplication.java similarity index 54% rename from src/main/java/com/example/aircraftcarrier/HelloApplication.java rename to src/main/java/com/example/aircraftcarrier/AircraftCarrierApplication.java index be0492f..049f7b4 100644 --- a/src/main/java/com/example/aircraftcarrier/HelloApplication.java +++ b/src/main/java/com/example/aircraftcarrier/AircraftCarrierApplication.java @@ -1,17 +1,19 @@ package com.example.aircraftcarrier; import javafx.application.Application; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.stage.Stage; import java.io.IOException; -public class HelloApplication extends Application { +public class AircraftCarrierApplication 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); + FXMLLoader fxmlLoader = new FXMLLoader(AircraftCarrierApplication.class.getResource("/com/example/aircraftcarrier/AircraftCarrierView.fxml")); + Scene scene = new Scene(fxmlLoader.load()); stage.setTitle("Hello!"); stage.setScene(scene); stage.show(); diff --git a/src/main/java/com/example/aircraftcarrier/AircraftCarrierController.java b/src/main/java/com/example/aircraftcarrier/AircraftCarrierController.java new file mode 100644 index 0000000..43358ed --- /dev/null +++ b/src/main/java/com/example/aircraftcarrier/AircraftCarrierController.java @@ -0,0 +1,109 @@ +package com.example.aircraftcarrier; + +import com.example.aircraftcarrier.Logic.Enum.DirectionType; +import com.example.aircraftcarrier.Logic.Drawing.DrawingAircraftCarrier; +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.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(); + DrawingAircraftCarrier drawingAircraftCarrier; + + @FXML + private AnchorPane Root; + @FXML + private Canvas canvas; + @FXML + private TextField inputCountBlocks; + @FXML + protected void buttonCreate_Click() { + drawingAircraftCarrier = new DrawingAircraftCarrier(); + int inputCountBlock; + + try { + inputCountBlock = Integer.parseInt(inputCountBlocks.getCharacters().toString()); + } catch (NumberFormatException ignored) { + inputCountBlock = 0; + } + + drawingAircraftCarrier.Init( + 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(), + inputCountBlock + ); + + 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()); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/aircraftcarrier/HelloController.java b/src/main/java/com/example/aircraftcarrier/HelloController.java deleted file mode 100644 index b21e401..0000000 --- a/src/main/java/com/example/aircraftcarrier/HelloController.java +++ /dev/null @@ -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!"); - } -} \ No newline at end of file diff --git a/src/main/java/com/example/aircraftcarrier/Logic/Drawing/DrawingAircraftCarrier.java b/src/main/java/com/example/aircraftcarrier/Logic/Drawing/DrawingAircraftCarrier.java new file mode 100644 index 0000000..babc02c --- /dev/null +++ b/src/main/java/com/example/aircraftcarrier/Logic/Drawing/DrawingAircraftCarrier.java @@ -0,0 +1,107 @@ +package com.example.aircraftcarrier.Logic.Drawing; + +import com.example.aircraftcarrier.Logic.Enum.BlockCount; +import com.example.aircraftcarrier.Logic.Enum.DirectionType; +import com.example.aircraftcarrier.Logic.Entity.EntityAircraftCarrier; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class DrawingAircraftCarrier { + public EntityAircraftCarrier aircraftCarrier; + public DrawingBlock drawingBlock; + private int x; + private int y; + private int widthCanvas; + private int heightCanvas; + private static final int widthPicture = 250; + private static final int heightPicture = 100; + + public void Init( + int speed, + int weight, + Color primaryColor, + Color secondaryColor, + boolean hasDeck, + boolean hasСabin, + int countBlock + ) { + aircraftCarrier = new EntityAircraftCarrier(); + aircraftCarrier.Init(speed, + weight, + primaryColor, + secondaryColor, + hasDeck, + hasСabin + ); + drawingBlock = new DrawingBlock(); + drawingBlock.setBlockCount(countBlock); + } + + public void setDrawingBlock(int count) { + drawingBlock.setBlockCount(count); + } + + public void setCanvasWidth(int width) { + if (x+widthPicture > width) return; + 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 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); + + if (aircraftCarrier.isHasDeck()) { + gc.setFill(aircraftCarrier.getPrimaryColor()); + gc.fillRect(x + 50, y + 40, 55, 20); + } + if (aircraftCarrier.isHasСabin()) { + gc.setFill(aircraftCarrier.getSecondaryColor()); + gc.fillRect(x + 105, y + 25, 30, 50); + } + + if (drawingBlock == null) return; + drawingBlock.Draw(gc,x,y,aircraftCarrier.getSecondaryColor()); + } + + public void Move(DirectionType direction) { + switch (direction) { + case Up: + if (y - aircraftCarrier.Step > 0) { + y -= aircraftCarrier.Step; + } + break; + case Down: + if (y + aircraftCarrier.Step + heightPicture < heightCanvas) { + y += aircraftCarrier.Step; + } + break; + case Right: + if (x + aircraftCarrier.Step + widthPicture < widthCanvas) { + x += aircraftCarrier.Step; + } + break; + case Left: + if (x - aircraftCarrier.Step > 0) { + x -= aircraftCarrier.Step; + } + break; + } + } +} diff --git a/src/main/java/com/example/aircraftcarrier/Logic/Drawing/DrawingBlock.java b/src/main/java/com/example/aircraftcarrier/Logic/Drawing/DrawingBlock.java new file mode 100644 index 0000000..54084e5 --- /dev/null +++ b/src/main/java/com/example/aircraftcarrier/Logic/Drawing/DrawingBlock.java @@ -0,0 +1,31 @@ +package com.example.aircraftcarrier.Logic.Drawing; + +import com.example.aircraftcarrier.Logic.Enum.BlockCount; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class DrawingBlock { + private BlockCount blockCount; + + 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; + } + } +} diff --git a/src/main/java/com/example/aircraftcarrier/Logic/Entity/EntityAircraftCarrier.java b/src/main/java/com/example/aircraftcarrier/Logic/Entity/EntityAircraftCarrier.java new file mode 100644 index 0000000..114c9c5 --- /dev/null +++ b/src/main/java/com/example/aircraftcarrier/Logic/Entity/EntityAircraftCarrier.java @@ -0,0 +1,55 @@ +package com.example.aircraftcarrier.Logic.Entity; + +import javafx.scene.paint.Color; + +public class EntityAircraftCarrier { + private int Speed; + private double Weight; + public int Step; + private Color PrimaryColor; + private Color SecondaryColor; + private boolean HasDeck; + private boolean HasСabin; + + + public void Init( + int speed, + double weight, + Color primaryColor, + Color secondaryColor, + boolean hasDeck, + boolean hasСabin + ) { + this.Speed = speed; + this.Weight = weight; + this.PrimaryColor = primaryColor; + this.SecondaryColor = secondaryColor; + this.HasDeck = hasDeck; + this.HasСabin = hasСabin; + this.Step = (int) (Speed * 100 / Weight); + } + + public int getSpeed() { + return Speed; + } + + public double getWeight() { + return Weight; + } + + public Color getPrimaryColor() { + return PrimaryColor; + } + + public Color getSecondaryColor() { + return SecondaryColor; + } + + public boolean isHasDeck() { + return HasDeck; + } + + public boolean isHasСabin() { + return HasСabin; + } +} diff --git a/src/main/java/com/example/aircraftcarrier/Logic/Enum/BlockCount.java b/src/main/java/com/example/aircraftcarrier/Logic/Enum/BlockCount.java new file mode 100644 index 0000000..ea47d99 --- /dev/null +++ b/src/main/java/com/example/aircraftcarrier/Logic/Enum/BlockCount.java @@ -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; + } +} diff --git a/src/main/java/com/example/aircraftcarrier/Logic/Enum/DirectionType.java b/src/main/java/com/example/aircraftcarrier/Logic/Enum/DirectionType.java new file mode 100644 index 0000000..425a393 --- /dev/null +++ b/src/main/java/com/example/aircraftcarrier/Logic/Enum/DirectionType.java @@ -0,0 +1,9 @@ +package com.example.aircraftcarrier.Logic.Enum; + +public enum DirectionType { + Unknown, + Up, + Down, + Right, + Left; +} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 280b348..9b57da2 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -5,4 +5,10 @@ module com.example.aircraftcarrier { opens com.example.aircraftcarrier to javafx.fxml; exports com.example.aircraftcarrier; + exports com.example.aircraftcarrier.Logic.Drawing; + opens com.example.aircraftcarrier.Logic.Drawing 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; } \ No newline at end of file diff --git a/src/main/resources/com/example/aircraftcarrier/AircraftCarrierView.fxml b/src/main/resources/com/example/aircraftcarrier/AircraftCarrierView.fxml new file mode 100644 index 0000000..313e711 --- /dev/null +++ b/src/main/resources/com/example/aircraftcarrier/AircraftCarrierView.fxml @@ -0,0 +1,17 @@ + + + + + + + + + +