2 лабораторная

This commit is contained in:
sardq 2023-12-04 20:50:24 +04:00
parent c8a89e6c86
commit 23b457e9c1
16 changed files with 529 additions and 69 deletions

119
src/AbstractStrategy.java Normal file
View File

@ -0,0 +1,119 @@
public abstract class AbstractStrategy
{
// Перемещаемый объект
private IMoveableObject _moveableObject;
// Статус перемещения
private Status _state = Status.NotInit;
// Ширина поля
protected int FieldWidth;
protected int GetFieldWidth()
{
return FieldWidth;
}
private void SetFieldWidth(int fieldWidth) {
FieldWidth = fieldWidth;
}
// Высота поля
protected int FieldHeight ;
protected int GetFieldHeigth()
{
return FieldHeight;
}
private void SetFieldHeigth(int fieldHeight) {
FieldHeight = fieldHeight;
}
// Статус перемещения
public Status GetStatus() { return _state; }
/// Установка данных
/// <param name="moveableObject">Перемещаемый объект</param>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
// Шаг перемещения
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
// Перемещение влево
/// <returns>Результат перемещения (true - удалось переместиться, false -неудача)</returns>
protected boolean MoveLeft() {return MoveTo(DirectionType.Left);}
// Перемещение вправо false - неудача)</returns>
protected boolean MoveRight() {return MoveTo(DirectionType.Right);}
/// <summary>
/// Перемещение вверх
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
protected boolean MoveUp(){return MoveTo(DirectionType.Up);}
/// <summary>
/// Перемещение вниз
/// </summary>
/// <returns>Результат перемещения (true - удалось переместиться,false - неудача)</returns>
protected boolean MoveDown() {return MoveTo(DirectionType.Down);}
/// <summary>
/// Параметры объекта
/// </summary>
protected ObjectParameters GetObjectParameters()
{
return _moveableObject.GetObjectPosition();
}
/// <summary>
/// Шаг объекта
/// </summary>
/// <returns></returns>
protected Integer GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject.GetStep();
}
/// <summary>
/// Перемещение к цели
/// </summary>
protected abstract void MoveToTarget();
/// <summary>
/// Достигнута ли цель
/// </summary>
/// <returns></returns>
protected abstract boolean IsTargetDestinaion();
/// <summary>
/// Попытка перемещения в требуемом направлении
/// </summary>
/// <param name="directionType">Направление</param>
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
private boolean MoveTo(DirectionType directionType)
{
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject.CheckCanMove(directionType))
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}

View File

