lab1 v 0.1

This commit is contained in:
yu.shlyapin 2025-02-05 20:56:06 +04:00
parent f93a9045cd
commit 116675a1e3
16 changed files with 393 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 -->
<id>default-cli</id>
<configuration>
<mainClass>com.example.aircraftcarrier/com.example.aircraftcarrier.HelloApplication
<mainClass>com.example.aircraftcarrier/AircraftCarrierApplication
</mainClass>
<launcher>app</launcher>
<jlinkZipName>app</jlinkZipName>

View File

@ -7,11 +7,11 @@ 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();

View File

@ -0,0 +1,85 @@
package com.example.aircraftcarrier;
import javafx.event.ActionEvent;
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.input.KeyCode;
import javafx.scene.input.KeyEvent;
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 Canvas canvas;
@FXML
protected void buttonCreate_Click() {
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()
);
drawingAircraftCarrier.setCanvasWidth(canvas.getWidth(),canvas.getHeight());
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");
}
}

View File

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

View File

@ -0,0 +1,87 @@
package com.example.aircraftcarrier;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
public class DrawingAircraftCarrier {
public EntityAircraftCarrier aircraftCarrier;
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 DrawingAircraftCarrier(
int speed,
int weight,
Color primaryColor,
Color secondaryColor,
boolean hasDeck,
boolean hasСabin
) {
aircraftCarrier = new EntityAircraftCarrier(speed,
weight,
primaryColor,
secondaryColor,
hasDeck,
hasСabin);
}
public void setCanvasWidth(double width, double height) {
// if (x+widthPicture > widthCanvas || y+heightPicture > heightCanvas);
widthCanvas = (int)width;
heightCanvas = (int)height;
}
public void setPosition(int newX, int newY) {
if (x == 0 || y == 0) {
x = newX;
y = newY;
}
if (x < 0 || y < 0 || x+widthPicture > widthCanvas || y+heightPicture > heightCanvas) return;
x = newX;
y = newY;
}
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);
gc.setFill(aircraftCarrier.getPrimaryColor());
gc.fillRect(x+50,y+40,55,20);
gc.setFill(aircraftCarrier.getSecondaryColor());
gc.fillRect(x+105,y+25,30,50);
}
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;
}
}
}

View File

@ -0,0 +1,55 @@
package com.example.aircraftcarrier;
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 EntityAircraftCarrier(
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;
}
}

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,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.*?>
<?import javafx.scene.control.*?>
<?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">
<children>
<Canvas fx:id="canvas" height="400.0" width="600.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(&quot;up-arrow.png&quot;); -fx-background-size: stretch;" AnchorPane.bottomAnchor="60.0" AnchorPane.rightAnchor="60.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(&quot;right-arrow.png&quot;); -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(&quot;down-arrow.png&quot;); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="60.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(&quot;left-arrow.png&quot;); -fx-background-size: stretch;" AnchorPane.bottomAnchor="20.0" AnchorPane.rightAnchor="100.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