8 Commits

31 changed files with 1178 additions and 109 deletions

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

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

View File

@@ -0,0 +1,64 @@
package CollectionAdditionalObjects;
import DiffetentsDrawingDecks.IDifferentDecks;
import DrawingShip.DrawingShip;
import DrawingShip.DrawingWarmlyShip;
import Entities.EntityShip;
import Entities.EntityWarmlyShip;
import java.awt.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class AdditionalCollections <T extends EntityShip, U extends IDifferentDecks>{
public T[] _collectionEntity;
public U[] _collectionDecks;
public AdditionalCollections(int size, Class<T> type1, Class<T> type2) {
_collectionEntity = (T[]) Array.newInstance(type1, size);
_collectionDecks = (U[]) Array.newInstance(type2, size);
CountEntities = size;
CountDecks = size;
}
public int CountEntities;
public int CountDecks;
public int Insert(T entity) {
int index = 0;
while (index < CountEntities) {
if (_collectionEntity[index] == null)
{
_collectionEntity[index] = entity;
return index;
}
++index;
}
return -1;
}
public int Insert(U decks) {
int index = 0;
while (index < CountDecks) {
if (_collectionDecks[index] == null)
{
_collectionDecks[index] = decks;
return index;
}
++index;
}
return -1;
}
public DrawingShip CreateAdditionalCollectionShip() {
Random random = new Random();
if (_collectionEntity == null || _collectionDecks == null) return null;
T entity = _collectionEntity[random.nextInt(CountEntities)];
U decks = _collectionDecks[random.nextInt(CountDecks)];
DrawingShip drawingShip = null;
if (entity instanceof EntityWarmlyShip) {
drawingShip = new DrawingWarmlyShip((EntityWarmlyShip) entity, decks);
}
else {
drawingShip = new DrawingShip(entity, decks);
}
return drawingShip;
}
}

View File

@@ -0,0 +1,34 @@
package CollectionGenericObjects;
import DrawingShip.DrawingShip;
import java.awt.*;
public abstract class AbstractCompany {
protected int _placeSizeWidth = 210;
protected int _placeSizeHeight = 150;
protected int _pictureWidth;
protected int _pictureHeight;
public ICollectionGenericObjects<DrawingShip> _collection = null;
private int GetMaxCount() {
return _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight);
}
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawingShip> collection)
{
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = collection;
_collection.SetMaxCount(GetMaxCount(), (Class) DrawingShip.class);
}
//перегрузка операторов в джаве не возможна
public DrawingShip GetRandomObject()
{
return _collection.Get((int)(Math.random()*GetMaxCount() + 0));
}
public void SetPosition()
{
SetObjectsPosition();
}
public abstract void DrawBackgound(Graphics graphics);
protected abstract void SetObjectsPosition();
}

View File

@@ -0,0 +1,12 @@
package CollectionGenericObjects;
public interface ICollectionGenericObjects<T>
{
int getCount();
void SetMaxCount(int count, Class<T> type);
int Insert(T obj);
int Insert(T obj, int position);
T Remove(int position);
T Get(int position);
}

View File

@@ -0,0 +1,77 @@
package CollectionGenericObjects;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{
private T[] _collection;
private int Count;
public void SetMaxCount(int size, Class<T> type) {
if (size > 0) {
_collection = (T[]) Array.newInstance(type, size);
Count = size;
}
}
@Override
public int getCount() {
return Count;
}
@Override
public int Insert(T obj) {
int index = 0;
while (index < getCount())
{
if (_collection[index] == null)
{
_collection[index] = obj;
return index;
}
++index;
}
return -1;
}
@Override
public int Insert(T obj, int position) {
if (position >= getCount() || position < 0)
return -1;
if (_collection[position] == null) {
_collection[position] = obj;
return position;
}
int index = position + 1;
while (index < getCount())
{
if (_collection[index] == null)
{
_collection[index] = obj;
return index;
}
++index;
}
index = position - 1;
while (index >= 0)
{
if (_collection[index] == null)
{
_collection[index] = obj;
return index;
}
--index;
}
return -1;
}
@Override
public T Remove(int position) {
if (position >= getCount() || position < 0)
return null;
T obj = (T) _collection[position];
_collection[position] = null;
return obj;
}
@Override
public T Get(int position) {
if (position >= getCount() || position < 0) return null;
return (T) _collection[position];
}
}

