Лабараторная работа №2 1

This commit is contained in:
IlyasValiulov 2024-03-14 20:03:10 +04:00
parent fb2d9e01db
commit cc0082b6c9
20 changed files with 591 additions and 0 deletions

1
WarmlyShip/.idea/.name Normal file
View File

@ -0,0 +1 @@
Main.java

View File

@ -0,0 +1,28 @@
package DiffetentsDrawingDecks;
import java.awt.*;
public class DrawingDecksType1 implements IDifferentDecks {
private NumberOfDecks numberOfDecks;
@Override
public void setNumberOfDecks(int numberofdeck) {
for (NumberOfDecks numofenum : NumberOfDecks.values()) {
if (numofenum.getNumdecks() == numberofdeck) {
numberOfDecks = numofenum;
return;
}
}
}
@Override
public NumberOfDecks getNumberOfDecks() {
return numberOfDecks;
}
@Override
public void DrawDecks(Graphics2D g, int x, int y, int width, int height, Color bodyColor) {
g.setColor(bodyColor);
g.fillRect(x, y, width, height);
g.setColor(Color.BLACK);
g.drawRect(x, y, width, height);
g.setColor(bodyColor);
}
}

View File

@ -0,0 +1,34 @@
package DiffetentsDrawingDecks;
import java.awt.*;
public class DrawingDecksType2 implements IDifferentDecks {
private NumberOfDecks numberOfDecks;
@Override
public void setNumberOfDecks(int numberofdeck) {
for (NumberOfDecks numofenum : NumberOfDecks.values()) {
if (numofenum.getNumdecks() == numberofdeck) {
numberOfDecks = numofenum;
return;
}
}
}
@Override
public NumberOfDecks getNumberOfDecks() {
return numberOfDecks;
}
@Override
public void DrawDecks(Graphics2D g, int x, int y, int width, int height, Color bodyColor) {
g.setStroke(new BasicStroke(3.0F));
g.setColor(bodyColor);
//верхняя часть полубы
g.drawLine(x, y, x + width, y);
//опорные балки
int countBalks = 4;
int wayBetweenBalks = width / countBalks;
for (int i = 0; i <= countBalks; i++) {
int nowX = x + wayBetweenBalks * i;
g.drawLine(nowX, y, nowX, y + height);
}
}
}

View File

@ -0,0 +1,28 @@
package DiffetentsDrawingDecks;
import java.awt.*;
public class DrawingDecksType3 implements IDifferentDecks{
private NumberOfDecks numberOfDecks;
@Override
public void setNumberOfDecks(int numberofdeck) {
for (NumberOfDecks numofenum : NumberOfDecks.values()) {
if (numofenum.getNumdecks() == numberofdeck) {
numberOfDecks = numofenum;
return;
}
}
}
@Override
public NumberOfDecks getNumberOfDecks() {
return numberOfDecks;
}
@Override
public void DrawDecks(Graphics2D g, int x, int y, int width, int height, Color bodyColor) {
g.setColor(bodyColor);
g.fillRect(x, y, width, height);
g.setColor(Color.BLACK);
g.fillRect(x+ 10, y+5, width - 20, height-5);
g.setColor(bodyColor);
}
}

View File

@ -0,0 +1,9 @@
package DiffetentsDrawingDecks;
import java.awt.*;
public interface IDifferentDecks {
void setNumberOfDecks(int numberofdeck);
NumberOfDecks getNumberOfDecks();
void DrawDecks(Graphics2D g, int x, int y, int width, int height, Color bodyColor);
}

View File

@ -0,0 +1,12 @@
package DiffetentsDrawingDecks;
public enum NumberOfDecks {
OneDeck(1),
TwoDecks(2),
ThreeDecks(3);
private int numberofdecks;
NumberOfDecks(int numberofdecks) {
this.numberofdecks = numberofdecks;
}
public int getNumdecks() {return numberofdecks;}
}

View File