@ -1,31 +1,41 @@
import java.awt.*;
public class DrawingCrane
{
private EntityCrane entityCrane;
private DrawingWheel drawingWheel;
protected EntityCrane entityCrane;
public EntityCrane GetEntityCrane(){return entityCrane;}
protected void SetEntityCrane(EntityCrane entityCraneTemp){entityCrane=entityCraneTemp;}
private IWheel drawingWheel;
private int _pictureWidth;
private int _pictureHeight;
private int _startPositionX;
private int _startPositionY;
protected int _startPositionX;
protected int _startPositionY;
private final int _craneWidth = 200;
private final int _craneHeight = 150;
public void Init(int speed, double weight, boolean counterWeight, boolean crane, Color bodyColor, Color additionalColor, int width, int height,int wheelCount)
public int GetPosX() {return _startPositionX;}
public int GetPosY() {return _startPositionY;}
public int GetWidth() {return _craneWidth;}
public int GetHeight() {return _craneHeight;}
protected DrawingCrane(int speed, double weight, Color bodyColor, int width, int height, int WheelCount, int wheelMode)
{
if ((_craneWidth > width) || (_craneHeight > height)) return;
_pictureHeight = height;
_pictureWidth = width;
entityCrane = new EntityCrane();
entityCrane.Init(speed, weight, counterWeight,crane, bodyColor, additionalColor);
drawingWheel = new DrawingWheel();
drawingWheel.SetWheelCounter(wheelCount);
int mode = wheelMode % 3;
entityCrane = new EntityCrane(speed, weight, bodyColor);
switch (mode)
{
case 0 -> {drawingWheel = new DrawingWheelOval();}
case 1 -> {drawingWheel = new DrawingWheelDouble();}
case 2 -> {drawingWheel = new DrawingWheelRectangle();}
}
drawingWheel.SetWheelCounter(WheelCount);
}
public void SetPosition(int x, int y)
{
if(x+_craneWidth>_pictureWidth)
{
while(x + _craneWidth > _pictureWidth)
@ -49,39 +59,56 @@ public class DrawingCrane
if (entityCrane == null) return;
switch (direction) {
case Left -> {
if (_startPositionX - entityCrane.Step > 0) {
_startPositionX -= (int) entityCrane.Step;
}
}
case Right->{
if (_startPositionX + entityCrane.Step + _craneWidth < _pictureWidth) {
_startPositionX += (int) entityCrane.Step;
}
}
case Up-> {
if (_startPositionY - entityCrane.Step > 0) {
_startPositionY -= (int) entityCrane.Step;
}
}
case Down ->{
if (_startPositionY + entityCrane.Step + _craneHeight < _pictureHeight) {
_startPositionY += (int) entityCrane.Step;
}
}
}
}
public boolean CanMove(DirectionType direction)
{
if (entityCrane == null)
{
return false;
}
switch(direction)
{
//влево
case Left -> {
return _startPositionX - entityCrane.Step > 0;}
//вверх
case Up -> {
return _startPositionY - entityCrane.Step > 0;}
// вправо
case Right -> {
return _startPositionX + entityCrane.Step - _craneWidth < _pictureWidth;}
//вниз
case Down -> {
return _startPositionY + entityCrane.Step - _craneHeight < _pictureHeight;}
default ->{
return false;
}
}
}
public void DrawTransport(Graphics g) {
var g2d = (Graphics2D)g;
if (entityCrane == null) return;
// Гусеницы
g2d.drawLine( _startPositionX + 5, _startPositionY + 110, _startPositionX + 195, _startPositionY + 110);
g2d.drawLine( _startPositionX, _startPositionY + 115, _startPositionX, _startPositionY + 145);
g2d.drawLine( _startPositionX + 5, _startPositionY + 150, _startPositionX + 195, _startPositionY + 150);
@ -100,16 +127,5 @@ public class DrawingCrane
//кабинка и выхлоп
g2d.drawRect( _startPositionX + 60, _startPositionY + 10, 20, 55);
g2d.drawRect( _startPositionX + 110, _startPositionY, 75, 65);
if (entityCrane.getCounterWeight()) {
g2d.setColor(entityCrane.getAdditionalColor());
g2d.drawRect( _startPositionX + 185, _startPositionY + 20, 15, 45);
}
if (entityCrane.getCrane()) {
g2d.drawRect( _startPositionX + 20, _startPositionY, 30, 65);
g2d.drawRect( _startPositionX, _startPositionY, 20, 30);
g2d.fillRect( _startPositionX + 20, _startPositionY, 30, 65);
g2d.fillRect( _startPositionX, _startPositionY, 20, 30);
}
}
}
}

View File

@ -0,0 +1,31 @@
import java.awt.*;
public class DrawingHoistingCrane extends DrawingCrane {
public DrawingHoistingCrane(int speed, double weight, Color bodyColor, Color additionalColor, boolean counterWeight, boolean crane,int width, int height, int WheelCount, int wheelMode)
{
super(speed, weight, bodyColor, width, height, WheelCount,wheelMode);
if (entityCrane != null)
{
entityCrane = new EntityHoistingCrane(speed, weight, bodyColor, additionalColor, counterWeight, crane);
}
}
@Override
public void DrawTransport(Graphics g) {
if (!(entityCrane instanceof EntityHoistingCrane)) {
return;
}
super.DrawTransport(g);
if (((EntityHoistingCrane) entityCrane).getCounterWeight()) {
g.setColor(((EntityHoistingCrane) entityCrane).getAdditionalColor());
g.drawRect(_startPositionX + 185, _startPositionY + 20, 15, 45);
}
if (((EntityHoistingCrane) entityCrane).getCrane()) {
g.drawRect(_startPositionX + 20, _startPositionY, 30, 65);
g.drawRect(_startPositionX, _startPositionY, 20, 30);
g.setColor(((EntityHoistingCrane) entityCrane).getAdditionalColor());
g.fillRect(_startPositionX + 20, _startPositionY, 30, 65);
g.fillRect(_startPositionX, _startPositionY, 20, 30);
}
}
}