View File

@@ -0,0 +1,50 @@
package CollectionGenericObjects;
import DrawingShip.DrawingShip;
import java.awt.*;
public class ShipPortService extends AbstractCompany{
public ShipPortService(int picWidth, int picHeight, ICollectionGenericObjects<DrawingShip> collection) {
super(picWidth, picHeight, collection);
}
@Override
public void DrawBackgound(Graphics g) {
//рисуем пристань
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
g.setColor(Color.BLACK);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height + 1; ++j)
{
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight,
i * _placeSizeWidth + _placeSizeWidth - 5, j * _placeSizeHeight );
}
}
}
@Override
protected void SetObjectsPosition() {
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int curWidth = width - 1;
int curHeight = 0;
for (int i = 0; i < (_collection.getCount()); i++) {
if (_collection.Get(i) != null) {
_collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight);
_collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 20, curHeight * _placeSizeHeight + 4);
}
if (curWidth > 0)
curWidth--;
else {
curWidth = width - 1;
curHeight++;
}
if (curHeight > height) {
return;
}
}
}
}

View File

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

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

@@ -1,3 +1,5 @@
package DiffetentsDrawingDecks;
public enum NumberOfDecks { public enum NumberOfDecks {
OneDeck(1), OneDeck(1),
TwoDecks(2), TwoDecks(2),

View File

@@ -0,0 +1,35 @@
package DrawingShip;
import CollectionGenericObjects.AbstractCompany;
import CollectionGenericObjects.ICollectionGenericObjects;
import CollectionGenericObjects.MassiveGenericObjects;
import javax.swing.*;
import java.awt.*;
public class CanvasFormShipCollection<T> extends JComponent
{
//неиспользуемые методы
public ICollectionGenericObjects<T> collection = new MassiveGenericObjects<T>();
public AbstractCompany company = null;
public void SetCollectionToCanvas(AbstractCompany company) {
this.company = company;
}
public CanvasFormShipCollection(){};
public void paintComponent(Graphics g) {
super.paintComponents(g);
if (company == null || company._collection == null) {
return;
}
company.DrawBackgound(g);
for (int i = 0; i < company._collection.getCount(); i++) {
Graphics2D g2d = (Graphics2D) g;
T obj = (T)company._collection.Get(i);
if (obj instanceof DrawingShip) {
((DrawingShip) obj).DrawTransport(g2d);
}
}
super.repaint();
}
}

View File

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

View File

@@ -1,4 +1,7 @@
package DrawingShip;
public enum DirectionType { public enum DirectionType {
Unknow,
Up, Up,
Down, Down,
Left, Left,

View File

@@ -1,24 +1,65 @@
package DrawingShip;
import DiffetentsDrawingDecks.DrawingDecksType2;
import DiffetentsDrawingDecks.DrawingDecksType3;
import DiffetentsDrawingDecks.IDifferentDecks;
import Entities.EntityShip;
import DiffetentsDrawingDecks.DrawingDecksType1;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
public class DrawingWarmlyShip extends JPanel { public class DrawingShip extends JPanel {
public EntityWarmlyShip EntityWarmlyShip; public Entities.EntityShip EntityShip;
public DrawingDecks drawingDecks = null; public IDifferentDecks drawingDecks;
private Integer picture_width; private Integer picture_width;
private Integer picture_height; private Integer picture_height;
private Integer _StartPosX; public Integer _StartPosX;
private Integer _StartPosY; public Integer _StartPosY;
private int drawingShipWidth = 150; private int drawingShipWidth = 150;
private int drawingShipHeight = 140; protected int drawingShipHeight = 50;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean sheeppipes, boolean fueltank) { public Integer GetPosX() {return _StartPosX;}
EntityWarmlyShip = new EntityWarmlyShip(); public Integer GetPosY() {return _StartPosY;}
EntityWarmlyShip.Init(speed, weight, bodycolor, additionalcolor, sheeppipes, fueltank); public Integer GetWidth() {return drawingShipWidth;}
public Integer GetHeight() {return drawingShipHeight;}
protected DrawingShip() {
picture_width = null; picture_width = null;
picture_height = null; picture_height = null;
_StartPosX = null; _StartPosX = null;
_StartPosY = null; _StartPosY = null;
drawingDecks = new DrawingDecks(); }
drawingDecks.setNumberOfDecks((int)(Math.random() * 4 + 0)); 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();
}
protected DrawingShip(int drawingShipWidth, int drawingShipHeight) {
super();
this.drawingShipWidth = drawingShipWidth;
this.drawingShipHeight = drawingShipHeight;
}
//добавил
public DrawingShip(EntityShip entity, IDifferentDecks decks) {
EntityShip = entity;
drawingDecks = decks;
} }
public boolean SetPictureSize(int width, int height) { public boolean SetPictureSize(int width, int height) {
if (width < drawingShipWidth || height < drawingShipHeight) return false; if (width < drawingShipWidth || height < drawingShipHeight) return false;
@@ -54,29 +95,29 @@ public class DrawingWarmlyShip extends JPanel {
else _StartPosY = y; else _StartPosY = y;
} }
public boolean MoveTransport(DirectionType direction) { public boolean MoveTransport(DirectionType direction) {
if (EntityWarmlyShip == null || _StartPosX == null || _StartPosY == null) return false; if (EntityShip == null || _StartPosX == null || _StartPosY == null) return false;
switch (direction) { switch (direction) {
case Left: case DirectionType.Left:
if (_StartPosX - EntityWarmlyShip.Step > 0) { if (_StartPosX - EntityShip.Step > 0) {
_StartPosX -= (int)EntityWarmlyShip.Step; _StartPosX -= (int)EntityShip.Step;
} }
return true; return true;
case Up: case DirectionType.Up:
if (_StartPosY - EntityWarmlyShip.Step > 0) if (_StartPosY - EntityShip.Step > 0)
{ {
_StartPosY -= (int)EntityWarmlyShip.Step; _StartPosY -= (int)EntityShip.Step;
} }
return true; return true;
case Right: case DirectionType.Right:
if (_StartPosX + drawingShipWidth + (int)EntityWarmlyShip.Step < picture_width - EntityWarmlyShip.Step) if (_StartPosX + drawingShipWidth + (int)EntityShip.Step < picture_width - EntityShip.Step)
{ {
_StartPosX += (int)EntityWarmlyShip.Step; _StartPosX += (int)EntityShip.Step;
} }
return true; return true;
case Down: case DirectionType.Down:
if (_StartPosY + drawingShipHeight + (int)EntityWarmlyShip.Step < picture_height - EntityWarmlyShip.Step) if (_StartPosY + drawingShipHeight + (int)EntityShip.Step < picture_height - EntityShip.Step)
{ {
_StartPosY += (int)EntityWarmlyShip.Step; _StartPosY += (int)EntityShip.Step;
} }
return true; return true;
default: default:
@@ -84,31 +125,24 @@ public class DrawingWarmlyShip extends JPanel {
} }
} }
public void DrawTransport(Graphics2D g) { public void DrawTransport(Graphics2D g) {
if (EntityWarmlyShip == null || _StartPosX == null || _StartPosY == null) return; if (EntityShip == null || _StartPosX == null || _StartPosY == null) return;
int y = _StartPosY; int y = _StartPosY;
if (EntityWarmlyShip.getShipPipes()) { g.setColor(EntityShip.getBodyColor());
//трубы if (drawingDecks != null && drawingDecks.getNumberOfDecks() != null) {
g.setColor(EntityWarmlyShip.getAdditionalColor());
g.fillRect(_StartPosX + 70, _StartPosY, 12, 30);
g.fillRect(_StartPosX + 90, _StartPosY, 12, 30);
y += 30;
}
g.setColor(EntityWarmlyShip.getBodyColor());
if (drawingDecks.getNumberOfDecks() != null) {
switch (drawingDecks.getNumberOfDecks()) { switch (drawingDecks.getNumberOfDecks()) {
case OneDeck: case OneDeck:
drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityWarmlyShip.getBodyColor()); drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityShip.getBodyColor());
y += 15; y += 15;
break; break;
case TwoDecks: case TwoDecks:
drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityWarmlyShip.getBodyColor()); drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityShip.getBodyColor());
drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityWarmlyShip.getBodyColor()); drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityShip.getBodyColor());
y += 30; y += 30;
break; break;
case ThreeDecks: case ThreeDecks:
drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityWarmlyShip.getBodyColor()); drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityShip.getBodyColor());
drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityWarmlyShip.getBodyColor()); drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityShip.getBodyColor());
drawingDecks.DrawDecks(g, _StartPosX + 30, y + 30, 100, 15, EntityWarmlyShip.getBodyColor()); drawingDecks.DrawDecks(g, _StartPosX + 30, y + 30, 100, 15, EntityShip.getBodyColor());
y += 45; y += 45;
break; break;
} }
@@ -117,11 +151,6 @@ public class DrawingWarmlyShip extends JPanel {
int[] arrayY = {y, y, y, y + 50, y + 50, y + 50, y + 50, y}; int[] arrayY = {y, y, y, y + 50, y + 50, y + 50, y + 50, y};
Polygon poly = new Polygon(arrayX, arrayY, 8); Polygon poly = new Polygon(arrayX, arrayY, 8);
g.fillPolygon(poly); g.fillPolygon(poly);
g.setColor(EntityWarmlyShip.getAdditionalColor());
if (EntityWarmlyShip.getFuelTank()) {
g.setColor(EntityWarmlyShip.getAdditionalColor());
g.fillRect(_StartPosX + 40, y + 30, 70, 10);
}
drawingShipHeight = y + 50 - _StartPosY; drawingShipHeight = y + 50 - _StartPosY;
} }
} }

