lab 3 prerelease

This commit is contained in:
yu.shlyapin 2025-02-17 18:59:24 +04:00
parent ca79a77693
commit c1cd71bd47
11 changed files with 140 additions and 48 deletions

View File

@ -40,7 +40,6 @@ public class AircraftCarrierController implements Initializable {
public void setDrawing(DrawingSimpleAircraftCarrier drawingAircraftCarrier) { public void setDrawing(DrawingSimpleAircraftCarrier drawingAircraftCarrier) {
this.drawingAircraftCarrier = drawingAircraftCarrier; this.drawingAircraftCarrier = drawingAircraftCarrier;
draw();
} }
@FXML @FXML
@ -62,14 +61,14 @@ public class AircraftCarrierController implements Initializable {
choiceBoxStrategy.setDisable(true); choiceBoxStrategy.setDisable(true);
strategy.MakeStep(); strategy.MakeStep();
drawingAircraftCarrier.Draw(canvas.getGraphicsContext2D()); draw();
if (strategy.GetStatus() == StrategyStatus.Finish) { if (strategy.GetStatus() == StrategyStatus.Finish) {
choiceBoxStrategy.setDisable(false); choiceBoxStrategy.setDisable(false);
strategy = null; strategy = null;
} }
} }
private void draw() { public void draw() {
canvas.getGraphicsContext2D().clearRect(0,0,canvas.getWidth(),canvas.getHeight()); canvas.getGraphicsContext2D().clearRect(0,0,canvas.getWidth(),canvas.getHeight());
drawingAircraftCarrier.Draw(canvas.getGraphicsContext2D()); drawingAircraftCarrier.Draw(canvas.getGraphicsContext2D());
} }

View File

@ -0,0 +1,38 @@
package com.example.aircraftcarrier;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import java.net.URL;
import java.util.ResourceBundle;
public class BlockDialogController implements Initializable {
@FXML
private ComboBox<String> ComboBoxShape;
private final String[] shapes = { "квадрат", "треугольник", "круг" };
@FXML
private TextField CountBlock;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
ComboBoxShape.getItems().setAll(shapes);
CountBlock.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observableValue, String s, String t1) {
if (t1.matches("\\d{0,2}")) CountBlock.setText(t1);
else CountBlock.setText(s);
}
});
}
public int getCount() {
return Integer.parseInt(CountBlock.getText());
}
public String getShape() {
return ComboBoxShape.getValue();
}
}

View File