@ -0,0 +1,18 @@
package DrawingShip;
import javax.swing.*;
import java.awt.*;
public class CanvasWarmlyShip extends JComponent {
public DrawingShip _drawingShip;
public CanvasWarmlyShip(){}
public void paintComponent(Graphics g) {
if (_drawingShip == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_drawingShip.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -0,0 +1,9 @@
package DrawingShip;
public enum DirectionType {
Unknow,
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,146 @@
package DrawingShip;
import DiffetentsDrawingDecks.DrawingDecksType2;
import DiffetentsDrawingDecks.DrawingDecksType3;
import DiffetentsDrawingDecks.IDifferentDecks;
import Entities.EntityShip;
import DiffetentsDrawingDecks.DrawingDecksType1;
import javax.swing.*;
import java.awt.*;
public class DrawingShip extends JPanel {
public Entities.EntityShip EntityShip;
public IDifferentDecks drawingDecks;
private Integer picture_width;
private Integer picture_height;
public Integer _StartPosX;
public Integer _StartPosY;
private int drawingShipWidth = 150;
protected int drawingShipHeight = 50;
public Integer GetPosX() {return _StartPosX;}
public Integer GetPosY() {return _StartPosY;}
public Integer GetWidth() {return drawingShipWidth;}
public Integer GetHeight() {return drawingShipHeight;}
protected DrawingShip() {
picture_width = null;
picture_height = null;
_StartPosX = null;
_StartPosY = null;
}
protected void SetAmountandTypeDecks() {
int numberOfDecks = (int)(Math.random() * 4 + 0);
switch ((int)(Math.random() * 3 + 1)) {
case 1:
drawingDecks = new DrawingDecksType1();
break;
case 2:
drawingDecks = new DrawingDecksType2();
break;
case 3:
drawingDecks = new DrawingDecksType3();
break;
default:
numberOfDecks = 0;
break;
}
drawingDecks.setNumberOfDecks(numberOfDecks);
}
public DrawingShip(int speed, double weight, Color bodycolor) {
super();
EntityShip = new EntityShip(speed, weight, bodycolor);
SetAmountandTypeDecks();
}
public boolean SetPictureSize(int width, int height) {
if (width < drawingShipWidth || height < drawingShipHeight) return false;
picture_width = width;
picture_height = height;
if (_StartPosX != null || _StartPosY != null) {
if (_StartPosX + drawingShipWidth > picture_width)
{
_StartPosX = _StartPosX - (_StartPosX + drawingShipWidth - picture_width);
}
else if (_StartPosX < 0) _StartPosX = 0;
if (_StartPosY + drawingShipHeight > picture_height)
{
_StartPosY = _StartPosY - (_StartPosY + drawingShipHeight - picture_height);
}
else if (_StartPosY < 0) _StartPosY = 0;
}
return true;
}
public void SetPosition(int x, int y) {
if (!(picture_width != null && picture_height != null)) return;
if (x + drawingShipWidth > picture_width)
{
_StartPosX = x - (x + drawingShipWidth - picture_width);
}
else if (x < 0) _StartPosX = 0;
else _StartPosX = x;
if (y + drawingShipHeight > picture_height)
{
_StartPosY = y - (y + drawingShipHeight - picture_height);
}
else if (y < 0) _StartPosY = 0;
else _StartPosY = y;
}
public boolean MoveTransport(DirectionType direction) {
if (EntityShip == null || _StartPosX == null || _StartPosY == null) return false;
switch (direction) {
case DirectionType.Left:
if (_StartPosX - EntityShip.Step > 0) {
_StartPosX -= (int)EntityShip.Step;
}
return true;
case DirectionType.Up:
if (_StartPosY - EntityShip.Step > 0)
{
_StartPosY -= (int)EntityShip.Step;
}
return true;
case DirectionType.Right:
if (_StartPosX + drawingShipWidth + (int)EntityShip.Step < picture_width - EntityShip.Step)
{
_StartPosX += (int)EntityShip.Step;
}
return true;
case DirectionType.Down:
if (_StartPosY + drawingShipHeight + (int)EntityShip.Step < picture_height - EntityShip.Step)
{
_StartPosY += (int)EntityShip.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g) {
if (EntityShip == null || _StartPosX == null || _StartPosY == null) return;
int y = _StartPosY;
g.setColor(EntityShip.getBodyColor());
if (drawingDecks.getNumberOfDecks() != null) {
switch (drawingDecks.getNumberOfDecks()) {
case OneDeck:
drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityShip.getBodyColor());
y += 15;
break;
case TwoDecks:
drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityShip.getBodyColor());
drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityShip.getBodyColor());
y += 30;
break;
case ThreeDecks:
drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityShip.getBodyColor());
drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityShip.getBodyColor());
drawingDecks.DrawDecks(g, _StartPosX + 30, y + 30, 100, 15, EntityShip.getBodyColor());
y += 45;
break;
}
}
int[] arrayX = {_StartPosX, _StartPosX+150, _StartPosX+150, _StartPosX+120, _StartPosX+120, _StartPosX+30, _StartPosX+30, _StartPosX};
int[] arrayY = {y, y, y, y + 50, y + 50, y + 50, y + 50, y};
Polygon poly = new Polygon(arrayX, arrayY, 8);
g.fillPolygon(poly);
drawingShipHeight = y + 50 - _StartPosY;
}
}

View File

@ -0,0 +1,47 @@
package DrawingShip;
import Entities.EntityWarmlyShip;
import java.awt.*;
public class DrawingWarmlyShip extends DrawingShip {
public DrawingWarmlyShip(int speed, double weight, Color bodycolor, Color additionalcolor, boolean sheeppipes, boolean fueltank) {
EntityShip = new EntityWarmlyShip(speed, weight, bodycolor, additionalcolor, sheeppipes, fueltank);
SetAmountandTypeDecks();
}
@Override
public void DrawTransport(Graphics2D g) {
if (EntityShip == null || !(EntityShip instanceof EntityWarmlyShip warmlyship) || _StartPosX == null || _StartPosY == null)
return;
int y = _StartPosY;
if (warmlyship.ShipPipes) {
//трубы
g.setColor(warmlyship.getAdditionalColor());
g.fillRect(_StartPosX + 70, _StartPosY, 12, 30);
g.fillRect(_StartPosX + 90, _StartPosY, 12, 30);
_StartPosY += 30;
}
super.DrawTransport(g);
int count_decks = 0;
if (drawingDecks.getNumberOfDecks() != null) {
switch (drawingDecks.getNumberOfDecks()) {
case OneDeck:
count_decks = 1;
break;
case TwoDecks:
count_decks = 2;
break;
case ThreeDecks:
count_decks = 3;
break;
}
}
if (warmlyship.FuelTank) {
g.setColor(warmlyship.getAdditionalColor());
g.fillRect(_StartPosX + 40, _StartPosY + count_decks * 15 + 30, 70, 10);
}
if (warmlyship.ShipPipes) {
_StartPosY -= 30;
drawingShipHeight += 30;
}
}
}

View File

@ -0,0 +1,18 @@
package Entities;
import java.awt.*;
public class EntityShip {
private int Speed;
private double Weight;
private Color BodyColor;
public Color getBodyColor() {return BodyColor;}
public double Step;
public EntityShip(int speed, double weight, Color bodycolor)
{
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
Step = Speed * 100 / Weight;
}
}

View File

@ -0,0 +1,16 @@
package Entities;
import java.awt.*;
public class EntityWarmlyShip extends EntityShip{
public Color AdditionalColor;
public Color getAdditionalColor() {return AdditionalColor;}
public boolean ShipPipes;
public boolean FuelTank;
public EntityWarmlyShip(int speed, double weight, Color bodyColor, Color additionalcolor, boolean sheeppipes, boolean fueltank)
{
super(speed, weight, bodyColor);
AdditionalColor = additionalcolor;
ShipPipes = sheeppipes;
FuelTank = fueltank;
}
}

View File

@ -0,0 +1,56 @@
package MovementStrategy;
public abstract class AbstractStrategy {
private IMoveableObjects _moveableObject;
private StrategyStatus _state = StrategyStatus.NotInit;
public int FieldWidth;
public int FieldHeight;
public StrategyStatus GetStatus() {return _state;}
public void SetData(IMoveableObjects moveableObjects, int width, int height)
{
if (moveableObjects == null)
{
_state = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObject = moveableObjects;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != StrategyStatus.InProgress) return;
if (IsTargetDestinaion())
{
_state = StrategyStatus.Finish;
return;
}
MoveToTarget();
}
protected boolean MoveLeft() {return MoveTo(MovementDirection.Left);};
protected boolean MoveRight() {return MoveTo(MovementDirection.Right);};
protected boolean MoveUp() {return MoveTo(MovementDirection.Up);};
protected boolean MoveDown() {return MoveTo(MovementDirection.Down);};
protected ObjectParameters GetObjectParameters() {return _moveableObject.GetObjectPosition();};
protected Integer GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return null;
}
return _moveableObject.GetStep();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestinaion();
private boolean MoveTo(MovementDirection movementDirection)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
boolean stateTryMoveObject = _moveableObject.TryMoveObject(movementDirection);
if (stateTryMoveObject) return stateTryMoveObject;
return false;
}
}