View File

@@ -0,0 +1,54 @@
package DrawingShip;
import DiffetentsDrawingDecks.IDifferentDecks;
import Entities.EntityShip;
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();
}
//добавил
public DrawingWarmlyShip(EntityWarmlyShip entity, IDifferentDecks decks) {
EntityShip = entity;
drawingDecks = decks;
}
@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 != null && 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.getFuelTank()) {
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,17 @@
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 boolean getFuelTank() {return 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

@@ -1,24 +0,0 @@
import java.awt.*;
public class EntityWarmlyShip {
private int Speed;
private double Weight;
private Color BodyColor;
public Color getBodyColor() {return BodyColor;}
private Color AdditionalColor;
public Color getAdditionalColor() {return AdditionalColor;}
private boolean ShipPipes;
public boolean getShipPipes() {return ShipPipes;}
private boolean FuelTank;
public boolean getFuelTank() {return FuelTank;}
public double Step;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean shippipes, boolean fueltank)
{
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
AdditionalColor = additionalcolor;
ShipPipes = shippipes;
FuelTank = fueltank;
Step = Speed * 100 / Weight;
}
}

View File

@@ -0,0 +1,138 @@
import CollectionAdditionalObjects.AdditionalCollections;
import CollectionGenericObjects.AbstractCompany;
import DiffetentsDrawingDecks.DrawingDecksType1;
import DiffetentsDrawingDecks.DrawingDecksType2;
import DiffetentsDrawingDecks.DrawingDecksType3;
import DiffetentsDrawingDecks.IDifferentDecks;
import DrawingShip.DrawingShip;
import DrawingShip.DrawingWarmlyShip;
import DrawingShip.CanvasWarmlyShip;
import Entities.EntityShip;
import Entities.EntityWarmlyShip;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormAdditionalCollection extends JFrame {
public DrawingShip drawingShip = null;
private AbstractCompany company = null;
private CanvasWarmlyShip canvasship = new CanvasWarmlyShip();
private AdditionalCollections<EntityShip, IDifferentDecks> additionalCollection = null;
private Random random = new Random();
private JButton buttonGenerate = new JButton("Создать");
private JList<String> listEntity = new JList<String>();
private JList<String> listDecks = new JList<String>();
public FormAdditionalCollection() {
setTitle("Random ship");
setMinimumSize(new Dimension(650,310));
additionalCollection = new AdditionalCollections<EntityShip, IDifferentDecks>(3,
(Class) EntityShip.class, (Class) IDifferentDecks.class);
AddEntities();
AddDecks();
buttonGenerate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
drawingShip = additionalCollection.CreateAdditionalCollectionShip();
drawingShip.SetPictureSize(getWidth(), getHeight());
drawingShip.SetPosition(50,50);
canvasship._drawingShip = drawingShip;
canvasship.repaint();
DrawingShip copyShip;
if (drawingShip instanceof DrawingWarmlyShip)
copyShip = new DrawingWarmlyShip((EntityWarmlyShip) drawingShip.EntityShip, drawingShip.drawingDecks);
else
copyShip = new DrawingShip(drawingShip.EntityShip, drawingShip.drawingDecks);
company._collection.Insert(copyShip);
FormShipCollection.canvasShow();
String[] data1 = new String[additionalCollection.CountEntities];
for (int i = 0; i < additionalCollection.CountEntities; i++) {
EntityShip entity = additionalCollection._collectionEntity[i];
data1[i] = ToString(entity);
}
String[] data2 = new String[additionalCollection.CountDecks];
for (int i = 0; i < additionalCollection.CountDecks; i++) {
IDifferentDecks decks = additionalCollection._collectionDecks[i];
data2[i] = ToString(decks);
}
listEntity.setListData(data1);
listDecks.setListData(data2);
}
});
buttonGenerate.setBounds(450, 10, 100, 50);
add(buttonGenerate);
listEntity.setBounds(10,200,300,60);
listDecks.setBounds(320,200,300,60);
add(listEntity);
add(listDecks);
add(canvasship);
setVisible(true);
}
private String ToString(EntityShip entity) {
String str = "";
if (entity instanceof EntityWarmlyShip) str += "EntityWarmlyShip ";
else str += "EntityShip ";
str += entity.getBodyColor().toString();
return str;
}
private String ToString(IDifferentDecks decks) {
if (decks == null || decks.getNumberOfDecks() == null)
return "Dont have decks";
String str = "Deck ";
if (decks instanceof DrawingDecksType1) str += "Type 1 ";
else if (decks instanceof DrawingDecksType2) str += "Type 2 ";
else str += "Type 3 ";
str += decks.getNumberOfDecks().toString();
return str;
}
public void AddEntities() {
for (int i = 0; i < additionalCollection.CountEntities; i++) {
random = new Random();
int speed = random.nextInt(100, 300);
double weight = random.nextInt(1000, 3000);
Color bodycolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
EntityShip entity;
if (random.nextBoolean()) {
entity = new EntityShip(speed, weight, bodycolor);
}
else {
Color additionalcolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
boolean sheeppipes = random.nextBoolean();
boolean fueltaln = random.nextBoolean();
entity = new EntityWarmlyShip(speed, weight, bodycolor,
additionalcolor, sheeppipes, fueltaln);
}
additionalCollection.Insert(entity);
}
}
public void AddDecks() {
for (int i = 0; i < additionalCollection.CountDecks; i++) {
random = new Random();
Integer numberOfDecks = random.nextInt(0, 4);
IDifferentDecks drawingDecks = null;
switch (random.nextInt(0,4)) {
case 1:
drawingDecks = new DrawingDecksType1();
break;
case 2:
drawingDecks = new DrawingDecksType2();
break;
case 3:
drawingDecks = new DrawingDecksType3();
break;
default:
numberOfDecks = null;
break;
}
if (drawingDecks != null) drawingDecks.setNumberOfDecks(numberOfDecks);
additionalCollection.Insert(drawingDecks);
}
}
void setCompany(AbstractCompany company) {
this.company = company;
}
}

