PIBD-12_Morozov_D.V. LabWork№2 #3
@ -85,4 +85,4 @@ public class DrawningContainerShip extends DrawningShip {
|
||||
g2d.fillPolygon(container2Polygon);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,32 @@
|
||||
package ProjectContainerShip.src.Drawings;
|
||||
import java.awt.*;
|
||||
public class DrawningContainerShipDeckFull implements IDrawningDeck {
|
||||
private DeckCount _deckCount;
|
||||
public void setEnumNumber(int wheelsCount){
|
||||
for (DeckCount value : DeckCount.values()){
|
||||
if (value.getEnumNumber() == wheelsCount){
|
||||
_deckCount = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void drawContainerShipDeck(Graphics g, Color color, int startPosX, int startPosY)
|
||||
{
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.setColor(color);
|
||||
g2d.setStroke(new BasicStroke(4));
|
||||
for (int i = 0; i < _deckCount.getEnumNumber(); i++) {
|
||||
drawDeck(g2d, (int) startPosX, (int)startPosY);
|
||||
startPosY-=10;
|
||||
}
|
||||
}
|
||||
private void drawDeck(Graphics2D g2d, int posX, int posY) {
|
||||
g2d.drawLine(posX + 20, posY + 50, posX + 50, posY + 50);
|
||||
g2d.drawLine(posX + 50, posY + 50, posX + 55, posY + 45);
|
||||
g2d.drawLine(posX + 55, posY + 45, posX + 50, posY + 40);
|
||||
g2d.drawLine(posX + 50, posY + 40, posX + 20, posY + 40);
|
||||
g2d.drawLine(posX + 20, posY + 40, posX + 15, posY + 45);
|
||||
g2d.drawLine(posX + 15, posY + 45, posX + 20, posY + 50);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package ProjectContainerShip.src.Drawings;
|
||||
import java.awt.*;
|
||||
public class DrawningContainerShipDeckTrapez implements IDrawningDeck {
|
||||
private DeckCount _deckCount;
|
||||
public void setEnumNumber(int wheelsCount){
|
||||
for (DeckCount value : DeckCount.values()){
|
||||
if (value.getEnumNumber() == wheelsCount){
|
||||
_deckCount = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
// поправить
|
||||
private void drawDeck(Graphics2D g2d, int posX, int posY) {
|
||||
g2d.drawLine(posX + 20, posY + 50, posX + 50, posY + 50);
|
||||
g2d.drawLine(posX + 50, posY + 50, posX + 60, posY + 40);
|
||||
g2d.drawLine(posX + 60, posY + 40, posX + 10, posY + 40);
|
||||
g2d.drawLine(posX + 10, posY + 40, posX + 20, posY + 50);
|
||||
}
|
||||
public void drawContainerShipDeck(Graphics g, Color color, int startPosX, int startPosY)
|
||||
{
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.setColor(color);
|
||||
g2d.setStroke(new BasicStroke(4));
|
||||
for (int i = 0; i < _deckCount.getEnumNumber(); i++) {
|
||||
drawDeck(g2d, (int) startPosX, (int)startPosY);
|
||||
startPosY-=10;
|
||||
}
|
||||
}
|
||||
}
|
144
ProjectContainerShip/src/Drawings/DrawningShip.java
Normal file
144
ProjectContainerShip/src/Drawings/DrawningShip.java
Normal file
@ -0,0 +1,144 @@
|
||||
package ProjectContainerShip.src.Drawings;
|
||||
|
||||
import ProjectContainerShip.src.Entities.EntityShip;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawningShip {
|
||||
private EntityShip entityShip;
|
||||
public EntityShip getEntityShip() {
|
||||
return entityShip;
|
||||
}
|
||||
private IDrawningDeck drawningDeck;
|
||||
private Integer _pictureWidth;
|
||||
private Integer _pictureHeight;
|
||||
protected Integer _startPosX;
|
||||
protected Integer _startPosY;
|
||||
private int _drawningShipWidth = 130;
|
||||
private int _drawningShipHeight = 60;
|
||||
public int getPosX() {
|
||||
return _startPosX;
|
||||
}
|
||||
public int getPosY() {
|
||||
return _startPosY;
|
||||
}
|
||||
public int getTrainWidth() {
|
||||
return _drawningShipWidth;
|
||||
}
|
||||
public int getTrainHeight() {
|
||||
return _drawningShipHeight;
|
||||
}
|
||||
public DrawningShip(int speed, double weight, Color bodyColor, int deckType){
|
||||
entityShip = new EntityShip(speed, weight, bodyColor);
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
switch(deckType){
|
||||
case 0:
|
||||
drawningDeck = new DrawningContainerShipDeck();
|
||||
break;
|
||||
case 1:
|
||||
drawningDeck = new DrawningContainerShipDeckFull();
|
||||
break;
|
||||
case 2:
|
||||
drawningDeck = new DrawningContainerShipDeckTrapez();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
Random rand = new Random();
|
||||
int deckCount = rand.nextInt(1, 4);
|
||||
drawningDeck.setEnumNumber(deckCount);
|
||||
}
|
||||
public DrawningShip(int speed, double weight, Color bodyColor, int deckType, int drawningShipWidth, int drawningShipHeight){
|
||||
this(speed, weight, bodyColor, deckType);
|
||||
_drawningShipWidth = drawningShipWidth;
|
||||
_drawningShipHeight = drawningShipHeight;
|
||||
}
|
||||
public boolean SetPictureSize(int width, int height){
|
||||
if (_drawningShipWidth <= width && _drawningShipHeight <= height){
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_startPosX != null && _startPosY != null){
|
||||
if (_startPosX + _drawningShipWidth > _pictureWidth) {
|
||||
_startPosX = _pictureWidth - _drawningShipWidth;
|
||||
}
|
||||
if (_startPosY + _drawningShipHeight > _pictureHeight) {
|
||||
_startPosY = _pictureHeight - _drawningShipHeight;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (_pictureHeight == null || _pictureWidth == null) {
|
||||
return;
|
||||
}
|
||||
if (x + _drawningShipWidth > _pictureWidth || x < 0)
|
||||
{
|
||||
x = 0;
|
||||
}
|
||||
if (y + _drawningShipHeight > _pictureHeight || y < 0)
|
||||
{
|
||||
y = 0;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
public boolean MoveTransport(DirectionType direction){
|
||||
if (entityShip == null || _startPosX == null || _startPosY == null){
|
||||
return false;
|
||||
}
|
||||
switch (direction){
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - entityShip.Step() > 0){
|
||||
_startPosY -= (int) entityShip.Step();
|
||||
}
|
||||
return true;
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + entityShip.Step() + _drawningShipHeight < _pictureHeight){
|
||||
_startPosY += (int) entityShip.Step();
|
||||
}
|
||||
return true;
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - entityShip.Step() > 0){
|
||||
_startPosX -= (int) entityShip.Step();
|
||||
}
|
||||
return true;
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + entityShip.Step() + _drawningShipWidth < _pictureWidth){
|
||||
_startPosX += (int) entityShip.Step();
|
||||
}
|
||||
return true;
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
public void DrawShip(Graphics g) {
|
||||
if (entityShip == null || _startPosX == null || _startPosY == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
Point[] containerShipBorders = new Point[] {
|
||||
new Point(_startPosX, _startPosY + 50),
|
||||
new Point(_startPosX + 20, _startPosY + 70),
|
||||
new Point(_startPosX + 110, _startPosY + 70),
|
||||
new Point(_startPosX + 130, _startPosY + 50)
|
||||
};
|
||||
Polygon containerShipPolygon = new Polygon();
|
||||
for(Point point : containerShipBorders)
|
||||
containerShipPolygon.addPoint(point.x, point.y);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.draw(containerShipPolygon);
|
||||
g2d.setColor(entityShip.getBodyColor());
|
||||
g2d.fill(containerShipPolygon);
|
||||
drawningDeck.drawContainerShipDeck(g, entityShip.getBodyColor(), _startPosX, _startPosY);
|
||||
}
|
||||
|
||||
}
|
8
ProjectContainerShip/src/Drawings/IDrawningDeck.java
Normal file
8
ProjectContainerShip/src/Drawings/IDrawningDeck.java
Normal file
@ -0,0 +1,8 @@
|
||||
package ProjectContainerShip.src.Drawings;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawningDeck {
|
||||
void setEnumNumber(int x);
|
||||
void drawContainerShipDeck(Graphics g, Color color, int startX, int startY);
|
||||
}
|
29
ProjectContainerShip/src/Entities/EntityShip.java
Normal file
29
ProjectContainerShip/src/Entities/EntityShip.java
Normal file
@ -0,0 +1,29 @@
|
||||
package ProjectContainerShip.src.Entities;
|
||||
|
||||
import java.awt.*;
|
||||
public class EntityShip {
|
||||
private int Speed;
|
||||
public int getSpeed() {
|
||||
return Speed;
|
||||
}
|
||||
|
||||
private double Weight;
|
||||
public double getWeight() {
|
||||
return Weight;
|
||||
}
|
||||
|
||||
private Color BodyColor;
|
||||
public Color getBodyColor() {
|
||||
return BodyColor;
|
||||
}
|
||||
|
||||
public double Step() {
|
||||
return Speed*100/Weight;
|
||||
}
|
||||
|
||||
public EntityShip(int speed, double weight, Color bodyColor) {
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
@ -93,6 +93,7 @@ public class FormContainerShip extends JFrame {
|
||||
buttonLeft.addActionListener(buttonMoveClickedListener);
|
||||
buttonUp.addActionListener(buttonMoveClickedListener);
|
||||
|
||||
|
||||
comboBoxStrategy.addItem("К Центру");
|
||||
comboBoxStrategy.addItem("К Краю");
|
||||
buttonStrategyStep.addActionListener(new ActionListener() {
|
||||
@ -157,4 +158,3 @@ public class FormContainerShip extends JFrame {
|
||||
controls.add(buttonRight);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,60 @@
|
||||
package ProjectContainerShip.src.MovementStrategy;
|
||||
|
||||
import ProjectContainerShip.src.Drawings.DirectionType;
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObject _movableObject;
|
||||
private StrategyStatus _state = StrategyStatus.NotInit;
|
||||
protected int FieldWidth;
|
||||
protected int FieldHeight;
|
||||
public StrategyStatus GetStatus() { return _state; }
|
||||
|
||||
public void SetData(IMoveableObject movableObject, int width, int height) {
|
||||
if (movableObject == null)
|
||||
{
|
||||
_state = StrategyStatus.NotInit;
|
||||
return;
|
||||
}
|
||||
_state = StrategyStatus.InProgress;
|
||||
_movableObject = movableObject;
|
||||
FieldHeight = height;
|
||||
FieldWidth = width;
|
||||
}
|
||||
|
||||
public void MakeStep() {
|
||||
if (_state != StrategyStatus.InProgress) {
|
||||
return;
|
||||
}
|
||||
if (IsTargetDestination()) {
|
||||
_state = StrategyStatus.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.GetObjectPosition(); }
|
||||
|
||||
protected int GetStep() {
|
||||
if(_state != StrategyStatus.InProgress) {
|
||||
return 0;
|
||||
}
|
||||
return _movableObject.GetStep();
|
||||
}
|
||||
|
||||
protected abstract void MoveToTarget();
|
||||
protected abstract boolean IsTargetDestination();
|
||||
private boolean MoveTo(DirectionType directionType) {
|
||||
if (_state != StrategyStatus.InProgress) {
|
||||
return false;
|
||||
}
|
||||
if (_movableObject.TryMoveObject(directionType)) {
|
||||
_movableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package ProjectContainerShip.src.MovementStrategy;
|
||||
|
||||
import ProjectContainerShip.src.Drawings.DirectionType;
|
||||
public interface IMoveableObject {
|
||||
ObjectParameters GetObjectPosition();
|
||||
int GetStep();
|
||||
boolean TryMoveObject(DirectionType direction);
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
30
ProjectContainerShip/src/MovementStrategy/MovableShip.java
Normal file
30
ProjectContainerShip/src/MovementStrategy/MovableShip.java
Normal file
@ -0,0 +1,30 @@
|
||||
package ProjectContainerShip.src.MovementStrategy;
|
||||
|
||||
import ProjectContainerShip.src.Drawings.DirectionType;
|
||||
import ProjectContainerShip.src.Drawings.DrawningShip;
|
||||
|
||||
public class MovableShip implements IMoveableObject {
|
||||
private DrawningShip _ship = null;
|
||||
public MovableShip(DrawningShip train){
|
||||
_ship = train;
|
||||
}
|
||||
|
||||
public ObjectParameters GetObjectPosition() {
|
||||
if (_ship == null || _ship.getEntityShip() == null){
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_ship.getPosX(), _ship.getPosY(), _ship.getTrainWidth(), _ship.getTrainHeight());
|
||||
}
|
||||
|
||||
public int GetStep() {
|
||||
return (int) _ship.getEntityShip().Step();
|
||||
}
|
||||
|
||||
public boolean TryMoveObject(DirectionType direction) {
|
||||
return _ship.MoveTransport(direction);
|
||||
}
|
||||
|
||||
public void MoveObject(DirectionType direction) {
|
||||
_ship.MoveTransport(direction);
|
||||
}
|
||||
}
|
27
ProjectContainerShip/src/MovementStrategy/MoveToBorder.java
Normal file
27
ProjectContainerShip/src/MovementStrategy/MoveToBorder.java
Normal file
@ -0,0 +1,27 @@
|
||||
package ProjectContainerShip.src.MovementStrategy;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy {
|
||||
protected boolean IsTargetDestination() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null){
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder() <= FieldWidth && objParams.RightBorder() + GetStep() >= FieldWidth &&
|
||||
objParams.DownBorder() <= FieldHeight && objParams.DownBorder() + GetStep() >= FieldHeight;
|
||||
}
|
||||
|
||||
protected void MoveToTarget(){
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null){
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.RightBorder() - FieldWidth;
|
||||
if (Math.abs(diffX) > GetStep()){
|
||||
MoveRight();
|
||||
}
|
||||
int diffY = objParams.DownBorder() - FieldHeight;
|
||||
if (Math.abs(diffY) > GetStep()){
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
39
ProjectContainerShip/src/MovementStrategy/MoveToCenter.java
Normal file
39
ProjectContainerShip/src/MovementStrategy/MoveToCenter.java
Normal file
@ -0,0 +1,39 @@
|
||||
package ProjectContainerShip.src.MovementStrategy;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy {
|
||||
protected boolean IsTargetDestination(){
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null){
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMidHorizontal() - GetStep() <= FieldWidth / 2 &&
|
||||
objParams.ObjectMidHorizontal() + GetStep() >= FieldWidth / 2 &&
|
||||
objParams.ObjectMidVertical() - GetStep() <= FieldHeight / 2 &&
|
||||
objParams.ObjectMidVertical() + GetStep() >= FieldHeight / 2;
|
||||
}
|
||||
|
||||
protected void MoveToTarget(){
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null){
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.ObjectMidHorizontal() - FieldWidth / 2;
|
||||
if (Math.abs(diffX) > GetStep()){
|
||||
if (diffX > 0){
|
||||
MoveLeft();
|
||||
}
|
||||
else{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
int diffY = objParams.ObjectMidVertical() - FieldHeight / 2;
|
||||
if (Math.abs(diffY) > GetStep()){
|
||||
if (diffY > 0){
|
||||
MoveUp();
|
||||
}
|
||||
else{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package ProjectContainerShip.src.MovementStrategy;
|
||||
|
||||
public enum MovementDirection {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package ProjectContainerShip.src.MovementStrategy;
|
||||
|
||||
public class ObjectParameters {
|
||||
private int _x;
|
||||
private int _y;
|
||||
private int _width;
|
||||
private 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 ObjectMidHorizontal(){return _x + _width / 2;};
|
||||
public int ObjectMidVertical(){return _y + _height / 2;};
|
||||
|
||||
public ObjectParameters(int x, int y, int width, int height){
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package ProjectContainerShip.src.MovementStrategy;
|
||||
|
||||
public enum StrategyStatus {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user