LabWork3 PIbd-21 Zacharchenko #3
@ -121,8 +121,6 @@ public class ControllerBus {
|
||||
pictureBoxBus.widthProperty().addListener(listener);
|
||||
pictureBoxBus.heightProperty().addListener(listener);
|
||||
Random rnd = new Random();
|
||||
ColorPicker colorPicker = new ColorPicker();
|
||||
ObservableList<Color> a = colorPicker.getCustomColors();
|
||||
_bus = new DrawingDDB(rnd.nextInt(300), rnd.nextInt(2000),
|
||||
Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
|
||||
choiceDoors.getValue(),
|
||||
|
@ -0,0 +1,165 @@
|
||||
package com.example.doubledeckerbus;
|
||||
|
||||
import javafx.beans.InvalidationListener;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.canvas.Canvas;
|
||||
import javafx.scene.canvas.GraphicsContext;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import javafx.scene.paint.Color;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
|
||||
import static com.example.doubledeckerbus.ControllerMapWithSetBus._mapBusesCollectionGeneric;
|
||||
|
||||
public class ControllerPolymorph {
|
||||
protected DrawingBus _bus;
|
||||
protected DrawingPolymorphBus<EntityBus, IDrawingDoors> polymorphBus;
|
||||
public DrawingBus SelectedBus;
|
||||
protected ObservableList<Integer> countOfDoors = FXCollections.observableArrayList(3, 4, 5);
|
||||
|
||||
@FXML
|
||||
protected Button buttonCreate;
|
||||
|
||||
@FXML
|
||||
protected Canvas canvasBus;
|
||||
|
||||
@FXML
|
||||
protected AnchorPane pictureBoxBus;
|
||||
|
||||
@FXML
|
||||
protected Label statusColor;
|
||||
|
||||
@FXML
|
||||
protected Label statusSpeed;
|
||||
|
||||
@FXML
|
||||
protected Label statusWeight;
|
||||
|
||||
@FXML
|
||||
protected ChoiceBox<Integer> choiceDoors;
|
||||
|
||||
@FXML
|
||||
void ButtonCreate_Click(ActionEvent event) {
|
||||
pictureBoxBus.widthProperty().addListener(listener);
|
||||
pictureBoxBus.heightProperty().addListener(listener);
|
||||
|
||||
Random rnd = new Random();
|
||||
IDrawingDoors door = null;
|
||||
switch (rnd.nextInt(3)) {
|
||||
case (0) -> door = new DrawingDoors();
|
||||
case (1) -> door = new DrawingEllipsoidDoors();
|
||||
case (2) -> door = new DrawingTriangleDoors();
|
||||
}
|
||||
door.setCountOfDoors(choiceDoors.getValue());
|
||||
|
||||
EntityBus bus = new EntityBus(rnd.nextInt(300), rnd.nextInt(2000),
|
||||
Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
|
||||
|
||||
polymorphBus.AddDoors(door);
|
||||
polymorphBus.AddEntity(bus);
|
||||
|
||||
_bus = polymorphBus.CreateBus();
|
||||
SetData();
|
||||
BorderChanged();
|
||||
}
|
||||
|
||||
void SetData() {
|
||||
Random rnd = new Random();
|
||||
_bus.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), (int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight());
|
||||
statusSpeed.setText("Скорость: %s".formatted(_bus.Bus.Speed));
|
||||
statusWeight.setText("Вес: %s".formatted(_bus.Bus.Weight));
|
||||
statusColor.setText("Цвет: %s".formatted(_bus.Bus.BodyColor));
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void initialize() {
|
||||
polymorphBus = new DrawingPolymorphBus<>(100, 100);
|
||||
choiceDoors.setItems(countOfDoors);
|
||||
choiceDoors.setValue(3);
|
||||
|
||||
}
|
||||
InvalidationListener listener = o -> BorderChanged();
|
||||
|
||||
private void Draw()
|
||||
{
|
||||
GraphicsContext gc = canvasBus.getGraphicsContext2D();
|
||||
gc.setFill(Color.WHITE);
|
||||
gc.fillRect(0, 0, pictureBoxBus.getWidth(), pictureBoxBus.getHeight());
|
||||
_bus.DrawTransport(gc);
|
||||
}
|
||||
|
||||
void BorderChanged() {
|
||||
canvasBus.setWidth(pictureBoxBus.getWidth());
|
||||
canvasBus.setHeight(pictureBoxBus.getHeight());
|
||||
_bus.ChangeBorders((int) pictureBoxBus.getWidth(), (int) pictureBoxBus.getHeight());
|
||||
Draw();
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void ButtonCreateExtra_Click(ActionEvent event) {
|
||||
pictureBoxBus.widthProperty().addListener(listener);
|
||||
pictureBoxBus.heightProperty().addListener(listener);
|
||||
Random rnd = new Random();
|
||||
IDrawingDoors door = null;
|
||||
switch (rnd.nextInt(3)) {
|
||||
case (0) -> door = new DrawingDoors();
|
||||
case (1) -> door = new DrawingEllipsoidDoors();
|
||||
case (2) -> door = new DrawingTriangleDoors();
|
||||
}
|
||||
door.setCountOfDoors(choiceDoors.getValue());
|
||||
|
||||
EntityDDB ddb_bus = new EntityDDB(rnd.nextInt(300), rnd.nextInt(2000),
|
||||
Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
|
||||
Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
|
||||
rnd.nextBoolean(), rnd.nextBoolean());
|
||||
|
||||
polymorphBus.AddDoors(door);
|
||||
polymorphBus.AddEntity(ddb_bus);
|
||||
|
||||
_bus = polymorphBus.CreateBus();
|
||||
SetData();
|
||||
BorderChanged();
|
||||
}
|
||||
@FXML
|
||||
private void ButtonSelectBus_Click(ActionEvent event) throws IOException {
|
||||
SelectedBus = _bus;
|
||||
|
||||
if (SelectedBus == null) {
|
||||
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
||||
alert.setTitle("SelectBus");
|
||||
alert.setContentText("Вы не создали объект");
|
||||
Optional<ButtonType> option = alert.showAndWait();
|
||||
}
|
||||
else {
|
||||
|
||||
DrawingObjectBus bus = new DrawingObjectBus(SelectedBus);
|
||||
|
||||
if (_mapBusesCollectionGeneric.add(bus) != -1) {
|
||||
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
||||
alert.setTitle("SelectBus");
|
||||
alert.setContentText("Вы создали объект");
|
||||
Optional<ButtonType> option = alert.showAndWait();
|
||||
} else {
|
||||
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
||||
alert.setTitle("SelectBus");
|
||||
alert.setContentText("Не удалось добавить объект");
|
||||
Optional<ButtonType> option = alert.showAndWait();
|
||||
}
|
||||
}
|
||||
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormMapWithSetBus.fxml"));
|
||||
Scene scene = new Scene(fxmlLoader.load());
|
||||
Form.myStage.setTitle("DoubleDeckerBus");
|
||||
Form.myStage.setScene(scene);
|
||||
Form.myStage.show();
|
||||
|
||||
}
|
||||
}
|
@ -30,7 +30,15 @@ public class DrawingBus {
|
||||
case 2 -> Doors = new DrawingEllipsoidDoors();
|
||||
}
|
||||
Doors.setCountOfDoors(countOfDoors);
|
||||
}
|
||||
|
||||
public DrawingBus(int speed, float weight, Color bodyColor) {
|
||||
Bus = new EntityBus(speed, weight, bodyColor);
|
||||
}
|
||||
|
||||
public DrawingBus(EntityBus bus, IDrawingDoors doors) {
|
||||
Bus = bus;
|
||||
Doors = doors;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y, int width, int height) {
|
||||
|
@ -9,6 +9,15 @@ public class DrawingDDB extends DrawingBus{
|
||||
Bus = new EntityDDB(speed, weight, bodyColor, extraColor, ladder, secondStage);
|
||||
}
|
||||
|
||||
public DrawingDDB(int speed, float weight, Color bodyColor, Color extraColor, boolean ladder, boolean secondStage) {
|
||||
super(speed, weight, bodyColor);
|
||||
Bus = new EntityDDB(speed, weight, bodyColor, extraColor, ladder, secondStage);
|
||||
}
|
||||
|
||||
public DrawingDDB(EntityBus bus, IDrawingDoors doors) {
|
||||
super(bus, doors);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawTransport(GraphicsContext gc)
|
||||
{
|
||||
|
@ -0,0 +1,45 @@
|
||||
package com.example.doubledeckerbus;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingPolymorphBus<T extends EntityBus, U extends IDrawingDoors> {
|
||||
Object[] buses;
|
||||
|
||||
Object[] doors;
|
||||
eegov
commented
Нужно использовать параметр, а не конкретный тип Нужно использовать параметр, а не конкретный тип
|
||||
|
||||
int busesCount = 0;
|
||||
int doorsCount = 0;
|
||||
|
||||
public DrawingPolymorphBus(int busesCount, int doorsCount) {
|
||||
buses = new Object[busesCount];
|
||||
doors = new Object[doorsCount];
|
||||
}
|
||||
|
||||
public int AddEntity(T boat)
|
||||
{
|
||||
if(busesCount < buses.length){
|
||||
buses[busesCount] = boat;
|
||||
return busesCount++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int AddDoors(U paddle)
|
||||
{
|
||||
if(doorsCount < doors.length){
|
||||
doors[doorsCount]=paddle;
|
||||
return doorsCount++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public DrawingBus CreateBus() {
|
||||
int idBus = new Random().nextInt(busesCount);
|
||||
int idDoor = new Random().nextInt(doorsCount);
|
||||
T selectedBus = (T)buses[idBus];
|
||||
U selectedDoors = (U)doors[idDoor];
|
||||
if (selectedBus instanceof EntityDDB) {
|
||||
return new DrawingDDB(selectedBus, selectedDoors);
|
||||
}
|
||||
return new DrawingBus(selectedBus, selectedDoors);
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ public class Form extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws IOException {
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormMapWithSetBus.fxml"));
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(Form.class.getResource("FormPolymorph.fxml"));
|
||||
Scene scene = new Scene(fxmlLoader.load());
|
||||
myStage = stage;
|
||||
myStage.setTitle("DoubleDeckerBus");
|
||||
|
@ -4,6 +4,7 @@ module com.example.doubledeckerbus {
|
||||
|
||||
requires org.controlsfx.controls;
|
||||
requires org.kordamp.bootstrapfx.core;
|
||||
requires javafx.graphics;
|
||||
|
||||
opens com.example.doubledeckerbus to javafx.fxml;
|
||||
exports com.example.doubledeckerbus;
|
||||
|
@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import javafx.scene.canvas.Canvas?>
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.ChoiceBox?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
<?import javafx.scene.layout.ColumnConstraints?>
|
||||
<?import javafx.scene.layout.GridPane?>
|
||||
<?import javafx.scene.layout.HBox?>
|
||||
<?import javafx.scene.layout.RowConstraints?>
|
||||
|
||||
<GridPane alignment="CENTER" prefHeight="691.0" prefWidth="1041.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.doubledeckerbus.ControllerPolymorph">
|
||||
<columnConstraints>
|
||||
<ColumnConstraints hgrow="ALWAYS" minWidth="10.0" percentWidth="100.0" />
|
||||
</columnConstraints>
|
||||
<rowConstraints>
|
||||
<RowConstraints fillHeight="false" maxHeight="1.7976931348623157E308" percentHeight="90.0" vgrow="ALWAYS" />
|
||||
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="-Infinity" percentHeight="10.0" prefHeight="31.0" vgrow="NEVER" />
|
||||
</rowConstraints>
|
||||
<children>
|
||||
<HBox alignment="CENTER_LEFT" prefHeight="57.0" prefWidth="777.0" spacing="20.0" GridPane.rowIndex="1">
|
||||
<children>
|
||||
<Label fx:id="statusSpeed" text="Скорость:" />
|
||||
<Label fx:id="statusWeight" text="Вес:" />
|
||||
<Label fx:id="statusColor" text="Цвет:" />
|
||||
<Label text="Колл-во дверей:" />
|
||||
<ChoiceBox fx:id="choiceDoors" prefHeight="24.0" prefWidth="58.0" />
|
||||
</children>
|
||||
</HBox>
|
||||
<AnchorPane fx:id="pictureBoxBus" maxHeight="2000000.0" maxWidth="2000000.0" minHeight="0.0" minWidth="0.0" prefHeight="603.0" prefWidth="1020.0">
|
||||
<children>
|
||||
<Canvas fx:id="canvasBus" layoutY="38.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" />
|
||||
<Button fx:id="buttonCreate" layoutX="6.0" layoutY="565.0" mnemonicParsing="false" onAction="#ButtonCreate_Click" text="Создать" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="6.0" />
|
||||
<Button fx:id="buttonCreateExtra" layoutX="90.0" layoutY="565.0" mnemonicParsing="false" onAction="#ButtonCreateExtra_Click" text="Продвинутый объект" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="90.0" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
</children>
|
||||
<padding>
|
||||
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
|
||||
</padding>
|
||||
</GridPane>
|
Loading…
Reference in New Issue
Block a user
Нужно использовать параметр, а не конкретный тип