View File

@@ -0,0 +1,220 @@
import CollectionGenericObjects.AbstractCompany;
import CollectionGenericObjects.MassiveGenericObjects;
import CollectionGenericObjects.ShipPortService;
import DrawingShip.CanvasFormShipCollection;
import DrawingShip.DrawingShip;
import DrawingShip.DrawingWarmlyShip;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.text.ParseException;
import java.util.Random;
import static java.lang.Integer.parseInt;
public class FormShipCollection extends JFrame{
private String title;
private Dimension dimension;
public static CanvasFormShipCollection<DrawingShip> _canvasWarmlyShip = new CanvasFormShipCollection<DrawingShip>();
private static AbstractCompany _company = null;
private JButton CreateButton = new JButton("Create warmlyship");;
private JButton CreateShipButton = new JButton("Create ship");
private JButton RemoveButton = new JButton("Remove");
private JButton GoToCheckButton = new JButton("Check");
private JButton RandomButton = new JButton("RandomShip");
private JButton RefreshButton = new JButton("Refresh");
private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"});
private JFormattedTextField MaskedTextField;
public FormShipCollection(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public static void canvasShow() {
_company.SetPosition();
_canvasWarmlyShip.SetCollectionToCanvas(_company);
_canvasWarmlyShip.repaint();
}
private void CreateObject(String typeOfClass) {
if (_company == null) return;
int speed = (int)(Math.random() * 300 + 100);
double weight = (double)(Math.random() * 3000 + 1000);
Color bodyColor = getColor();
DrawingShip drawingShip;
switch (typeOfClass) {
case "DrawingShip":
drawingShip = new DrawingShip(speed, weight, bodyColor);
break;
case "DrawingWarmlyShip":
Color additionalColor = getColor();
boolean sheepPipes = new Random().nextBoolean();
boolean fuelTank = new Random().nextBoolean();;
drawingShip = new DrawingWarmlyShip(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank);
break;
default: return;
}
if (_company._collection.Insert(drawingShip, 0) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
canvasShow();
}
else {
JOptionPane.showMessageDialog(null, "Объект не удалось добавить");
}
}
public Color getColor() {
Color initializator = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
Color color = JColorChooser.showDialog(this, "Select a color", initializator);
return color;
}
public void Init() {
setTitle(title);
setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MaskFormatter mask = null;
try {
mask = new MaskFormatter("##");
mask.setPlaceholder("00");
} catch (ParseException e) {
throw new RuntimeException(e);
}
MaskedTextField = new JFormattedTextField(mask);
ComboBoxCollections.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (ComboBoxCollections.getSelectedItem().toString()) {
case "Хранилище":
_company = new ShipPortService(getWidth()-200, getHeight()-70, new MassiveGenericObjects<DrawingShip>());
break;
}
}
});
CreateShipButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingShip");
}
});
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingWarmlyShip");
}
});
RemoveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null || MaskedTextField.getText() == null) {
return;
}
int pos = parseInt(MaskedTextField.getText());
int resultConfirmDialog = JOptionPane.showConfirmDialog(null,
"Удалить", "Удаление",
JOptionPane.YES_NO_OPTION);
if (resultConfirmDialog == JOptionPane.NO_OPTION) return;
if (_company._collection.Remove(pos) != null) {
JOptionPane.showMessageDialog(null, "Объект удален");
canvasShow();
}
else {
JOptionPane.showMessageDialog(null, "Объект не удалось удалить");
}
}
});
GoToCheckButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null)
{
return;
}
DrawingShip ship = null;
int counter = 100;
while (ship == null)
{
ship = _company.GetRandomObject();
counter--;
if (counter <= 0)
{
break;
}
}
if (ship == null)
{
return;
}
FormWarmlyShip form = new FormWarmlyShip("Теплоход", new Dimension(900,565));
form.Init(ship);
}
});
RandomButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null)
{
return;
}
FormAdditionalCollection form = new FormAdditionalCollection();
form.setCompany(_company);
}
});
RefreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null)
{
return;
}
canvasShow();
}
});
_canvasWarmlyShip.setBounds(0, 0, getWidth()-200, getHeight());
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
CreateShipButton.setBounds(getWidth()-190, 60, 150, 30);
CreateButton.setBounds(getWidth()-190, 100, 150, 30);
MaskedTextField.setBounds(getWidth()-190,200,150,30);
RemoveButton.setBounds(getWidth()-190, 240, 150, 30);
GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30);
RandomButton.setBounds(getWidth()-190, 320, 150, 30);
RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30);
setSize(dimension.width,dimension.height);
setLayout(null);
add(_canvasWarmlyShip);
add(ComboBoxCollections);
add(CreateShipButton);
add(CreateButton);
add(MaskedTextField);
add(RemoveButton);
add(GoToCheckButton);
add(RandomButton);
add(RefreshButton);
setVisible(true);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
_canvasWarmlyShip.setBounds(0, 0, getWidth()-200, getHeight()-70);
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
CreateShipButton.setBounds(getWidth()-190, 60, 150, 30);
CreateButton.setBounds(getWidth()-190, 100, 150, 30);
MaskedTextField.setBounds(getWidth()-190,200,150,30);
RemoveButton.setBounds(getWidth()-190, 240, 150, 30);
GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30);
RandomButton.setBounds(getWidth()-190, 320, 150, 30);
RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30);
}
});
}
}

