Лабараторная работа №3(начало)

This commit is contained in:
artur-kalimullin 2024-05-22 01:05:19 +04:00
parent 53693d9d08
commit 021b810748
14 changed files with 60 additions and 328 deletions

View File

@ -7,7 +7,7 @@ import java.awt.*;
import java.util.Random;
public class DrawningAirCraft {
private final EntityAirCraft entityAirCraft;
public Entities.EntityAirCraft entityAirCraft;
public EntityAirCraft getEntityAirCraft() {
return entityAirCraft;
@ -20,11 +20,11 @@ public class DrawningAirCraft {
private int _drawingAirCraftWidth = 66;
private int _drawingAirCraftHeight = 74;
public int GetPosX() {
public Integer GetPosX() {
return _startPosX;
}
public int GetPosY() {
public Integer GetPosY() {
return _startPosY;
}
@ -36,7 +36,7 @@ public class DrawningAirCraft {
return _drawingAirCraftHeight;
}
private IDrawEngine drawEngine;
public IDrawEngine drawEngine;
public DrawningAirCraft(int speed, float weight, Color bodyColor, int engineType) {
entityAirCraft = new EntityAirCraft(speed, weight, bodyColor);
@ -68,6 +68,11 @@ public class DrawningAirCraft {
_drawingAirCraftWidth = aircraftWidth;
}
public DrawningAirCraft(EntityAirCraft _entityAirCraft, IDrawEngine _drawEngine) {
entityAirCraft = _entityAirCraft;
drawEngine = _drawEngine;
}
public void setPosition(int x, int y) {
if (_pictureHeight == null || _pictureWidth == null)
return;

View File

@ -5,15 +5,16 @@ import Entities.EntityAirFighter;
import java.awt.*;
public class DrawningAirFighter extends DrawningAirCraft {
private EntityAirFighter entityAirFighter;
public DrawningAirFighter(int speed, float weight, Color bodyColor, int engineType, Color additionalColor, boolean pgo, boolean rockets) {
super(speed, weight, bodyColor, engineType, 76, 80);
entityAirFighter = new EntityAirFighter(speed, weight, bodyColor, additionalColor, pgo, rockets);
entityAirCraft = new EntityAirFighter(speed, weight, bodyColor, additionalColor, pgo, rockets);
}
public DrawningAirFighter(EntityAirFighter entity, IDrawEngine engine) {
super(entity, engine);
}
public void drawAirCraft(Graphics g) {
if (entityAirFighter == null || _startPosX == null || _startPosY == null) {
if (entityAirCraft == null|| !(entityAirCraft instanceof EntityAirFighter entityAirFighter) || _startPosX == null || _startPosY == null) {
return;
}
super.drawAirCraft(g);

View File

@ -35,4 +35,10 @@ public class DrawningEngine implements IDrawEngine {
g2d.drawRect(_startX + 30, _startY + 17, 10, 2 );
}
}
@Override
public String toString() {
String buffer = "";
buffer += "Тип: обычные, Количество двигателей: " + _engineCount.getEnumNumber();
return buffer;
}
}

View File

@ -35,4 +35,10 @@ public class DrawningLongEngine implements IDrawEngine {
g2d.drawRoundRect(_startX + 25, _startY + 17, 15, 2, 5, 5);
}
}
@Override
public String toString() {
String buffer = "";
buffer += "Тип: длиные, Количество двигателей: " + _engineCount.getEnumNumber();
return buffer;
}
}

View File

@ -35,4 +35,10 @@ public class DrawningOvalEngine implements IDrawEngine {
g2d.drawOval(_startX + 30, _startY + 17, 10, 2);
}
}
@Override
public String toString() {
String buffer = "";
buffer += "Тип: овальные, Количество двигателей: " + _engineCount.getEnumNumber();
return buffer;
}
}

View File

@ -25,4 +25,14 @@ public class EntityAirCraft {
Weight = weight;
BodyColor = bodyColor;
}
@Override
public String toString() {
String buffer = "";
buffer += "Скорость: " + this.Speed;
buffer += ", Вес: " + this.Weight;
buffer += String.format(", Основной цвет : RGB{%d, %d, %d}",
this.BodyColor.getRed(), this.BodyColor.getGreen(), this.BodyColor.getBlue());
return buffer;
}
}

View File

@ -21,4 +21,14 @@ public class EntityAirFighter extends EntityAirCraft {
Pgo = pgo;
Rockets = rockets;
}
@Override
public String toString() {
String buffer = super.toString();
buffer += String.format(", Дополнительный цвет : RGB{%d, %d, %d}",
this.AdditionalColor.getRed(), this.AdditionalColor.getGreen(), this.AdditionalColor.getBlue());
buffer += ", ПГО: " + this.Pgo;
buffer += ", Ракеты: " + this.Rockets;
return buffer;
}
}

View File

@ -1,131 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormAirFighter">
<grid id="27dc6" binding="PanelWrapper" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="599" height="476"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="1ff9a" binding="PictureBox" layout-manager="GridLayoutManager" row-count="6" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<toolTipText value=""/>
</properties>
<border type="none"/>
<children>
<vspacer id="7b40c">
<constraints>
<grid row="2" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false">
<preferred-size width="88" height="14"/>
</grid>
</constraints>
</vspacer>
<hspacer id="e0b21">
<constraints>
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="a8c2" class="javax.swing.JButton" binding="buttonCreateAirFighter">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="150" height="30"/>
<preferred-size width="150" height="30"/>
<maximum-size width="150" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать истребитель"/>
</properties>
</component>
<component id="79b3e" class="javax.swing.JButton" binding="buttonCreateAirCraft">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="150" height="30"/>
<preferred-size width="150" height="30"/>
<maximum-size width="150" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать самолёт"/>
</properties>
</component>
<component id="fe25b" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="5" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="arrowLeft.png"/>
<text value=""/>
</properties>
</component>
<component id="3eabd" class="javax.swing.JButton" binding="buttonStrategyStep">
<constraints>
<grid row="1" column="3" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Шаг"/>
</properties>
</component>
<component id="86d84" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="5" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="arrowRight.png"/>
<text value=""/>
</properties>
</component>
<component id="3dc0" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="5" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="arrowDown.png"/>
<text value=""/>
</properties>
</component>
<component id="ccb2a" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="4" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="arrowUp.png"/>
<text value=""/>
</properties>
</component>
<component id="988c8" class="javax.swing.JComboBox" binding="comboBoxStrategy">
<constraints>
<grid row="0" column="2" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="9" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<actionCommand value=""/>
<doubleBuffered value="false"/>
<toolTipText value=""/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -1,176 +0,0 @@
import Drawnings.DrawningAirCraft;
import Drawnings.DrawningAirFighter;
import MovementStrategy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Random;
import java.util.List;
public class FormAirFighter extends JFrame {
protected DrawningAirCraft _drawningAirCraft;
JPanel PanelWrapper;
private JPanel PictureBox;
private JButton buttonCreateAirFighter;
private JButton buttonCreateAirCraft;
private JButton buttonRight;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonUp;
private JComboBox comboBoxStrategy;
private JButton buttonStrategyStep;
private AbstractStrategy _strategy;
private List<JComponent> controls;
private void createObject(String type) {
Random random = new Random();
switch (type) {
case "Drawnings.DrawningAirCraft":
_drawningAirCraft = new DrawningAirCraft(random.nextInt(30,100), random.nextInt(100,500),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3));
break;
case "Drawnings.DrawningAirFighter":
_drawningAirCraft = new DrawningAirFighter(random.nextInt(30,100), random.nextInt(100,500),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean());
break;
default:
return;
}
_drawningAirCraft.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight());
_drawningAirCraft.setPosition(random.nextInt(25, 100),
random.nextInt(25, 100));
_strategy = null;
comboBoxStrategy.setEnabled(true);
Draw();
}
public FormAirFighter() {
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
buttonLeft.setName("buttonLeft");
buttonRight.setName("buttonRight");
InitializeControlsRepaintList();
buttonCreateAirFighter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createObject("Drawnings.DrawningAirFighter");
}
});
buttonCreateAirCraft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createObject("Drawnings.DrawningAirCraft");
}
});
ActionListener buttonMoveClickedListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String buttonName = ((JButton) e.getSource()).getName();
boolean result = false;
switch (buttonName) {
case "buttonUp": {
result = _drawningAirCraft.moveTransport(MovementDirection.Up);
}
break;
case "buttonDown": {
result = _drawningAirCraft.moveTransport(MovementDirection.Down);
}
break;
case "buttonLeft": {
result = _drawningAirCraft.moveTransport(MovementDirection.Left);
}
break;
case "buttonRight": {
result = _drawningAirCraft.moveTransport(MovementDirection.Right);
}
break;
}
if (result)
Draw();
}
};
buttonRight.addActionListener(buttonMoveClickedListener);
buttonDown.addActionListener(buttonMoveClickedListener);
buttonLeft.addActionListener(buttonMoveClickedListener);
buttonUp.addActionListener(buttonMoveClickedListener);
comboBoxStrategy.addItem("К центру");
comboBoxStrategy.addItem("К краю");
buttonStrategyStep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_drawningAirCraft == null)
return;
if (comboBoxStrategy.isEnabled()) {
switch (comboBoxStrategy.getSelectedIndex()) {
case 0:
_strategy = new MoveToCenter();
break;
case 1:
_strategy = new MoveToBorder();
break;
default:
_strategy = null;
break;
}
if (_strategy == null) {
return;
}
_strategy.SetData(new MoveableAirCraft(_drawningAirCraft), PictureBox.getWidth(), PictureBox.getHeight());
}
if (_strategy == null) {
return;
}
_strategy.MakeStep();
Draw();
comboBoxStrategy.setEnabled(false);
if (_strategy.GetStatus() == StrategyStatus.Finish) {
comboBoxStrategy.setEnabled(true);
_strategy = null;
}
}
});
}
private void Draw() {
if (_drawningAirCraft.getEntityAirCraft() == null)
return;
if (PictureBox.getWidth() == 0 || PictureBox.getHeight() == 0) {
return;
}
Graphics g = PictureBox.getGraphics();
g.setColor(PictureBox.getBackground());
g.fillRect(0,0, PictureBox.getWidth(), PictureBox.getHeight());
_drawningAirCraft.drawAirCraft(g);
RepaintControls();
}
private void RepaintControls() {
for (JComponent control : controls) {
control.repaint();
}
}
private void InitializeControlsRepaintList() {
controls = new LinkedList<>();
controls.add(buttonCreateAirFighter);
controls.add(buttonCreateAirCraft);
controls.add(buttonUp);
controls.add(buttonDown);
controls.add(buttonLeft);
controls.add(buttonRight);
controls.add(comboBoxStrategy);
controls.add(buttonStrategyStep);
}
}