View File

@ -0,0 +1,31 @@
import java.awt.*;
public class DrawingWheelDouble implements IWheel{
private WheelCounter wheelCounter;
public void SetWheelCounter(int count)
{
if (count % 3 == 0)
wheelCounter = WheelCounter.SIX;
else if (count % 3 == 1)
wheelCounter = WheelCounter.FOUR;
else if (count % 3 == 2)
wheelCounter = WheelCounter.FIVE;
}
public void DrawWheels(int _startPositionX, int _startPositionY, Graphics2D g2d)
{
g2d.drawOval( _startPositionX + 2, _startPositionY + 112, 36, 36);
g2d.drawOval( _startPositionX + 7, _startPositionY + 117, 25, 25);
g2d.drawOval( _startPositionX + 160, _startPositionY + 112, 36, 36);
g2d.drawOval( _startPositionX + 165, _startPositionY + 117, 25, 25);
g2d.drawOval( _startPositionX + 74, _startPositionY + 112, 10, 10);
g2d.drawOval( _startPositionX + 76, _startPositionY + 114, 6, 6);
g2d.drawOval( _startPositionX + 111, _startPositionY + 112, 10, 10);
g2d.drawOval( _startPositionX + 113, _startPositionY + 114, 6, 6);
switch (wheelCounter)
{
case FIVE -> {g2d.drawOval( _startPositionX + 133, _startPositionY + 128, 20, 20);g2d.drawOval( _startPositionX + 136, _startPositionY + 131, 14, 14);}
case SIX -> {g2d.drawOval( _startPositionX + 45, _startPositionY + 128, 20, 20);g2d.drawOval( _startPositionX + 48, _startPositionY + 131, 14, 14);g2d.drawOval( _startPositionX + 136, _startPositionY + 131, 14, 14); g2d.drawOval( _startPositionX + 133, _startPositionY + 128, 20, 20);}
}
}
}

View File