View File

@ -0,0 +1,7 @@
package MovementStrategy;
public interface IMoveableObjects {
ObjectParameters GetObjectPosition();
int GetStep();
boolean TryMoveObject(MovementDirection direction);
}

View File

@ -0,0 +1,27 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy{
@Override
protected boolean IsTargetDestinaion() {
ObjectParameters objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.RightBorder + GetStep() >= FieldWidth-GetStep() &&
objParams.DownBorder + GetStep() >= FieldHeight-GetStep();
}
@Override
protected void MoveToTarget() {
ObjectParameters objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
//реализация в правый нижний угол
int x = objParams.RightBorder;
if (x + GetStep() < FieldWidth) MoveRight();
int y = objParams.DownBorder;
if (y + GetStep() < FieldHeight) MoveDown();
}
}

View File

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

View File

@ -0,0 +1,45 @@
package MovementStrategy;
import DrawingShip.CanvasWarmlyShip;
import DrawingShip.DirectionType;
import DrawingShip.DrawingShip;
public class MoveableShip implements IMoveableObjects{
private CanvasWarmlyShip canvas = new CanvasWarmlyShip();
public MoveableShip(DrawingShip drawningship)
{
canvas._drawingShip = drawningship;
}
@Override
public ObjectParameters GetObjectPosition() {
if (canvas._drawingShip == null || canvas._drawingShip.EntityShip == null ||
canvas._drawingShip.GetPosX() == null || canvas._drawingShip.GetPosY() == null)
{
return null;
}
return new ObjectParameters(canvas._drawingShip.GetPosX(), canvas._drawingShip.GetPosY(),
canvas._drawingShip.GetWidth(), canvas._drawingShip.GetHeight());
}
@Override
public int GetStep() {
return (int)(canvas._drawingShip.EntityShip.Step);
}
@Override
public boolean TryMoveObject(MovementDirection direction) {
if (canvas._drawingShip == null || canvas._drawingShip.EntityShip == null)
{
return false;
}
return canvas._drawingShip.MoveTransport(GetDirectionType(direction));
}
private static DirectionType GetDirectionType(MovementDirection direction)
{
switch (direction) {
case MovementDirection.Left: return DirectionType.Left;
case MovementDirection.Right: return DirectionType.Right;
case MovementDirection.Up: return DirectionType.Up;
case MovementDirection.Down: return DirectionType.Down;
default: return DirectionType.Unknow;
}
}
}

View File

@ -0,0 +1,8 @@
package MovementStrategy;
public enum MovementDirection {
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,27 @@
package MovementStrategy;
public class ObjectParameters {
private int _x;
private int _y;
private int _width;
private int _height;
public int LeftBorder = _x;
public int TopBorder = _y;
public int RightBorder = _x + _width;
public int DownBorder = _y + _height;
public int ObjectMiddleHorizontal = _x + _width / 2;
public int ObjectMiddleVertical = _y + _height / 2;
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
LeftBorder = _x;
TopBorder = _y;
RightBorder = _x + _width;
DownBorder = _y + _height;
ObjectMiddleHorizontal = _x + _width / 2;
ObjectMiddleVertical = _y + _height / 2;
}
}

View File

@ -0,0 +1,7 @@
package MovementStrategy;
public enum StrategyStatus {
NotInit,
InProgress,
Finish
}