View File

@ -1,13 +1,7 @@
import javax.swing.*;
import Forms.FormAirCraftCollection;
public class Main {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(false);
JFrame frame = new JFrame("Истребитель");
frame.setContentPane(new FormAirFighter().PanelWrapper);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200);
frame.pack();
frame.setSize(700, 500);
frame.setVisible(true);
FormAirCraftCollection formAirCraftCollection = new FormAirCraftCollection();
formAirCraftCollection.OpenFrame();
}
}

View File

@ -1,18 +1,18 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy {
@Override
protected boolean IsTargetDestination()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return objParams.RightBorder() <= FieldWidth &&
objParams.RightBorder() + GetStep() >= FieldWidth &&
objParams.DownBorder() <= FieldHeight &&
return objParams.RightBorder() + GetStep() >= FieldWidth &&
objParams.DownBorder() + GetStep() >= FieldHeight;
}
@Override
protected void MoveToTarget()
{
var objParams = GetObjectParameters();

View File

@ -1,6 +1,7 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy {
@Override
protected boolean IsTargetDestination()
{
var objParams = GetObjectParameters();
@ -12,9 +13,9 @@ public class MoveToCenter extends AbstractStrategy {
objParams.ObjectMiddleVertical() - GetStep() <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2);
}
@Override
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return;