@ -1,6 +1,6 @@
import java.awt.*;
public class DrawingWheel {
public class DrawingWheelOval implements IWheel {
private WheelCounter wheelCounter;
public void SetWheelCounter(int count)
{

View File

@ -0,0 +1,32 @@
import java.awt.*;
public class DrawingWheelRectangle implements IWheel {
private WheelCounter wheelCounter;
public WheelCounter getWheelCount()
{
return wheelCounter;
}
public void SetWheelCounter(int count)
{
if (count % 3 == 0)
wheelCounter = WheelCounter.SIX;
else if (count % 3 == 1)
wheelCounter = WheelCounter.FOUR;
else if (count % 3 == 2)
wheelCounter = WheelCounter.FIVE;
}
public void DrawWheels(int _startPositionX, int _startPositionY, Graphics2D g2d)
{
g2d.drawRect( _startPositionX + 2, _startPositionY + 112, 36, 36);
g2d.drawRect( _startPositionX + 160, _startPositionY + 112, 36, 36);
g2d.drawRect( _startPositionX + 74, _startPositionY + 112, 10, 10);
g2d.drawRect( _startPositionX + 111, _startPositionY + 112, 10, 10);
switch (wheelCounter)
{
case FIVE -> {g2d.drawRect( _startPositionX + 133, _startPositionY + 128, 20, 20);}
case SIX -> {g2d.drawRect( _startPositionX + 45, _startPositionY + 128, 20, 20); g2d.drawRect( _startPositionX + 133, _startPositionY + 128, 20, 20);}
}
}
}

View File

@ -0,0 +1,29 @@
public class DrawningObjectCrane implements IMoveableObject {
private final DrawingCrane _drawingCrane;
public DrawningObjectCrane(DrawingCrane drawingCrane)
{
_drawingCrane = drawingCrane;
}
public ObjectParameters GetObjectPosition()
{
if (_drawingCrane == null || _drawingCrane.entityCrane == null)
{
return null;
}
return new ObjectParameters(_drawingCrane.GetPosX(), _drawingCrane.GetPosY(), _drawingCrane.GetWidth(), _drawingCrane.GetHeight());
}
public int GetStep() {
int step = (int) (_drawingCrane.entityCrane.Step);
if (step != 0)
return step;
return 0;
}
public boolean CheckCanMove(DirectionType direction) {
boolean canMove = _drawingCrane.CanMove(direction);
if(canMove)
return true;
return false;
}
public void MoveObject(DirectionType direction) { _drawingCrane.MoveCrane(direction);}
}

View File

@ -6,8 +6,6 @@ public class EntityCrane
private int Speed ;
public int getSpeed() {return Speed;}
private void setSpeed(int value) {Speed = value;}
//вес
private double Weight ;
public double getWeight() {return Weight;}
@ -16,29 +14,14 @@ public class EntityCrane
private Color BodyColor ;
public Color getBodyColor() {return BodyColor;}
private void setBodyColor(Color value) {BodyColor = value;}
//дополнительный
private Color AdditionalColor ;
public Color getAdditionalColor() {return AdditionalColor;}
private void setAdditionalColor(Color value) {AdditionalColor = value;}
private boolean CounterWeight ;
public boolean getCounterWeight() {return CounterWeight;}
private void setCounterWeight(boolean value) {CounterWeight = value;}
private boolean Crane;
public boolean getCrane() {return Crane;}
private void setCrane(boolean value) {Crane = value;}
//шаг перемещения
public double Step;
public void Init(int speed, double weight, boolean counterWeight,boolean crane, Color bodyColor, Color additionalColor)
public EntityCrane(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
CounterWeight = counterWeight;
Crane = crane;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Step = (double)Speed * 100 / Weight;
}
}
}

View File

@ -0,0 +1,19 @@
import java.awt.*;
public class EntityHoistingCrane extends EntityCrane
{
private Color AdditionalColor ;
public Color getAdditionalColor() {return AdditionalColor;}
private boolean CounterWeight ;
public boolean getCounterWeight() {return CounterWeight;}
private boolean Crane;
public boolean getCrane() {return Crane;}
private void setCrane(boolean value) {Crane = value;}
public EntityHoistingCrane(int speed, double weight, Color bodyColor, Color additionalColor, boolean counterWeight, boolean crane)
{
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
CounterWeight = counterWeight;
Crane = crane;
}
}

View File

@ -1,14 +1,11 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.*;
import java.io.IOException;
import java.util.Random;
class DrawCrane extends JComponent {
private DrawingCrane _drawingCrane;
private AbstractStrategy _abstractStrategy;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
@ -20,15 +17,59 @@ class DrawCrane extends JComponent {
protected void CreateCraneButton_Click()
{
Random rnd = new Random();
_drawingCrane = new DrawingCrane();
Color addColor = new Color(rnd.nextInt( 256), rnd.nextInt( 256), rnd.nextInt( 256));
Color bodyColor = new Color(rnd.nextInt( 256), rnd.nextInt( 256), rnd.nextInt(256));
_drawingCrane.Init(rnd.nextInt(100)+100, rnd.nextInt(2000)+1000,
ConvertToBoolean(rnd.nextInt( 2)), ConvertToBoolean(rnd.nextInt( 2)), bodyColor, addColor,
Frame.Width-10, Frame.Height-38, rnd.nextInt( 3)+4);
_drawingCrane = new DrawingCrane(rnd.nextInt(100)+100, rnd.nextInt(2000)+1000,
bodyColor,
Frame.Width-10, Frame.Height-38, rnd.nextInt( 3),rnd.nextInt( 3));
_drawingCrane.SetPosition(rnd.nextInt( 100), rnd.nextInt(90)+10);
super.repaint();
}
protected void CreateHoistingCraneButton_Click()
{
Random rnd = new Random();
Color addColor = new Color(rnd.nextInt( 256), rnd.nextInt( 256), rnd.nextInt( 256));
Color bodyColor = new Color(rnd.nextInt( 256), rnd.nextInt( 256), rnd.nextInt(256));
_drawingCrane = new DrawingHoistingCrane(rnd.nextInt(100)+100, rnd.nextInt(2000)+1000,
bodyColor,
addColor,ConvertToBoolean(rnd.nextInt( 2)), ConvertToBoolean(rnd.nextInt( 2)),Frame.Width-10, Frame.Height-38, rnd.nextInt( 3),rnd.nextInt( 3));
_drawingCrane.SetPosition(rnd.nextInt( 100), rnd.nextInt(90)+10);
super.repaint();
}
protected void buttonStep_Click(JComboBox comboBox,int width,int height)
{
if (_drawingCrane == null)
{
return;
}
if (comboBox.isEnabled())
{
String strategy = String.valueOf(comboBox.getSelectedItem());
switch (strategy)
{
case "0"-> {_abstractStrategy =new MoveToCenter();}
case "1"-> {_abstractStrategy =new MoveToBorder();}
default -> {_abstractStrategy = null;}
};
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.SetData(new DrawningObjectCrane(_drawingCrane), width, height);
comboBox.setEnabled(false);
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
super.repaint();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBox.setEnabled(true);
_abstractStrategy = null;
}
}
protected void ButtonMove_Click(String tag)
{
if (_drawingCrane == null)
@ -55,7 +96,6 @@ public class Frame extends JFrame {
}
protected static final int Width = 900;
protected static final int Height = 500;
DrawCrane Crane;
private void InitUI() {
setSize(Width, Height);
@ -91,6 +131,7 @@ public class Frame extends JFrame {
down.setLayout(null);
JButton CreateButton = new JButton("Создать");
JButton CreateHoistingButton = new JButton("Создать улучшенный кран");
right.setIcon(new ImageIcon("src/crane/right.png"));
left.setIcon(new ImageIcon("src/crane/left.png"));
down.setIcon(new ImageIcon("src/crane/down.png"));
@ -115,16 +156,28 @@ public class Frame extends JFrame {
constraints.gridx = 2;
panelMoveButtons.add(right, constraints);
String[] items = {
"0",
"1"
};
JComboBox comboBoxStrategy = new JComboBox(items);
comboBoxStrategy.setBounds(697,16,182,33);
JPanel panelCreateButton = new JPanel();
panelCreateButton.setLayout(new BorderLayout());
panelCreateButton.setBounds(0, 420, 112, 34);
panelCreateButton.setBounds(0, 420, 200, 34);
panelCreateButton.add(CreateButton);
JPanel panelCreateHoistingButton = new JPanel();
panelCreateHoistingButton.setLayout(new BorderLayout());
panelCreateHoistingButton.setBounds(200, 420, 200, 34);
panelCreateHoistingButton.add(CreateHoistingButton);
JButton buttonStep =new JButton("Шаг");
buttonStep.setBounds(798,55,80,35);
add(buttonStep);
add(panelMoveButtons);
add(panelCreateButton);
add(panelCreateHoistingButton);
add(comboBoxStrategy);
setVisible(true);
CreateButton.addActionListener(new ActionListener() {
@Override
@ -132,11 +185,24 @@ public class Frame extends JFrame {
Crane.CreateCraneButton_Click();
}
});
CreateHoistingButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Crane.CreateHoistingCraneButton_Click();
}
});
buttonStep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Crane.buttonStep_Click(comboBoxStrategy,getWidth(),getHeight());
}
});
}
public class MoveAL implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Crane.ButtonMove_Click(e.getActionCommand());
}
}
}
}