@ -1,5 +1,8 @@
package com.example.aircraftcarrier.Logic.Aircraft; 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.Block.IDrawingBlock;
import com.example.aircraftcarrier.Logic.Entity.EntityAircraftCarrier; import com.example.aircraftcarrier.Logic.Entity.EntityAircraftCarrier;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
@ -16,7 +19,9 @@ public class DrawingAircraftCarrier extends DrawingSimpleAircraftCarrier {
Color primaryColor, Color primaryColor,
Color secondaryColor, Color secondaryColor,
boolean hasDeck, boolean hasDeck,
boolean hasСabin boolean hasСabin,
int countBlock,
String shape
) { ) {
super(); super();
aircraftCarrier = new EntityAircraftCarrier(speed, aircraftCarrier = new EntityAircraftCarrier(speed,
@ -25,6 +30,16 @@ public class DrawingAircraftCarrier extends DrawingSimpleAircraftCarrier {
secondaryColor, secondaryColor,
hasDeck, hasDeck,
hasСabin); 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;
}
} }
public void setDrawingBlock(IDrawingBlock iDrawingBlock) { public void setDrawingBlock(IDrawingBlock iDrawingBlock) {

View File

@ -7,6 +7,10 @@ import javafx.scene.paint.Color;
public class DrawingBlock implements IDrawingBlock { public class DrawingBlock implements IDrawingBlock {
private BlockCount blockCount; private BlockCount blockCount;
public DrawingBlock(int blockCountValue) {
this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue);
}
public void setBlockCount(int blockCountValue) { public void setBlockCount(int blockCountValue) {
this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue); this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue);
} }

View File

@ -7,7 +7,9 @@ import javafx.scene.paint.Color;
public class DrawingBlockCircle implements IDrawingBlock { public class DrawingBlockCircle implements IDrawingBlock {
private BlockCount blockCount; private BlockCount blockCount;
public DrawingBlockCircle() {} public DrawingBlockCircle(int blockCountValue) {
this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue);
}
public void setBlockCount(int blockCountValue) { public void setBlockCount(int blockCountValue) {
this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue); this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue);

View File

@ -7,7 +7,10 @@ import javafx.scene.paint.Color;
public class DrawingBlockTriangle implements IDrawingBlock { public class DrawingBlockTriangle implements IDrawingBlock {
private BlockCount blockCount; private BlockCount blockCount;
public DrawingBlockTriangle() {}
public DrawingBlockTriangle(int blockCountValue) {
this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue);
}
public void setBlockCount(int blockCountValue) { public void setBlockCount(int blockCountValue) {
this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue); this.blockCount = BlockCount.ConvertToBlockCount(blockCountValue);

View File

@ -29,7 +29,7 @@ public abstract class AbstractCompany {
} }
public static boolean remove(AbstractCompany company, int pos){ public static boolean remove(AbstractCompany company, int pos){
return company != null && company._collection != null && company._collection.Remove(pos); return company != null & company._collection != null & company._collection.Remove(pos);
} }
public DrawingSimpleAircraftCarrier GetRandom() { public DrawingSimpleAircraftCarrier GetRandom() {
@ -44,7 +44,7 @@ public abstract class AbstractCompany {
for (int i = 0; i < _collection.count(); i++) for (int i = 0; i < _collection.count(); i++)
{ {
DrawingSimpleAircraftCarrier obj = _collection.Get(i); DrawingSimpleAircraftCarrier obj = _collection.Get(i);
if (obj == null) return; if (obj == null) continue;
obj.Draw(gc); obj.Draw(gc);
} }
} }

View File

@ -44,10 +44,10 @@ public class DocksService extends AbstractCompany {
for (int i = 0; i < _collection.count(); i++) { for (int i = 0; i < _collection.count(); i++) {
DrawingSimpleAircraftCarrier obj = _collection.Get(i); DrawingSimpleAircraftCarrier obj = _collection.Get(i);
if (obj == null) { continue; } if (obj != null) {
System.out.println(i); obj.setCanvasParam(_pictureWidth,_pictureHeight);
obj.setCanvasParam(_pictureWidth,_pictureHeight); obj.setPosition(xpos * _placeSizeWidth + 10, (ypos-1) * _placeSizeHeight + 10);
obj.setPosition(xpos * _placeSizeWidth + 10, (ypos-1) * _placeSizeHeight + 10); }
xpos++; xpos++;
if (xpos >= maxXPositionCount) if (xpos >= maxXPositionCount)
{ {

View File

@ -3,6 +3,8 @@ package com.example.aircraftcarrier;
import com.example.aircraftcarrier.Logic.Aircraft.DrawingAircraftCarrier; import com.example.aircraftcarrier.Logic.Aircraft.DrawingAircraftCarrier;
import com.example.aircraftcarrier.Logic.Aircraft.DrawingSimpleAircraftCarrier; import com.example.aircraftcarrier.Logic.Aircraft.DrawingSimpleAircraftCarrier;
import com.example.aircraftcarrier.Logic.Block.DrawingBlock; 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.Block.IDrawingBlock;
import com.example.aircraftcarrier.Logic.CollectionGenericObjects.AbstractCompany; import com.example.aircraftcarrier.Logic.CollectionGenericObjects.AbstractCompany;
import com.example.aircraftcarrier.Logic.CollectionGenericObjects.DocksService; import com.example.aircraftcarrier.Logic.CollectionGenericObjects.DocksService;
@ -34,12 +36,8 @@ public class ShipCollectionController implements Initializable {
@FXML @FXML
private Canvas canvasCollection; private Canvas canvasCollection;
@FXML @FXML
private AnchorPane wrapperCanvas;
@FXML
private ComboBox<String> comboBoxStorage; private ComboBox<String> comboBoxStorage;
private final String[] storageText = new String[]{"Хранилище"}; private final String[] storageText = new String[]{"Хранилище"};
@FXML
private static ColorPicker asdd1;
@FXML @FXML
protected void ComboBox_Choice() { protected void ComboBox_Choice() {
@ -59,26 +57,18 @@ public class ShipCollectionController implements Initializable {
private static Color GetColor(Random random) { private static Color GetColor(Random random) {
Color c = Color.rgb(random.nextInt(256),random.nextInt(256),random.nextInt(256)); Color c = Color.rgb(random.nextInt(256),random.nextInt(256),random.nextInt(256));
// //TODO: доделать DialogPane dp = new DialogPane();
// asdd1.setVisible(false);
// asdd1.sho ColorPicker asd = new ColorPicker();
// ColorPicker colorPicker = new ColorPicker(c); asd.setValue(c);
// Button confirmButton = new Button("Установить");
// confirmButton.setAlignment(Pos.CENTER_RIGHT); dp.setContent(asd);
//
// HBox buttonBox = new HBox(confirmButton); dp.getButtonTypes().setAll(ButtonType.YES, ButtonType.NO);
// buttonBox.setAlignment(Pos.TOP_RIGHT); Dialog<ButtonType> dialog = new Dialog<>();
// buttonBox.setPadding(new Insets(10, 10, 10, 10)); dialog.setDialogPane(dp);
// VBox root = new VBox(colorPicker, buttonBox); dialog.showAndWait();
// if (dialog.getResult() == ButtonType.YES) { c = asd.getValue(); }
//
//
// Stage colorStage = new Stage();
// colorStage.setScene(new Scene(root));
//
// confirmButton.setOnAction(event -> colorStage.close());
//
// colorStage.showAndWait();
return c; return c;
} }
@ -101,24 +91,45 @@ public class ShipCollectionController implements Initializable {
@FXML @FXML
protected void ButtonCreate_Click() { protected void ButtonCreate_Click() {
if (_company == null) return; if (_company == null) return;
DrawingAircraftCarrier drawingAircraftCarrier;
drawingAircraftCarrier = new DrawingAircraftCarrier( int inputCount;
String shape;
try {
FXMLLoader dialogLoader = new FXMLLoader(
AircraftCarrierApplication.class.getResource(
"/com/example/aircraftcarrier/BlockDialogView.fxml"
)
);
DialogPane blockPane = dialogLoader.load();
BlockDialogController blockDialogController = dialogLoader.getController();
Dialog<Object> s = new Dialog<>();
s.setDialogPane(blockPane);
s.showAndWait();
shape = blockDialogController.getShape();
inputCount = blockDialogController.getCount();
} catch (Exception ignore) {
inputCount = 0;
shape = "";
}
DrawingAircraftCarrier drawingAircraftCarrier = new DrawingAircraftCarrier(
random.nextInt(100) + 100, random.nextInt(100) + 100,
random.nextInt(1000) + 1000, random.nextInt(1000) + 1000,
GetColor(random), GetColor(random),
GetColor(random), GetColor(random),
random.nextBoolean(), random.nextBoolean(),
random.nextBoolean() random.nextBoolean(),
inputCount,
shape
); );
IDrawingBlock imo = new DrawingBlock();
imo.setBlockCount(2);
drawingAircraftCarrier.setDrawingBlock(imo);
drawingAircraftCarrier.setCanvasWidth((int) canvasCollection.getWidth()); drawingAircraftCarrier.setCanvasWidth((int) canvasCollection.getWidth());
drawingAircraftCarrier.setCanvasHeight((int) canvasCollection.getHeight()); drawingAircraftCarrier.setCanvasHeight((int) canvasCollection.getHeight());
System.out.println( AbstractCompany.add(_company,drawingAircraftCarrier)); AbstractCompany.add(_company,drawingAircraftCarrier);
_company.Show(canvasCollection.getGraphicsContext2D()); _company.Show(canvasCollection.getGraphicsContext2D());
} }
@ -142,11 +153,12 @@ public class ShipCollectionController implements Initializable {
try { try {
AnchorPane Pane = fxmlLoader.load(); AnchorPane Pane = fxmlLoader.load();
AircraftCarrierController controller = fxmlLoader.getController(); AircraftCarrierController controller = fxmlLoader.getController();
controller.setDrawing(aircraft);
Stage stage = new Stage(); Stage stage = new Stage();
Scene scene = new Scene(Pane); Scene scene = new Scene(Pane);
stage.setScene(scene); stage.setScene(scene);
stage.showAndWait(); controller.setDrawing(aircraft);
stage.show();
controller.draw();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -186,8 +198,6 @@ public class ShipCollectionController implements Initializable {
@Override @Override
public void initialize(URL url, ResourceBundle resourceBundle) { public void initialize(URL url, ResourceBundle resourceBundle) {
comboBoxStorage.getItems().addAll(storageText); comboBoxStorage.getItems().addAll(storageText);
// canvasCollection.widthProperty().bind(wrapperCanvas.widthProperty());
// canvasCollection.heightProperty().bind(wrapperCanvas.heightProperty());
IdField.textProperty().addListener(new ChangeListener<String>() { IdField.textProperty().addListener(new ChangeListener<String>() {
@Override @Override
public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) { public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<DialogPane prefHeight="120.0" prefWidth="250.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.aircraftcarrier.BlockDialogController">
<content>
<VBox minHeight="150.0" minWidth="250.0" prefHeight="150.0" prefWidth="265.0">
<children>
<Label text="Форма блоков" />
<ComboBox minWidth="-Infinity" prefWidth="230.0" fx:id="ComboBoxShape" />
<Label text="Количество блоков" />
<TextField fx:id="CountBlock" />
</children>
</VBox>
</content>
<buttonTypes>
<ButtonType fx:constant="OK" />
<ButtonType fx:constant="CANCEL" />
</buttonTypes>
</DialogPane>

View File

@ -23,7 +23,7 @@
</BorderPane> </BorderPane>
</right> </right>
<center> <center>
<AnchorPane fx:id="wrapperCanvas" BorderPane.alignment="CENTER"> <AnchorPane BorderPane.alignment="CENTER">
<children> <children>
<Canvas fx:id="canvasCollection" height="600.0" width="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> <Canvas fx:id="canvasCollection" height="600.0" width="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children> </children>