lab1 release
This commit is contained in:
parent
116675a1e3
commit
d9d73ad660
@ -1,6 +1,8 @@
|
|||||||
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;
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
package com.example.aircraftcarrier;
|
package com.example.aircraftcarrier;
|
||||||
|
|
||||||
import javafx.event.ActionEvent;
|
import com.example.aircraftcarrier.Logic.Enum.DirectionType;
|
||||||
|
import com.example.aircraftcarrier.Logic.Drawing.DrawingAircraftCarrier;
|
||||||
import javafx.event.Event;
|
import javafx.event.Event;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.fxml.Initializable;
|
import javafx.fxml.Initializable;
|
||||||
import javafx.scene.canvas.Canvas;
|
import javafx.scene.canvas.Canvas;
|
||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.Button;
|
||||||
|
import javafx.scene.control.TextField;
|
||||||
import javafx.scene.input.KeyCode;
|
import javafx.scene.input.KeyCode;
|
||||||
import javafx.scene.input.KeyEvent;
|
import javafx.scene.input.KeyEvent;
|
||||||
|
import javafx.scene.layout.AnchorPane;
|
||||||
import javafx.scene.paint.Color;
|
import javafx.scene.paint.Color;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@ -19,13 +22,17 @@ public class AircraftCarrierController implements Initializable {
|
|||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
DrawingAircraftCarrier drawingAircraftCarrier;
|
DrawingAircraftCarrier drawingAircraftCarrier;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private AnchorPane Root;
|
||||||
@FXML
|
@FXML
|
||||||
private Canvas canvas;
|
private Canvas canvas;
|
||||||
@FXML
|
@FXML
|
||||||
|
private TextField inputCountBlocks;
|
||||||
|
@FXML
|
||||||
protected void buttonCreate_Click() {
|
protected void buttonCreate_Click() {
|
||||||
drawingAircraftCarrier = new DrawingAircraftCarrier(
|
drawingAircraftCarrier = new DrawingAircraftCarrier(
|
||||||
random.nextInt(100)+100,
|
random.nextInt(100) + 100,
|
||||||
random.nextInt(1000)+1000,
|
random.nextInt(1000) + 1000,
|
||||||
Color.rgb(
|
Color.rgb(
|
||||||
random.nextInt(256),
|
random.nextInt(256),
|
||||||
random.nextInt(256),
|
random.nextInt(256),
|
||||||
@ -39,12 +46,21 @@ public class AircraftCarrierController implements Initializable {
|
|||||||
random.nextBoolean(),
|
random.nextBoolean(),
|
||||||
random.nextBoolean()
|
random.nextBoolean()
|
||||||
);
|
);
|
||||||
drawingAircraftCarrier.setCanvasWidth(canvas.getWidth(),canvas.getHeight());
|
|
||||||
drawingAircraftCarrier.setPosition(
|
try {
|
||||||
random.nextInt(50)+10,
|
drawingAircraftCarrier.setDrawingBlock(Integer.parseInt(inputCountBlocks.getCharacters().toString()));
|
||||||
random.nextInt(50)+10
|
} catch (NumberFormatException ignored) {}
|
||||||
);
|
|
||||||
drawingAircraftCarrier.Draw(canvas.getGraphicsContext2D());
|
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
|
@FXML
|
||||||
@ -81,5 +97,7 @@ public class AircraftCarrierController implements Initializable {
|
|||||||
@Override
|
@Override
|
||||||
public void initialize(URL url, ResourceBundle resourceBundle) {
|
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||||||
System.out.println("start");
|
System.out.println("start");
|
||||||
|
canvas.widthProperty().bind(Root.widthProperty());
|
||||||
|
canvas.heightProperty().bind(Root.heightProperty());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,11 +1,14 @@
|
|||||||
package com.example.aircraftcarrier;
|
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.canvas.GraphicsContext;
|
||||||
import javafx.scene.paint.Color;
|
import javafx.scene.paint.Color;
|
||||||
import javafx.scene.paint.Paint;
|
|
||||||
|
|
||||||
public class DrawingAircraftCarrier {
|
public class DrawingAircraftCarrier {
|
||||||
public EntityAircraftCarrier aircraftCarrier;
|
public EntityAircraftCarrier aircraftCarrier;
|
||||||
|
public DrawingBlock drawingBlock;
|
||||||
private int x;
|
private int x;
|
||||||
private int y;
|
private int y;
|
||||||
private int widthCanvas;
|
private int widthCanvas;
|
||||||
@ -29,20 +32,26 @@ public class DrawingAircraftCarrier {
|
|||||||
hasСabin);
|
hasСabin);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCanvasWidth(double width, double height) {
|
public void setDrawingBlock(int count) {
|
||||||
// if (x+widthPicture > widthCanvas || y+heightPicture > heightCanvas);
|
drawingBlock = new DrawingBlock();
|
||||||
widthCanvas = (int)width;
|
drawingBlock.setBlockCount(count);
|
||||||
heightCanvas = (int)height;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPosition(int newX, int newY) {
|
public void setCanvasWidth(int width) {
|
||||||
if (x == 0 || y == 0) {
|
if (x+widthPicture > width);
|
||||||
x = newX;
|
widthCanvas = width;
|
||||||
y = newY;
|
}
|
||||||
}
|
|
||||||
if (x < 0 || y < 0 || x+widthPicture > widthCanvas || y+heightPicture > heightCanvas) return;
|
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;
|
x = newX;
|
||||||
y = newY;
|
y = newY;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Draw(GraphicsContext gc) {
|
public void Draw(GraphicsContext gc) {
|
||||||
@ -54,10 +63,17 @@ public class DrawingAircraftCarrier {
|
|||||||
|
|
||||||
gc.strokeOval(x+175,y+35,30,30);
|
gc.strokeOval(x+175,y+35,30,30);
|
||||||
|
|
||||||
gc.setFill(aircraftCarrier.getPrimaryColor());
|
if (aircraftCarrier.isHasDeck()) {
|
||||||
gc.fillRect(x+50,y+40,55,20);
|
gc.setFill(aircraftCarrier.getPrimaryColor());
|
||||||
gc.setFill(aircraftCarrier.getSecondaryColor());
|
gc.fillRect(x + 50, y + 40, 55, 20);
|
||||||
gc.fillRect(x+105,y+25,30,50);
|
}
|
||||||
|
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) {
|
public void Move(DirectionType direction) {
|
@ -0,0 +1,36 @@
|
|||||||
|
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 DrawingBlock() {}
|
||||||
|
public DrawingBlock(int blockCountValue) {
|
||||||
|
this.blockCount = BlockCount.GetBlockCount(blockCountValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBlockCount(int blockCountValue) {
|
||||||
|
this.blockCount = BlockCount.GetBlockCount(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.aircraftcarrier;
|
package com.example.aircraftcarrier.Logic.Entity;
|
||||||
|
|
||||||
import javafx.scene.paint.Color;
|
import javafx.scene.paint.Color;
|
||||||
|
|
@ -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 GetBlockCount(int count) {
|
||||||
|
return switch (count) {
|
||||||
|
case 2 -> Two;
|
||||||
|
case 4 -> Four;
|
||||||
|
case 6 -> Six;
|
||||||
|
default -> Other;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getCount() {
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.aircraftcarrier;
|
package com.example.aircraftcarrier.Logic.Enum;
|
||||||
|
|
||||||
public enum DirectionType {
|
public enum DirectionType {
|
||||||
Unknown,
|
Unknown,
|
@ -5,4 +5,10 @@ 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.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;
|
||||||
}
|
}
|
@ -4,13 +4,14 @@
|
|||||||
<?import javafx.scene.control.*?>
|
<?import javafx.scene.control.*?>
|
||||||
<?import javafx.scene.layout.*?>
|
<?import javafx.scene.layout.*?>
|
||||||
|
|
||||||
<AnchorPane 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">
|
<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>
|
<children>
|
||||||
<Canvas fx:id="canvas" height="400.0" width="600.0" />
|
<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="#buttonCreate_Click" text="Создать" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" />
|
<Button layoutX="14.0" layoutY="361.0" mnemonicParsing="false" onAction="#buttonCreate_Click" text="Создать" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" />
|
||||||
<Button fx:id="up" layoutX="436.0" layoutY="336.0" minHeight="35.0" minWidth="35.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url("up-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="60.0" AnchorPane.rightAnchor="60.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("up-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="50.0" AnchorPane.rightAnchor="50.0" />
|
||||||
<Button fx:id="right" layoutX="436.0" layoutY="336.0" minHeight="35.0" minWidth="35.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url("right-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="20.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("right-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="20.0" />
|
||||||
<Button fx:id="down" layoutX="436.0" layoutY="336.0" minHeight="35.0" minWidth="35.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url("down-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="60.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("down-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="50.0" />
|
||||||
<Button fx:id="left" layoutX="436.0" layoutY="336.0" minHeight="35.0" minWidth="35.0" mnemonicParsing="false" onAction="#MoveEvent" style="-fx-background-image: url("left-arrow.png"); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="100.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("left-arrow.png"); -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" />
|
||||||
</children>
|
</children>
|
||||||
</AnchorPane>
|
</AnchorPane>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user