View File

@@ -1,33 +1,43 @@
import DrawingShip.CanvasFormShipCollection;
import DrawingShip.CanvasWarmlyShip;
import DrawingShip.DirectionType;
import DrawingShip.DrawingShip;
import MovementStrategy.*;
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.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter; import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormWarmlyShip extends JFrame { public class FormWarmlyShip extends JFrame {
private String title; private String title;
private Dimension dimension; private Dimension dimension;
private int Width, Height; private int Width, Height;
private CanvasWarmlyShip canvasWarmlyShip = new CanvasWarmlyShip(); public CanvasWarmlyShip canvasWarmlyShip = new CanvasWarmlyShip();
private JButton CreateButton = new JButton("Create");;
private JButton UpButton = new JButton(); private JButton UpButton = new JButton();
private JButton DownButton = new JButton();; private JButton DownButton = new JButton();;
private JButton LeftButton = new JButton();; private JButton LeftButton = new JButton();;
private JButton RightButton = new JButton(); private JButton RightButton = new JButton();
private AbstractStrategy _strategy;
private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
private JButton ButtonStrategy = new JButton("Шаг");
public FormWarmlyShip(String title, Dimension dimension) { public FormWarmlyShip(String title, Dimension dimension) {
this.title = title; this.title = title;
this.dimension = dimension; this.dimension = dimension;
} }
public void Init() { public void Init(DrawingShip ship) {
setTitle(title); setTitle(title);
setMinimumSize(dimension); setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Width = getWidth() - 15;
Height = getHeight() - 35;
CreateButton.setName("CREATE"); Width = getWidth() - 10;
Height = getHeight() - 34;
ComboBoxStrategy.setEnabled(true);
_strategy = null;
canvasWarmlyShip._drawingShip = ship;
Icon iconUp = new ImageIcon("src\\images\\up.jpg"); Icon iconUp = new ImageIcon("src\\images\\up.jpg");
UpButton.setIcon(iconUp); UpButton.setIcon(iconUp);
UpButton.setName("UP"); UpButton.setName("UP");
@@ -41,42 +51,62 @@ public class FormWarmlyShip extends JFrame {
Icon iconRight = new ImageIcon("src\\images\\right.jpg"); Icon iconRight = new ImageIcon("src\\images\\right.jpg");
RightButton.setIcon(iconRight); RightButton.setIcon(iconRight);
CreateButton.addActionListener(new ActionListener() { ButtonStrategy.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
int StartPositionX = (int)(Math.random() * 90 + 10); if (canvasWarmlyShip._drawingShip == null) return;
int StartPositionY = (int)(Math.random() * 90 + 10); if (ComboBoxStrategy.isEnabled())
int speed = (int)(Math.random() * 300 + 100); {
double weight = (double)(Math.random() * 3000 + 1000); int index = ComboBoxStrategy.getSelectedIndex();
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); switch(index)
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));; {
boolean sheepPipes = new Random().nextBoolean(); case 0:
boolean fuelTank = new Random().nextBoolean();; _strategy = new MoveToCenter();
canvasWarmlyShip._drawingWarmlyShip = new DrawingWarmlyShip(); break;
canvasWarmlyShip._drawingWarmlyShip.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank); case 1:
canvasWarmlyShip._drawingWarmlyShip.SetPictureSize(Width, Height); _strategy = new MoveToBorder();
canvasWarmlyShip._drawingWarmlyShip.SetPosition( StartPositionX, StartPositionY); break;
canvasWarmlyShip.repaint(); default:
_strategy = null;
break;
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableShip(canvasWarmlyShip._drawingShip), Width, Height);
}
if (_strategy == null)
{
return;
}
ComboBoxStrategy.setEnabled(false);
_strategy.MakeStep();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
ComboBoxStrategy.setEnabled(true);
_strategy = null;
}
} }
}); });
ActionListener actionListener = new ActionListener() { ActionListener actionListener = new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent event) { public void actionPerformed(ActionEvent event) {
if (canvasWarmlyShip._drawingWarmlyShip == null) return; if (canvasWarmlyShip._drawingShip == null) return;
boolean result = false; boolean result = false;
switch ((((JButton)(event.getSource())).getName())) { switch ((((JButton)(event.getSource())).getName())) {
case "UP": case "UP":
result = canvasWarmlyShip._drawingWarmlyShip.MoveTransport(DirectionType.Up); result = canvasWarmlyShip._drawingShip.MoveTransport(DirectionType.Up);
break; break;
case "DOWN": case "DOWN":
result = canvasWarmlyShip._drawingWarmlyShip.MoveTransport(DirectionType.Down); result = canvasWarmlyShip._drawingShip.MoveTransport(DirectionType.Down);
break; break;
case "LEFT": case "LEFT":
result = canvasWarmlyShip._drawingWarmlyShip.MoveTransport(DirectionType.Left); result = canvasWarmlyShip._drawingShip.MoveTransport(DirectionType.Left);
break; break;
case "RIGHT": case "RIGHT":
result = canvasWarmlyShip._drawingWarmlyShip.MoveTransport(DirectionType.Right); result = canvasWarmlyShip._drawingShip.MoveTransport(DirectionType.Right);
break; break;
} }
if (result) { if (result) {
@@ -92,31 +122,34 @@ public class FormWarmlyShip extends JFrame {
setSize(dimension.width,dimension.height); setSize(dimension.width,dimension.height);
setLayout(null); setLayout(null);
canvasWarmlyShip.setBounds(0,0, getWidth(), getHeight()); canvasWarmlyShip.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50); LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
add(CreateButton); ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
add(UpButton); add(UpButton);
add(DownButton); add(DownButton);
add(RightButton); add(RightButton);
add(LeftButton); add(LeftButton);
add(ComboBoxStrategy);
add(ButtonStrategy);
add(canvasWarmlyShip); add(canvasWarmlyShip);
setVisible(true); setVisible(true);
//обработка события изменения размеров окна //обработка события изменения размеров окна
addComponentListener(new ComponentAdapter() { addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
Width = getWidth() - 15; Width = getWidth() - 10;
Height = getHeight() - 35; Height = getHeight() - 34;
if (canvasWarmlyShip._drawingWarmlyShip != null) if (canvasWarmlyShip._drawingShip != null)
canvasWarmlyShip._drawingWarmlyShip.SetPictureSize(Width, Height); canvasWarmlyShip._drawingShip.SetPictureSize(Width, Height);
canvasWarmlyShip.setBounds(0,0, getWidth(), getHeight()); canvasWarmlyShip.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50); LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
} }
}); });
} }

View File

@@ -2,7 +2,7 @@ import java.awt.*;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
FormWarmlyShip form = new FormWarmlyShip("Теплоход", new Dimension(500,500)); FormShipCollection form = new FormShipCollection("Коллекция судов", new Dimension(1100, 648));
form.Init(); form.Init();
} }
} }

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,46 @@
package MovementStrategy;
import DrawingShip.CanvasFormShipCollection;
import DrawingShip.DirectionType;
import DrawingShip.DrawingShip;
import DrawingShip.CanvasWarmlyShip;
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,9 @@
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
}