7
src/IMoveableObject.java Normal file
View File

@ -0,0 +1,7 @@
public interface IMoveableObject
{
ObjectParameters GetObjectPosition();
int GetStep();
boolean CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction);
}

5
src/IWheel.java Normal file
View File

@ -0,0 +1,5 @@
import java.awt.*;
public interface IWheel {
void DrawWheels(int _startPositionX, int _startPositionY, Graphics2D g2d);
void SetWheelCounter(int wheelCount);
}

47
src/MoveToBorder.java Normal file
View File

@ -0,0 +1,47 @@
public class MoveToBorder extends AbstractStrategy{
@Override
protected boolean IsTargetDestinaion()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal() <= FieldWidth && objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth &&
objParams.ObjectMiddleHorizontal() <= FieldHeight &&
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldHeight;
}
@Override
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
var diffX = objParams.RightBorder() - FieldWidth;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.DownBorder() - FieldHeight;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

45
src/MoveToCenter.java Normal file
View File

@ -0,0 +1,45 @@
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean IsTargetDestinaion()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 && objParams.ObjectMiddleVertical() <= FieldHeight / 2 && objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2;
}
@Override
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

25
src/ObjectParameters.java Normal file
View File

@ -0,0 +1,25 @@
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 DownBorder(){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;
}
}

5
src/Status.java Normal file
View File

@ -0,0 +1,5 @@
public enum Status {
NotInit,
InProgress,
Finish
}