Port from base project

This commit is contained in:
ShabOl 2023-11-28 22:48:37 +04:00
parent e89ec6c1d7
commit 618f1926d6
14 changed files with 556 additions and 128 deletions

View File

@ -1,38 +0,0 @@
package AirBomber;
import java.awt.Color;
public class BomberEntity
{
private int Speed;
private double Weight;
private Color BodyColor;
private Color AdditionalColor;
private boolean Bombs;
private boolean FuelTanks;
public double Step;
public void Init(int Speed, double Weight, Color BodyColor, Color AdditionalColor, boolean FuelTanks, boolean Bombs)
{
this.Speed = Speed;
this.Weight = Weight;
this.BodyColor = BodyColor;
this.AdditionalColor = AdditionalColor;
this.FuelTanks = FuelTanks;
this.Bombs = Bombs;
this.Step = (double)Speed * 100 / Weight * 5 / 2;
}
public int GetSpeed() { return Speed; }
public double GetWeight() { return Weight; }
public Color GetBodyColor() { return BodyColor; }
public Color GetAdditionalColor() { return AdditionalColor; }
public boolean GetBombs() { return Bombs; }
public boolean GetFuelTanks() { return FuelTanks; }
}

View File

@ -1,7 +1,8 @@
package AirBomber; package AirBomber;
import AirBomber.MovementStrategy.*;
import AirBomber.Rendering.*;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@ -9,7 +10,8 @@ import java.util.Random;
public class BomberForm extends JFrame public class BomberForm extends JFrame
{ {
private BomberRenderer _bomberRenderer; private BomberRendererBase _bomberRenderer;
private AbstractStrategy _strategy;
public BomberForm() public BomberForm()
{ {
@ -32,12 +34,11 @@ public class BomberForm extends JFrame
BomberPictureBox.setIcon(new ImageIcon(bmp)); BomberPictureBox.setIcon(new ImageIcon(bmp));
} }
private void ButtonCreate_Click(ActionEvent e) private void ButtonCreateBomber_Click(ActionEvent e)
{ {
Random random = new Random(); Random random = new Random();
_bomberRenderer = new BomberRenderer();
_bomberRenderer.Init( _bomberRenderer = new BomberRenderer(
random.nextInt(100, 300), random.nextInt(100, 300),
random.nextInt(1000, 3000), random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
@ -47,11 +48,75 @@ public class BomberForm extends JFrame
BomberPictureBox.getWidth(), BomberPictureBox.getWidth(),
BomberPictureBox.getHeight() BomberPictureBox.getHeight()
); );
_bomberRenderer.SetPosition(random.nextInt(20, 100), random.nextInt(20, 100)); _bomberRenderer.SetPosition(random.nextInt(10, 60), random.nextInt(10, 60));
MovementStrategyComboBox.setEnabled(true);
Draw(); Draw();
} }
private void ButtonCreateBomberBase_Click(ActionEvent e)
{
Random random = new Random();
_bomberRenderer = new BomberRendererBase(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
BomberPictureBox.getWidth(),
BomberPictureBox.getHeight()
);
_bomberRenderer.SetPosition(random.nextInt(10, 60), random.nextInt(10, 60));
MovementStrategyComboBox.setEnabled(true);
Draw();
}
private void ButtonPerformStep_Click(ActionEvent e)
{
if (_bomberRenderer == null)
return;
if (MovementStrategyComboBox.isEnabled())
{
switch (MovementStrategyComboBox.getSelectedIndex())
{
case 0:
_strategy = new MoveToCenterStrategy();
break;
case 1:
_strategy = new MoveToCenterStrategy();
break;
default:
_strategy = null;
}
if (_strategy == null)
return;
_strategy.SetData(
new ObjectEntityRenderer(_bomberRenderer),
BomberPictureBox.getWidth(),
BomberPictureBox.getHeight()
);
MovementStrategyComboBox.setEnabled(false);
}
if (_strategy == null)
return;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == Status.Finish)
{
MovementStrategyComboBox.setEnabled(true);
_strategy = null;
}
}
private void ButtonMove_Click(ActionEvent e) private void ButtonMove_Click(ActionEvent e)
{ {
if (_bomberRenderer == null) if (_bomberRenderer == null)
@ -84,25 +149,28 @@ public class BomberForm extends JFrame
private void InitializeComponent() private void InitializeComponent()
{ {
BomberPictureBox = new JLabel(); BomberPictureBox = new JLabel();
CreateButton = new JButton(); ButtonCreateBomberBase = new JButton();
ButtonRight = new JButton(); ButtonRight = new JButton();
ButtonDown = new JButton(); ButtonDown = new JButton();
ButtonLeft = new JButton(); ButtonLeft = new JButton();
ButtonUp = new JButton(); ButtonUp = new JButton();
MovementStrategyComboBox = new JComboBox<String>();
ButtonCreateBomber = new JButton();
ButtonPerformMove = new JButton();
// //
// BomberPictureBox // BomberPictureBox
// //
BomberPictureBox.setBounds(0, 0, 884, 461); BomberPictureBox.setBounds(0, 0, 884, 461);
// //
// CreateButton // ButtonCreateBomberBase
// //
CreateButton.setName("CreateButton"); ButtonCreateBomberBase.setName("ButtonCreateBomberBase");
CreateButton.setBounds(12, 419, 80, 30); ButtonCreateBomberBase.setBounds(177, 407, 159, 42);
CreateButton.setText("Создать"); ButtonCreateBomberBase.setText("Создать бомбардировщик");
CreateButton.setBackground(new Color(225, 225, 225)); ButtonCreateBomberBase.setBackground(new Color(225, 225, 225));
CreateButton.setFont(new Font("Segoe UI", Font.PLAIN, 11)); ButtonCreateBomberBase.setFont(new Font("Segoe UI", Font.PLAIN, 11));
CreateButton.setFocusable(false); ButtonCreateBomberBase.setFocusable(false);
CreateButton.addActionListener(e -> ButtonCreate_Click(e)); ButtonCreateBomberBase.addActionListener(e -> ButtonCreateBomberBase_Click(e));
// //
// ButtonRight // ButtonRight
// //
@ -144,6 +212,32 @@ public class BomberForm extends JFrame
ButtonUp.setIcon(new ImageIcon("src/AirBomber/Resources/ArrowUp.png")); ButtonUp.setIcon(new ImageIcon("src/AirBomber/Resources/ArrowUp.png"));
ButtonUp.addActionListener(e -> ButtonMove_Click(e)); ButtonUp.addActionListener(e -> ButtonMove_Click(e));
// //
// MovementStrategyComboBox
//
MovementStrategyComboBox.setBounds(706, 12, 166, 23);
MovementStrategyComboBox.addItem("Перемещать в центр");
MovementStrategyComboBox.addItem("Перемещать вправо вниз");
//
// ButtonCreateBomber
//
ButtonCreateBomber.setName("ButtonCreateBomber");
ButtonCreateBomber.setBounds(12, 407, 159, 42);
ButtonCreateBomber.setText("Создать продвинутый бомбардировщик");
ButtonCreateBomber.setBackground(new Color(225, 225, 225));
ButtonCreateBomber.setFont(new Font("Segoe UI", Font.PLAIN, 11));
ButtonCreateBomber.setFocusable(false);
ButtonCreateBomber.addActionListener(e -> ButtonCreateBomber_Click(e));
//
// ButtonPerformMove
//
ButtonPerformMove.setName("ButtonPerformMove");
ButtonPerformMove.setBounds(706, 50, 166, 31);
ButtonPerformMove.setText("Сделать шаг");
ButtonPerformMove.setBackground(new Color(225, 225, 225));
ButtonPerformMove.setFont(new Font("Segoe UI", Font.PLAIN, 11));
ButtonPerformMove.setFocusable(false);
ButtonPerformMove.addActionListener(e -> ButtonPerformStep_Click(e));
//
// BomberForm // BomberForm
// //
setTitle("Бомбардировщик"); setTitle("Бомбардировщик");
@ -152,18 +246,24 @@ public class BomberForm extends JFrame
setLayout(null); setLayout(null);
setLocationRelativeTo(null); setLocationRelativeTo(null);
setVisible(true); setVisible(true);
add(ButtonPerformMove);
add(ButtonCreateBomber);
add(MovementStrategyComboBox);
add(ButtonUp); add(ButtonUp);
add(ButtonLeft); add(ButtonLeft);
add(ButtonDown); add(ButtonDown);
add(ButtonRight); add(ButtonRight);
add(CreateButton); add(ButtonCreateBomberBase);
add(BomberPictureBox); add(BomberPictureBox);
} }
private JLabel BomberPictureBox; private JLabel BomberPictureBox;
private JButton CreateButton; private JButton ButtonCreateBomberBase;
private JButton ButtonRight; private JButton ButtonRight;
private JButton ButtonDown; private JButton ButtonDown;
private JButton ButtonLeft; private JButton ButtonLeft;
private JButton ButtonUp; private JButton ButtonUp;
private JComboBox<String> MovementStrategyComboBox;
private JButton ButtonCreateBomber;
private JButton ButtonPerformMove;
} }

View File

@ -0,0 +1,25 @@
package AirBomber.Entities;
import java.awt.Color;
public class BomberEntity extends BomberEntityBase
{
private Color AdditionalColor;
private boolean Bombs;
private boolean FuelTanks;
public BomberEntity(int Speed, double Weight, Color BodyColor, Color AdditionalColor, boolean FuelTanks, boolean Bombs)
{
super(Speed, Weight, BodyColor);
this.AdditionalColor = AdditionalColor;
this.FuelTanks = FuelTanks;
this.Bombs = Bombs;
}
public Color GetAdditionalColor() { return AdditionalColor; }
public boolean GetBombs() { return Bombs; }
public boolean GetFuelTanks() { return FuelTanks; }
}

View File

@ -0,0 +1,24 @@
package AirBomber.Entities;
import java.awt.Color;
public class BomberEntityBase
{
private int Speed;
private double Weight;
private Color BodyColor;
public double Step;
public BomberEntityBase(int Speed, double Weight, Color BodyColor)
{
this.Speed = Speed;
this.Weight = Weight;
this.BodyColor = BodyColor;
}
public int GetSpeed() { return Speed; }
public double GetWeight() { return Weight; }
public Color GetBodyColor() { return BodyColor; }
}

View File

@ -0,0 +1,81 @@
package AirBomber.MovementStrategy;
public abstract class AbstractStrategy
{
private IMovableObject _movableObject;
private Status _state = Status.NotInit;
protected int FieldWidth;
protected int FieldHeight;
public int GetFieldWidth() { return FieldWidth; }
public int GetFieldHeight() { return FieldHeight; }
public Status GetStatus() { return _state; }
public void SetData(IMovableObject MovableObject, int Width, int Height)
{
if (MovableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_movableObject = MovableObject;
FieldWidth = Width;
FieldHeight = Height;
}
public void MakeStep()
{
if (_state != Status.InProgress)
return;
if (IsTargetDestination())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
protected boolean MoveLeft() { return MoveTo(DirectionType.Left); }
protected boolean MoveRight() { return MoveTo(DirectionType.Right); }
protected boolean MoveUp() { return MoveTo(DirectionType.Up); }
protected boolean MoveDown() { return MoveTo(DirectionType.Down); }
protected ObjectParameters GetObjectParameters()
{
return _movableObject != null ? _movableObject.GetObjectPosition() : null;
}
protected Integer GetStep()
{
if (_state != Status.InProgress)
return null;
return _movableObject != null ? _movableObject.GetStep() : null;
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestination();
private boolean MoveTo(DirectionType DirectionType)
{
if (_state != Status.InProgress)
return false;
if (_movableObject != null && _movableObject.CanMove(DirectionType))
{
_movableObject.MoveObject(DirectionType);
return true;
}
return false;
}
}

View File

@ -1,4 +1,4 @@
package AirBomber; package AirBomber.MovementStrategy;
public enum DirectionType public enum DirectionType
{ {

View File

@ -0,0 +1,12 @@
package AirBomber.MovementStrategy;
public interface IMovableObject
{
ObjectParameters GetObjectPosition();
int GetStep();
boolean CanMove(DirectionType Direction);
void MoveObject(DirectionType Direction);
}

View File

@ -0,0 +1,45 @@
package AirBomber.MovementStrategy;
public class MoveToCenterStrategy extends AbstractStrategy
{
@Override
protected boolean IsTargetDestination()
{
ObjectParameters ObjParams = GetObjectParameters();
if (ObjParams == null)
return false;
float FieldMiddleHorizontal = FieldWidth / 2;
float FieldMiddleVertical = FieldHeight / 2;
return
Math.abs(ObjParams.ObjectMiddleHorizontal() - FieldMiddleHorizontal) < GetStep() &&
Math.abs(ObjParams.ObjectMiddleVertical() - FieldMiddleVertical) < GetStep();
}
@Override
protected void MoveToTarget()
{
ObjectParameters ObjParams = GetObjectParameters();
if (ObjParams == null)
return;
float diffX = ObjParams.ObjectMiddleHorizontal() - FieldWidth / 2;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
MoveLeft();
else
MoveRight();
}
float diffY = ObjParams.ObjectMiddleVertical() - FieldHeight / 2;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
MoveUp();
else
MoveDown();
}
}
}

View File

@ -0,0 +1,44 @@
package AirBomber.MovementStrategy;
public class MoveToDownRightStrategy extends AbstractStrategy
{
@Override
protected boolean IsTargetDestination()
{
ObjectParameters ObjParams = GetObjectParameters();
if (ObjParams == null)
return false;
return
ObjParams.RightBorder() <= FieldWidth &&
ObjParams.RightBorder() + GetStep() >= FieldWidth &&
ObjParams.BottomBorder() <= FieldHeight &&
ObjParams.BottomBorder() + GetStep() >= FieldHeight;
}
@Override
protected void MoveToTarget()
{
ObjectParameters ObjParams = GetObjectParameters();
if (ObjParams == null)
return;
float diffX = ObjParams.RightBorder() - FieldWidth;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
MoveLeft();
else
MoveRight();
}
float diffY = ObjParams.BottomBorder() - FieldHeight;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
MoveUp();
else
MoveDown();
}
}
}

View File

@ -0,0 +1,48 @@
package AirBomber.MovementStrategy;
import AirBomber.Rendering.*;
public class ObjectEntityRenderer implements IMovableObject
{
private final BomberRendererBase _entityRenderer;
public ObjectEntityRenderer(BomberRendererBase EntityRenderer)
{
_entityRenderer = EntityRenderer;
}
public ObjectParameters GetObjectPosition()
{
if (_entityRenderer == null || _entityRenderer.GetEntityBomber() == null)
return null;
return new ObjectParameters(
_entityRenderer.PosX(),
_entityRenderer.PosY(),
_entityRenderer.EntityWidth(),
_entityRenderer.EntityHeight()
);
}
public int GetStep()
{
if (_entityRenderer == null || _entityRenderer.GetEntityBomber() == null)
return 0;
return (int)(_entityRenderer.GetEntityBomber().Step);
}
public boolean CanMove(DirectionType Direction)
{
if (_entityRenderer == null)
return false;
return _entityRenderer.CanMove(Direction);
}
public void MoveObject(DirectionType Direction)
{
if (_entityRenderer != null)
_entityRenderer.MoveEntity(Direction);
}
}

View File

@ -0,0 +1,26 @@
package AirBomber.MovementStrategy;
public class ObjectParameters
{
private final int _x;
private final int _y;
private final int _width;
private final int _height;
public int LeftBorder() { return _x; }
public int TopBorder() { return _y; }
public int RightBorder() { return _x + _width; }
public int BottomBorder() { return _y + _height; }
public int ObjectMiddleHorizontal() { return _x + _width / 2; }
public int ObjectMiddleVertical() { return _y + _height / 2; }
public ObjectParameters(int x, int y, int Width, int Height)
{
_x = x;
_y = y;
_width = Width;
_height = Height;
}
}

View File

@ -0,0 +1,8 @@
package AirBomber.MovementStrategy;
public enum Status
{
NotInit,
InProgress,
Finish
}

View File

@ -0,0 +1,64 @@
package AirBomber.Rendering;
import AirBomber.Entities.*;
import java.awt.*;
public class BomberRenderer extends BomberRendererBase
{
public BomberRenderer(int Speed, double Weight, Color BodyColor, Color AdditionalColor, boolean FuelTanks, boolean Bombs, int Width, int Height)
{
super(Speed, Weight, BodyColor, Width, Height, 200, 200);
if (EntityBomber == null)
EntityBomber = new BomberEntity(Speed, Weight, BodyColor, AdditionalColor, FuelTanks, Bombs);
}
@Override
public void DrawEntity(Graphics g)
{
if ( !(EntityBomber instanceof BomberEntity DerivedEntityBomber) )
return;
super.DrawEntity(g);
/** Отрисовка дополнительных элементов */
g.setColor(DerivedEntityBomber.GetAdditionalColor());
if (DerivedEntityBomber.GetFuelTanks())
{
int[] LeftGasTankX = {
_startPosX + 50,
_startPosX + 75,
_startPosX + 75,
_startPosX + 50,
};
int[] LeftGasTankY = {
_startPosY + 85,
_startPosY + 85,
_startPosY + 70,
_startPosY + 70,
};
g.fillPolygon(LeftGasTankX, LeftGasTankY, 4);
int[] RightGasTankX = {
_startPosX + 50,
_startPosX + 75,
_startPosX + 75,
_startPosX + 50,
};
int[] RightGasTankY = {
_startPosY + 115,
_startPosY + 115,
_startPosY + 130,
_startPosY + 130,
};
g.fillPolygon(RightGasTankX, RightGasTankY, 4);
}
if (DerivedEntityBomber.GetBombs())
{
g.fillOval(_startPosX + 110, _startPosY + 115, 50, 25);
g.fillOval(_startPosX + 110, _startPosY + 60, 50, 25);
}
}
}

View File

@ -1,43 +1,57 @@
package AirBomber; package AirBomber.Rendering;
import AirBomber.*;
import AirBomber.Entities.*;
import AirBomber.MovementStrategy.*;
import java.awt.*; import java.awt.*;
import java.util.Random;
public class BomberRenderer public class BomberRendererBase
{ {
private BomberEntity EntityBomber; protected BomberEntityBase EntityBomber;
private int _pictureWidth; private int _pictureWidth;
private int _pictureHeight; private int _pictureHeight;
private int _startPosX; protected int _startPosX;
private int _startPosY; protected int _startPosY;
private int _bomberWidth = 200; protected int _bomberWidth = 200;
private int _bomberHeight = 200; protected int _bomberHeight = 200;
public int PosX() { return _startPosX; }
public int PosY() { return _startPosY; }
public EngineRenderer EngineRenderer; public EngineRenderer EngineRenderer;
public boolean Init(int Speed, double Weight, Color BodyColor, Color AdditionalColor, boolean FuelTanks, boolean Bombs, int Width, int Height) public int EntityWidth() { return _bomberWidth; }
public int EntityHeight() { return _bomberHeight; }
public BomberRendererBase(int Speed, double Weight, Color BodyColor, int Width, int Height)
{ {
if (Width < _bomberWidth || Height < _bomberHeight) if (Width < _bomberWidth || Height < _bomberHeight)
return false; return;
_pictureWidth = Width; _pictureWidth = Width;
_pictureHeight = Height; _pictureHeight = Height;
EntityBomber = new BomberEntity(); EntityBomber = new BomberEntityBase(Speed, Weight, BodyColor);
EntityBomber.Init(Speed, Weight, BodyColor, AdditionalColor, FuelTanks, Bombs);
EngineRenderer = new EngineRenderer();
Random Random = new Random();
EngineRenderer.SetAmount(Random.nextInt(1, 7));
return true;
} }
public BomberEntity GetEntityBomber() { return EntityBomber; } public BomberRendererBase(int Speed, double Weight, Color BodyColor, int Width, int Height, int EntityWidth, int EntityHeight)
{
if (Width < _bomberWidth || Height < _bomberHeight)
return;
_pictureWidth = Width;
_pictureHeight = Height;
_bomberWidth = EntityWidth;
_bomberHeight = EntityHeight;
EntityBomber = new BomberEntityBase(Speed, Weight, BodyColor);
}
public BomberEntityBase GetEntityBomber() { return EntityBomber; }
public void SetPosition(int x, int y) public void SetPosition(int x, int y)
{ {
@ -59,6 +73,29 @@ public class BomberRenderer
_startPosY = y; _startPosY = y;
} }
public boolean CanMove(DirectionType Direction)
{
if (EntityBomber == null)
return false;
switch (Direction) {
case Left:
return _startPosX - EntityBomber.Step > 0;
case Up:
return _startPosY - EntityBomber.Step > 0;
case Right:
return _startPosX + _bomberWidth + EntityBomber.Step <= _pictureWidth;
case Down:
return _startPosY + _bomberHeight + EntityBomber.Step <= _pictureHeight;
default:
return false;
}
}
public void MoveEntity(DirectionType Direction) public void MoveEntity(DirectionType Direction)
{ {
if (EntityBomber == null) if (EntityBomber == null)
@ -67,27 +104,19 @@ public class BomberRenderer
switch (Direction) switch (Direction)
{ {
case Up: case Up:
if (_startPosY - EntityBomber.Step > 0) _startPosY -= (int)EntityBomber.Step;
_startPosY -= (int)EntityBomber.Step;
break; break;
case Down: case Down:
if (_startPosY + _bomberHeight + EntityBomber.Step <= _pictureHeight) _startPosY += (int)EntityBomber.Step;
_startPosY += (int)EntityBomber.Step;
break; break;
case Left: case Left:
if (_startPosX - EntityBomber.Step > 0) _startPosX -= (int)EntityBomber.Step;
_startPosX -= (int)EntityBomber.Step;
break; break;
case Right: case Right:
if (_startPosX + _bomberWidth + EntityBomber.Step <= _pictureWidth) _startPosX += (int)EntityBomber.Step;
_startPosX += (int)EntityBomber.Step;
break; break;
} }
} }
@ -183,46 +212,6 @@ public class BomberRenderer
}; };
g.drawPolygon(BackRightWingX, BackRightWingY, 4); g.drawPolygon(BackRightWingX, BackRightWingY, 4);
/** Отрисовка дополнительных элементов */
g.setColor(EntityBomber.GetAdditionalColor());
if (EntityBomber.GetFuelTanks())
{
int[] LeftGasTankX = {
_startPosX + 50,
_startPosX + 75,
_startPosX + 75,
_startPosX + 50,
};
int[] LeftGasTankY = {
_startPosY + 85,
_startPosY + 85,
_startPosY + 70,
_startPosY + 70,
};
g.fillPolygon(LeftGasTankX, LeftGasTankY, 4);
int[] RightGasTankX = {
_startPosX + 50,
_startPosX + 75,
_startPosX + 75,
_startPosX + 50,
};
int[] RightGasTankY = {
_startPosY + 115,
_startPosY + 115,
_startPosY + 130,
_startPosY + 130,
};
g.fillPolygon(RightGasTankX, RightGasTankY, 4);
}
if (EntityBomber.GetBombs())
{
g.fillOval(_startPosX + 110, _startPosY + 115, 50, 25);
g.fillOval(_startPosX + 110, _startPosY + 60, 50, 25);
}
EngineRenderer.DrawEngines(g, EntityBomber.GetBodyColor(), _startPosX, _startPosY); EngineRenderer.DrawEngines(g, EntityBomber.GetBodyColor(), _startPosX, _startPosY);
} }
} }