Compare commits

...

5 Commits

34 changed files with 1629 additions and 1 deletions

BIN
src/res/arrowDown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 699 B

BIN
src/res/arrowLeft.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 713 B

BIN
src/res/arrowRight.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 711 B

BIN
src/res/arrowUp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 687 B

View File

@ -0,0 +1,61 @@
package CollectionAdditionalObjects;
import DifferentBlocks.IDrawingBlocks;
import Drawings.DrawingWarship;
import Drawings.DrawingBattleship;
import Entities.EntityBattleship;
import Entities.EntityWarship;
import java.lang.reflect.Array;
import java.util.Random;
public class AdditionalCollections <T extends EntityWarship, U extends IDrawingBlocks>{
public T[] _collectionEntity;
public U[] _collectionBlocks;
public AdditionalCollections(int size, Class<T> type1, Class<T> type2) {
_collectionEntity = (T[]) Array.newInstance(type1, size);
_collectionBlocks = (U[]) Array.newInstance(type2, size);
CountEntities = size;
CountBlocks = size;
}
public int CountEntities;
public int CountBlocks;
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 blocks) {
int index = 0;
while (index < CountBlocks) {
if (_collectionBlocks[index] == null)
{
_collectionBlocks[index] = blocks;
return index;
}
++index;
}
return -1;
}
public DrawingWarship CreateAdditionalCollectionWarship() {
Random random = new Random();
if (_collectionEntity == null || _collectionBlocks == null) return null;
T entity = _collectionEntity[random.nextInt(CountEntities)];
U blocks = _collectionBlocks[random.nextInt(CountBlocks)];
DrawingWarship drawingWarship = null;
if (entity instanceof EntityBattleship) {
drawingWarship = new DrawingBattleship((EntityBattleship) entity, blocks);
}
else {
drawingWarship = new DrawingWarship(entity, blocks);
}
return drawingWarship;
}
}

View File

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

View File

@ -0,0 +1,11 @@
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,75 @@
package CollectionGenericObjects;
import java.lang.reflect.Array;
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 Drawings.DrawingWarship;
import java.awt.*;
public class WarshipSharingService extends AbstractCompany{
public WarshipSharingService(int picWidth, int picHeight, ICollectionGenericObjects<DrawingWarship> collection) {
super(picWidth, picHeight, collection);
}
@Override
public void DrawBackground(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 / 2, j * _placeSizeHeight);
}
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight);
}
}
//установка объектов
@Override
protected void SetObjectsPosition() {
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int posWidth = 0;
int posHeight = height - 1;
for (int i = 0; i < (_collection.getCount()); i++) {
if (_collection.Get(i) != null) {
_collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight);
_collection.Get(i).SetPosition(_placeSizeWidth * posWidth + 4, posHeight * _placeSizeHeight + 4);
}
if (posWidth < width - 1)
posWidth++;
else {
posWidth = 0;
posHeight--;
}
if (posHeight > height) {
return;
}
}
}
}

View File

@ -0,0 +1,103 @@
package DifferentBlocks;
import java.awt.*;
public class DrawingBlocks implements IDrawingBlocks {
private NumBlocks numBlocks;
//определяет количество блоков
public void setNumBlocks(int number) {
switch (number) {
case 1:
numBlocks = NumBlocks.Two;
break;
case 2:
numBlocks = NumBlocks.Four;
break;
case 3:
numBlocks = NumBlocks.Six;
break;
default:
numBlocks = NumBlocks.Four;
}
}
@Override
public NumBlocks getNumBlocks() {
return numBlocks;
}
public void drawBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
switch (numBlocks) {
case Two:
drawTwoBlocks(g2D, color, _startPosX, _startPosY);
break;
case Four:
drawFourBlocks(g2D, color, _startPosX, _startPosY);
break;
case Six:
drawSixBlocks(g2D, color, _startPosX, _startPosY);
break;
}
}
private void drawTwoBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
//прямоугольник левый на палубе (горизонтальный)
g2D.setColor(Color.LIGHT_GRAY);
g2D.drawRect(_startPosX + 32, _startPosY + 13, 21, 12);
g2D.fillRect(_startPosX + 32, _startPosY + 13, 21, 12);
//прямоугольник правый на палубе (вертикальный)
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 53, _startPosY + 7, 16, 24);
g2D.fillRect(_startPosX + 53, _startPosY + 7, 16, 24);
}
private void drawFourBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
//прямоугольник левый на палубе (горизонтальный)
g2D.setColor(Color.magenta);
g2D.drawRect(_startPosX + 32, _startPosY + 13, 8, 12);
g2D.fillRect(_startPosX + 32, _startPosY + 13, 8, 12);
//прямоугольник правый на палубе (вертикальный)
g2D.setColor(Color.GREEN);
g2D.drawRect(_startPosX + 53, _startPosY + 8, 16, 8);
g2D.fillRect(_startPosX + 53, _startPosY + 8, 16, 8);
g2D.setColor(Color.GREEN);
g2D.drawRect(_startPosX + 53, _startPosY + 23, 16, 8);
g2D.fillRect(_startPosX + 53, _startPosY + 23, 16, 8);
g2D.setColor(Color.DARK_GRAY);
g2D.drawRect(_startPosX + 19, _startPosY + 13, 8, 12);
g2D.fillRect(_startPosX + 19, _startPosY + 13, 8, 12);
}
private void drawSixBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 19, _startPosY + 13, 10, 13);
g2D.fillRect(_startPosX + 19, _startPosY + 13, 10, 13);
g2D.setColor(Color.LIGHT_GRAY);
g2D.drawRect(_startPosX + 31, _startPosY + 15, 12, 9);
g2D.fillRect(_startPosX + 31, _startPosY + 15, 12, 9);
g2D.setColor(Color.PINK);
g2D.drawRect(_startPosX + 46, _startPosY + 13, 10, 13);
g2D.fillRect(_startPosX + 46, _startPosY + 13, 10, 13);
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 64, _startPosY + 4, 7, 8);
g2D.fillRect(_startPosX + 64, _startPosY + 4, 7, 8);
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 64, _startPosY + 15, 7, 8);
g2D.fillRect(_startPosX + 64, _startPosY + 15, 7, 8);
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 64, _startPosY + 28, 7, 8);
g2D.fillRect(_startPosX + 64, _startPosY + 28, 7, 8);
}
}

View File

@ -0,0 +1,104 @@
package DifferentBlocks;
import java.awt.*;
public class DrawingBlocksType1 implements IDrawingBlocks {
private NumBlocks numBlocks;
@Override
public NumBlocks getNumBlocks() {
return null;
}
@Override
public void setNumBlocks(int number) {
switch (number) {
case 1:
numBlocks = NumBlocks.Two;
break;
case 2:
numBlocks = NumBlocks.Four;
break;
case 3:
numBlocks = NumBlocks.Six;
break;
default:
numBlocks = NumBlocks.Four;
}
}
@Override
public void drawBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
switch (numBlocks) {
case Two:
drawTwoBlocks(g2D, color, _startPosX, _startPosY);
break;
case Four:
drawFourBlocks(g2D, color, _startPosX, _startPosY);
break;
case Six:
drawSixBlocks(g2D, color, _startPosX, _startPosY);
break;
}
}
private void drawTwoBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
//прямоугольник левый на палубе (горизонтальный)
g2D.setColor(Color.LIGHT_GRAY);
g2D.drawRect(_startPosX + 32, _startPosY + 13, 21, 12);
g2D.fillRect(_startPosX + 32, _startPosY + 13, 21, 12);
//прямоугольник правый на палубе (вертикальный)
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 53, _startPosY + 7, 16, 24);
g2D.fillRect(_startPosX + 53, _startPosY + 7, 16, 24);
}
private void drawFourBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
//прямоугольник левый на палубе (горизонтальный)
g2D.setColor(Color.magenta);
g2D.drawOval(_startPosX + 32, _startPosY + 13, 8, 12);
g2D.fillOval(_startPosX + 32, _startPosY + 13, 8, 12);
//прямоугольник правый на палубе (вертикальный)
g2D.setColor(Color.GREEN);
g2D.drawOval(_startPosX + 53, _startPosY + 8, 16, 8);
g2D.fillOval(_startPosX + 53, _startPosY + 8, 16, 8);
g2D.setColor(Color.GREEN);
g2D.drawOval(_startPosX + 53, _startPosY + 23, 16, 8);
g2D.fillOval(_startPosX + 53, _startPosY + 23, 16, 8);
g2D.setColor(Color.DARK_GRAY);
g2D.drawOval(_startPosX + 19, _startPosY + 13, 8, 12);
g2D.fillOval(_startPosX + 19, _startPosY + 13, 8, 12);
}
private void drawSixBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 19, _startPosY + 13, 10, 13);
g2D.fillOval(_startPosX + 19, _startPosY + 13, 10, 13);
g2D.setColor(Color.LIGHT_GRAY);
g2D.drawOval(_startPosX + 31, _startPosY + 15, 12, 9);
g2D.fillOval(_startPosX + 31, _startPosY + 15, 12, 9);
g2D.setColor(Color.PINK);
g2D.drawOval(_startPosX + 46, _startPosY + 13, 10, 13);
g2D.fillOval(_startPosX + 46, _startPosY + 13, 10, 13);
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 64, _startPosY + 4, 7, 8);
g2D.fillOval(_startPosX + 64, _startPosY + 4, 7, 8);
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 64, _startPosY + 15, 7, 8);
g2D.fillOval(_startPosX + 64, _startPosY + 15, 7, 8);
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 64, _startPosY + 28, 7, 8);
g2D.fillOval(_startPosX + 64, _startPosY + 28, 7, 8);
}
}

View File

@ -0,0 +1,103 @@
package DifferentBlocks;
import java.awt.*;
public class DrawingBlocksType2 implements IDrawingBlocks {
private NumBlocks numBlocks;
@Override
public void setNumBlocks(int number) {
switch (number) {
case 1:
numBlocks = NumBlocks.Two;
break;
case 2:
numBlocks = NumBlocks.Four;
break;
case 3:
numBlocks = NumBlocks.Six;
break;
default:
numBlocks = NumBlocks.Four;
}
}
@Override
public NumBlocks getNumBlocks() {
return null;
}
@Override
public void drawBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
switch (numBlocks) {
case Two:
drawTwoBlocks(g2D, color, _startPosX, _startPosY);
break;
case Four:
drawFourBlocks(g2D, color, _startPosX, _startPosY);
break;
case Six:
drawSixBlocks(g2D, color, _startPosX, _startPosY);
break;
}
}
private void drawTwoBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
//прямоугольник левый на палубе (горизонтальный)
g2D.setColor(Color.LIGHT_GRAY);
g2D.drawRect(_startPosX + 32, _startPosY + 13, 21, 12);
g2D.fillRect(_startPosX + 32, _startPosY + 13, 21, 12);
//прямоугольник правый на палубе (вертикальный)
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 53, _startPosY + 7, 16, 24);
g2D.fillRect(_startPosX + 53, _startPosY + 7, 16, 24);
}
private void drawFourBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
//прямоугольник левый на палубе (горизонтальный)
g2D.setColor(Color.magenta);
g2D.drawOval(_startPosX + 32, _startPosY + 13, 8, 12);
g2D.fillOval(_startPosX + 32, _startPosY + 13, 8, 12);
g2D.setColor(Color.GREEN);
g2D.drawOval(_startPosX + 53, _startPosY + 8, 16, 8);
g2D.fillOval(_startPosX + 53, _startPosY + 8, 16, 8);
g2D.setColor(Color.GREEN);
g2D.drawOval(_startPosX + 53, _startPosY + 23, 16, 8);
g2D.fillOval(_startPosX + 53, _startPosY + 23, 16, 8);
g2D.setColor(Color.DARK_GRAY);
g2D.drawOval(_startPosX + 19, _startPosY + 13, 8, 12);
g2D.fillOval(_startPosX + 19, _startPosY + 13, 8, 12);
}
private void drawSixBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 19, _startPosY + 13, 10, 13);
g2D.fillOval(_startPosX + 19, _startPosY + 13, 10, 13);
g2D.setColor(Color.LIGHT_GRAY);
g2D.drawOval(_startPosX + 31, _startPosY + 15, 12, 9);
g2D.fillOval(_startPosX + 31, _startPosY + 15, 12, 9);
g2D.setColor(Color.PINK);
g2D.drawOval(_startPosX + 46, _startPosY + 13, 10, 13);
g2D.fillOval(_startPosX + 46, _startPosY + 13, 10, 13);
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 64, _startPosY + 4, 7, 8);
g2D.fillOval(_startPosX + 64, _startPosY + 4, 7, 8);
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 64, _startPosY + 15, 7, 8);
g2D.fillOval(_startPosX + 64, _startPosY + 15, 7, 8);
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 64, _startPosY + 28, 7, 8);
g2D.fillOval(_startPosX + 64, _startPosY + 28, 7, 8);
}
}

View File

@ -0,0 +1,8 @@
package DifferentBlocks;
import java.awt.*;
public interface IDrawingBlocks {
void setNumBlocks(int number);
NumBlocks getNumBlocks();
void drawBlocks(Graphics2D g2d, Color color, int _startPosX, int _startPosY);
}

View File

@ -0,0 +1,7 @@
package DifferentBlocks;
public enum NumBlocks {
Two,
Four,
Six;
}

View File

@ -0,0 +1,12 @@
public enum DirectionType
{
Up,
Down,
Left,
Right,
}

View File

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

View File

@ -0,0 +1,32 @@
package Drawings;
import CollectionGenericObjects.AbstractCompany;
import javax.swing.*;
import java.awt.*;
public class CanvasFormWarshipCollection<T> extends JComponent
{
public AbstractCompany company = null;
public void SetCollectionToCanvas(AbstractCompany company) {
this.company = company;
}
public CanvasFormWarshipCollection(){}
public void paintComponent(Graphics g) {
super.paintComponents(g);
if (company == null || company._collection == null) {
return;
}
company.DrawBackground(g);
for (int i = 0; i < company._collection.getCount(); i++) {
Graphics2D g2d = (Graphics2D) g;
T obj = (T)company._collection.Get(i);
if (obj instanceof DrawingWarship) {
((DrawingWarship) obj).DrawTransport(g2d);
}
}
super.repaint();
}
}

View File

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

View File

@ -0,0 +1,57 @@
package Drawings;
import Entities.EntityBattleship;
import DifferentBlocks.IDrawingBlocks;
import java.awt.*;
public class DrawingBattleship extends DrawingWarship
{
private IDrawingBlocks drawingBlocks;
public DrawingBattleship(int speed, double weight, Color bodyColor, Color additionalColor, boolean compartment, boolean tower)
{
EntityWarship = new EntityBattleship(speed, weight, bodyColor, additionalColor, compartment, tower);
DrawBlocks();
}
public DrawingBattleship(EntityBattleship entityBattleship, IDrawingBlocks blocks) {
EntityWarship = entityBattleship;
drawingBlocks = blocks;
}
@Override
public void DrawTransport(Graphics2D g)
{
if (EntityWarship == null || !(EntityWarship instanceof EntityBattleship battleship) || _startPosX == null || _startPosY == null) {
return;
}
//ПРОВЕРКА
super.DrawTransport(g); // Обращение к методу DrawTransport базового класса
if (battleship.Compartment)
{
//отсеки для ракет
//заливка отсеков
g.setColor(battleship.getAdditionalColor());
g.fillRect(_startPosX + 15, _startPosY + 5, 27, 5); //верхний отсек для ракет
g.fillRect(_startPosX + 15, _startPosY + 28, 27, 5); //нижний отсек для ракет
//границы отсеков
g.drawRect(_startPosX + 15, _startPosY + 5, 27, 5); //верхний отсек
g.drawRect(_startPosX + 15, _startPosY + 28, 27, 5); //нижний отсек
}
if (battleship.Tower)
{
//границы башни
g.drawRect(_startPosX + 107, _startPosY + 17, 30, 5);
g.fillOval(_startPosX + 98, _startPosY + 15, 10, 10);
//заливка башни
g.setColor(battleship.getAdditionalColor());
g.fillOval(_startPosX + 98, _startPosY + 15, 10, 10);
g.fillRect(_startPosX + 107, _startPosY + 17, 30, 5);
}
}
}

View File

@ -0,0 +1,179 @@
package Drawings;
import DifferentBlocks.DrawingBlocks;
import DifferentBlocks.DrawingBlocksType1;
import DifferentBlocks.DrawingBlocksType2;
import DifferentBlocks.IDrawingBlocks;
import Entities.EntityWarship;
import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Type;
public class DrawingWarship extends JPanel {
public IDrawingBlocks drawingBlocks;
public Entities.EntityWarship EntityWarship;
public Integer _pictureWidth;
public Integer _pictureHeight;
public Integer _startPosX;
public Integer _startPosY;
public int _drawingWarshipWidth = 120;
public int _drawingWarshipHeight = 80;
public Integer GetPosX() {return _startPosX;}
public Integer GetPosY() {return _startPosY;}
public Integer GetWidth() {return _drawingWarshipWidth;}
public Integer GetHeight() {return _drawingWarshipHeight;}
protected DrawingWarship() {
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
public DrawingWarship(int speed, double weight, Color bodyColor)
{
super();
EntityWarship = new EntityWarship(speed, weight, bodyColor);
DrawBlocks();
}
public DrawingWarship(EntityWarship entityWarship, IDrawingBlocks blocks) {
EntityWarship = entityWarship;
drawingBlocks = blocks;
}
//////////////////////
protected void DrawBlocks() {
int number = (int)(Math.random() * 4 + 0);
switch ((int)(Math.random() * 3 + 1)) {
case 1:
drawingBlocks = new DrawingBlocksType1();
break;
case 2:
drawingBlocks = new DrawingBlocksType2();
break;
case 3:
drawingBlocks = new DrawingBlocks();
break;
default:
number = 0;
break;
}
drawingBlocks.setNumBlocks(number);
}
public boolean SetPictureSize(int width, int height)
{
if (width < _drawingWarshipWidth || height < _drawingWarshipHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null || _startPosY != null)
{
if (_startPosX + _drawingWarshipWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawingWarshipWidth;
}
else if (_startPosX < 0) _startPosX = 0;
if (_startPosY + _drawingWarshipHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawingWarshipHeight;
}
else if (_startPosY < 0) _startPosY = 0;
}
return true;
}
public void SetPosition(int x, int y) {
if (!(_pictureWidth != null && _pictureHeight != null)) return;
if (x + _drawingWarshipWidth > _pictureWidth)
{
_startPosX = x - (x + _drawingWarshipWidth - _pictureWidth);
}
else if (x < 0) _startPosX = 0;
else _startPosX = x;
if (y + _drawingWarshipHeight > _pictureHeight)
{
_startPosY = y - (y + _drawingWarshipHeight - _pictureHeight);
}
else if (y < 0) _startPosY = 0;
else _startPosY = y;
}
public boolean MoveTransport(DirectionType direction) {
if (EntityWarship == null || _startPosX == null || _startPosY == null) return false;
switch (direction) {
case Left:
if (_startPosX - EntityWarship.Step > 0) {
_startPosX -= (int)EntityWarship.Step;
}
return true;
case Up:
if (_startPosY - EntityWarship.Step > 0)
{
_startPosY -= (int)EntityWarship.Step;
}
return true;
case Right:
if (_startPosX + _drawingWarshipWidth + (int)EntityWarship.Step < _pictureWidth - EntityWarship.Step)
{
_startPosX += (int)EntityWarship.Step;
}
return true;
case Down:
if (_startPosY + _drawingWarshipHeight + (int)EntityWarship.Step < _pictureHeight - EntityWarship.Step)
{
_startPosY += (int)EntityWarship.Step;
} return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g)
{
if (EntityWarship == null || _startPosX == null || _startPosY == null)
{
return;
}
int y = _startPosY;
Color bodyColor = EntityWarship.getBodyColor();
//границы линкора
g.drawRect(_startPosX + 10, _startPosY, 80, 40); //ширина высота (сама палуба)
//заливка линкора
g.setColor(EntityWarship.getBodyColor());
g.fillRect(_startPosX + 10, _startPosY, 80, 40);
//границы двигателей
g.drawRect(_startPosX + 5, _startPosY + 7, 5, 10); //двигатель верхний
g.drawRect(_startPosX + 5, _startPosY + 22, 5, 10); //двигатель нижний
//заливка двигателей (2 фигни сзади)
g.setColor(Color.BLACK);
g.fillRect(_startPosX + 5, _startPosY + 7, 5, 10); //верхний
g.fillRect(_startPosX + 5, _startPosY + 22, 5, 10); //нижний
// блоки
if (drawingBlocks != null)
drawingBlocks.drawBlocks(g, Color.BLACK, _startPosX, _startPosY);
//носик палубы
int[] xPoints = {_startPosX + 90,_startPosX + 120, _startPosX + 90};
int[] yPoints = {_startPosY,_startPosY + 20,_startPosY + 40};
Polygon Points = new Polygon(xPoints, yPoints, 3);
g.drawPolygon(Points);
g.setColor(EntityWarship.getBodyColor());
g.fillPolygon(Points);
//круг на палубе
g.drawOval(_startPosX + 78, _startPosY + 12, 15, 15);
//заливка круга
g.setColor(Color.BLUE);
g.fillOval(_startPosX + 78, _startPosY + 12, 15, 15);
}
}

View File

@ -0,0 +1,22 @@
package Entities;
import java.awt.*;
public class EntityBattleship extends EntityWarship{
public Color AdditionalColor;
public Color getAdditionalColor(){
return AdditionalColor;
}
public boolean Compartment;
public boolean Tower;
public EntityBattleship(int speed, double weight, Color bodyColor, Color additionalColor, boolean compartment, boolean tower){
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
Compartment = compartment;
Tower = tower;
}
}
//свойства в джаве нет

View File

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

View File

@ -0,0 +1,144 @@
import CollectionAdditionalObjects.AdditionalCollections;
import CollectionGenericObjects.AbstractCompany;
import DifferentBlocks.DrawingBlocksType2;
import DifferentBlocks.DrawingBlocksType1;
import DifferentBlocks.DrawingBlocks;
import DifferentBlocks.IDrawingBlocks;
import Drawings.DrawingWarship;
import Drawings.CanvasBattleship;
import Drawings.DrawingBattleship;
import Entities.EntityWarship;
import Entities.EntityBattleship;
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 DrawingWarship drawingWarship = null;
private AbstractCompany company = null;
private CanvasBattleship canvasBattleship = new CanvasBattleship();
private AdditionalCollections<EntityWarship, IDrawingBlocks> additionalCollection = null;
private Random random = new Random();
private JButton buttonGenerate = new JButton("Создать");
private JButton buttonAdd = new JButton("Добавить");
private JList<String> listEntity = new JList<String>();
private JList<String> listBlocks = new JList<String>();
public FormAdditionalCollection() {
setTitle("Случайный объект");
setMinimumSize(new Dimension(650,310));
additionalCollection = new AdditionalCollections<EntityWarship, IDrawingBlocks>(3, (Class) EntityWarship.class, (Class) IDrawingBlocks.class);
AddEntities();
AddBlocks();
buttonGenerate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
drawingWarship = additionalCollection.CreateAdditionalCollectionWarship();
drawingWarship.SetPictureSize(getWidth(), getHeight());
drawingWarship.SetPosition(50,50);
canvasBattleship._drawingWarship = drawingWarship;
canvasBattleship.repaint();
String[] data1 = new String[additionalCollection.CountEntities];
for (int i = 0; i < additionalCollection.CountEntities; i++) {
EntityWarship entity = additionalCollection._collectionEntity[i];
data1[i] = ToString(entity);
}
String[] data2 = new String[additionalCollection.CountBlocks];
for (int i = 0; i < additionalCollection.CountBlocks; i++) {
IDrawingBlocks blocks = additionalCollection._collectionBlocks[i];
data2[i] = ToString(blocks);
}
listEntity.setListData(data1);
listBlocks.setListData(data2);
}
});
buttonAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (drawingWarship != null) {
DrawingWarship copyWarship;
if (drawingWarship instanceof DrawingBattleship)
copyWarship = new DrawingBattleship((EntityBattleship) drawingWarship.EntityWarship, drawingWarship.drawingBlocks);
else
copyWarship = new DrawingWarship(drawingWarship.EntityWarship, drawingWarship.drawingBlocks);
company._collection.Insert(copyWarship);
FormWarshipCollection.canvasShow();
}
}
});
buttonGenerate.setBounds(450, 10, 100, 50);
buttonAdd.setBounds(450, 70, 100, 50);
add(buttonGenerate);
add(buttonAdd);
listEntity.setBounds(10,200,300,60);
listBlocks.setBounds(320,200,300,60);
add(listEntity);
add(listBlocks);
add(canvasBattleship);
setVisible(true);
}
private String ToString(EntityWarship entity) {
String str = "";
if (entity instanceof EntityBattleship) str += "EntityBattleship ";
else str += "EntityWarship ";
str += entity.getBodyColor().toString();
return str;
}
private String ToString(IDrawingBlocks blocks) {
if (blocks == null || blocks.getNumBlocks() == null)
return "Не имеет блоки";
String str = "Blocks ";
if (blocks instanceof DrawingBlocksType1) str += "Тип 1 ";
else if (blocks instanceof DrawingBlocksType2) str += "Тип 2 ";
else str += "Type Blocks ";
str += blocks.getNumBlocks().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));
EntityWarship entity;
if (random.nextBoolean()) {
entity = new EntityWarship(speed, weight, bodycolor);
}
else {
Color additionalcolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
boolean compartment = random.nextBoolean();
boolean tower = random.nextBoolean();
entity = new EntityBattleship(speed, weight, bodycolor, additionalcolor, compartment, tower);
}
additionalCollection.Insert(entity);
}
}
public void AddBlocks() {
for (int i = 0; i < additionalCollection.CountBlocks; i++) {
random = new Random();
Integer number = random.nextInt(0, 4);
IDrawingBlocks drawingBlocks = null;
switch (random.nextInt(0,4)) {
case 1:
drawingBlocks = new DrawingBlocksType1();
break;
case 2:
drawingBlocks = new DrawingBlocksType2();
break;
case 3:
drawingBlocks = new DrawingBlocks();
break;
default:
number = null;
break;
}
if (drawingBlocks != null) drawingBlocks.setNumBlocks(number);
additionalCollection.Insert(drawingBlocks);
}
}
void setCompany(AbstractCompany company) {
this.company = company;
}
}

136
src/src/FormBattleship.java Normal file
View File

@ -0,0 +1,136 @@
import Drawings.*;
import Drawings.DirectionType;
import MovementStrategy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormBattleship extends JFrame {
private String title;
private Dimension dimension;
private int Width, Height;
private CanvasBattleship canvasBattleship = new CanvasBattleship();
private JButton UpButton = new JButton();
private JButton DownButton = new JButton();
private JButton LeftButton = new JButton();
private JButton RightButton = new JButton();
private AbstractStrategy _strategy;
private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
private JButton ButtonStrategy = new JButton("Шаг");
public FormBattleship(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public void Init(DrawingWarship warship) {
setTitle(title);
setMinimumSize(dimension);
Width = getWidth() - 15;
Height = getHeight() - 35;
ComboBoxStrategy.setEnabled(true);
_strategy = null;
canvasBattleship._drawingWarship = warship;
Icon iconUp = new ImageIcon("src/res/arrowUp.png");
UpButton.setIcon(iconUp);
UpButton.setName("Up");
DownButton.setName("Down");
Icon iconDown = new ImageIcon("src/res/arrowDown.png");
DownButton.setIcon(iconDown);
LeftButton.setName("Left");
Icon iconLeft = new ImageIcon("src/res/arrowLeft.png");
LeftButton.setIcon(iconLeft);
RightButton.setName("Right");
Icon iconRight = new ImageIcon("src/res/arrowRight.png");
RightButton.setIcon(iconRight);
ButtonStrategy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (canvasBattleship._drawingWarship == null) return;
if (ComboBoxStrategy.isEnabled())
{
int index = ComboBoxStrategy.getSelectedIndex();
switch(index)
{
case 0:
_strategy = new MoveToCenter();
break;
case 1:
_strategy = new MoveToBorder();
break;
default:
_strategy = null;
break;
}
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableWarship(canvasBattleship._drawingWarship), 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() {
@Override
public void actionPerformed(ActionEvent event) {
if (canvasBattleship._drawingWarship == null) return;
boolean result = false;
switch ((((JButton)(event.getSource())).getName())) {
case "Up":
result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Up);
break;
case "Down":
result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Down);
break;
case "Left":
result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Left);
break;
case "Right":
result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Right);
break;
}
if (result) {
canvasBattleship.repaint();
}
}
};
UpButton.addActionListener(actionListener);
DownButton.addActionListener(actionListener);
LeftButton.addActionListener(actionListener);
RightButton.addActionListener(actionListener);
setSize(dimension.width,dimension.height);
setLayout(null);
canvasBattleship.setBounds(0,0, getWidth(), getHeight());
UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35);
DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35);
RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35);
LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35);
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 25);
ButtonStrategy.setBounds(getWidth() - 130, 45, 100, 25);
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(ButtonStrategy);
add(ComboBoxStrategy);
add(canvasBattleship);
setVisible(true);
}
}

View File

@ -0,0 +1,202 @@
import CollectionGenericObjects.AbstractCompany;
import CollectionGenericObjects.WarshipSharingService;
import CollectionGenericObjects.MassiveGenericObjects;
import Drawings.CanvasFormWarshipCollection;
import Drawings.DrawingWarship;
import Drawings.DrawingBattleship;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.Random;
import static java.lang.Integer.parseInt;
public class FormWarshipCollection extends JFrame{
private String title;
private Dimension dimension;
public static CanvasFormWarshipCollection<DrawingWarship> _canvasBattleship = new CanvasFormWarshipCollection<DrawingWarship>();
private static AbstractCompany _company = null;
private JButton CreateBatButton = new JButton("Создать линкор");
private JButton CreateWarButton = new JButton("Создать военный корабль");
private JButton RemoveButton = new JButton("Удалить");
private JButton GoToCheckButton = new JButton("Тест");
private JButton RandomButton = new JButton("Случайный объект");
private JButton RefreshButton = new JButton("Обновить");
private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"});
private JFormattedTextField MaskedTextField;
public FormWarshipCollection(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public static void canvasShow() {
_company.SetPosition();
_canvasBattleship.SetCollectionToCanvas(_company);
_canvasBattleship.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();
DrawingWarship drawingWarship;
switch (typeOfClass) {
case "DrawingWarship":
drawingWarship = new DrawingWarship(speed, weight, bodyColor);
break;
case "DrawingBattleship":
Color additionalColor = getColor();
boolean compartment = new Random().nextBoolean();
boolean tower = new Random().nextBoolean();
drawingWarship = new DrawingBattleship(speed, weight, bodyColor, additionalColor, compartment, tower);
break;
default: return;
}
if (_company._collection.Insert(drawingWarship, 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, "Выберите цвет", 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 WarshipSharingService(getWidth()-200, getHeight()-70, new MassiveGenericObjects<DrawingWarship>());
break;
}
}
});
CreateWarButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingWarship");
}
});
CreateBatButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingBattleship");
}
});
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;
}
DrawingWarship warship = null;
int counter = 100;
while (warship == null)
{
warship = _company.GetRandomObject();
counter--;
if (counter <= 0)
{
break;
}
}
if (warship == null)
{
return;
}
FormBattleship form = new FormBattleship("Линкор", new Dimension(900,565));
form.Init(warship);
}
});
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();
}
});
_canvasBattleship.setBounds(0, 0, getWidth()-200, getHeight());
ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
CreateWarButton.setBounds(getWidth()-190, 60, 150, 30);
CreateBatButton.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(_canvasBattleship);
add(ComboBoxCollections);
add(CreateWarButton);
add(CreateBatButton);
add(MaskedTextField);
add(RemoveButton);
add(GoToCheckButton);
add(RandomButton);
add(RefreshButton);
setVisible(true);
}
}

View File

@ -1,5 +1,9 @@
import java.awt.*;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
FormWarshipCollection form = new FormWarshipCollection("Коллекция кораблей", new Dimension(1100,650));
form.Init();
form.setLocationRelativeTo(null);
}
}

View File

@ -0,0 +1,61 @@
package MovementStrategy;
public abstract class AbstractStrategy {
private IMoveableObjects _moveableObjects;
private StrategyStatus _state = StrategyStatus.NotIInit;
protected int FieldWidth;
protected int FieldHeight;
public StrategyStatus GetStatus() { return _state; }
public void SetData(IMoveableObjects moveableObjects, int width, int height)
{
if (moveableObjects == null)
{
_state = StrategyStatus.NotIInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObjects = moveableObjects;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != StrategyStatus.InProgress) return;
if (IsTargetDestination())
{
_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 _moveableObjects.GetObjectPosition();
}
protected Integer GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return null;
}
return _moveableObjects.GetStep();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestination();
private boolean MoveTo(MovementDirection movementDirection)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
boolean stateTryMoveObject = _moveableObjects.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,26 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy{
@Override
protected boolean IsTargetDestination(){
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 IsTargetDestination() {
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 Drawings.CanvasBattleship;
import Drawings.DirectionType;
import Drawings.DrawingWarship;
public class MoveableWarship implements IMoveableObjects {
private CanvasBattleship canvas = new CanvasBattleship();
public MoveableWarship(DrawingWarship drawingWarship)
{
canvas._drawingWarship = drawingWarship;
}
@Override
public ObjectParameters GetObjectPosition() {
if (canvas._drawingWarship == null || canvas._drawingWarship.EntityWarship == null ||
canvas._drawingWarship.GetPosX() == null || canvas._drawingWarship.GetPosY() == null)
{
return null;
}
return new ObjectParameters(canvas._drawingWarship.GetPosX(), canvas._drawingWarship.GetPosY(),
canvas._drawingWarship.GetWidth(), canvas._drawingWarship.GetHeight());
}
@Override
public int GetStep() {
return (int)(canvas._drawingWarship.EntityWarship.Step);
}
@Override
public boolean TryMoveObject(MovementDirection direction) {
if (canvas._drawingWarship == null || canvas._drawingWarship.EntityWarship == null)
{
return false;
}
return canvas._drawingWarship.MoveTransport(GetDirectionType(direction));
}
private static DirectionType GetDirectionType(MovementDirection direction)
{
switch (direction) {
case Left: return DirectionType.Left;
case Right: return DirectionType.Right;
case Up: return DirectionType.Up;
case 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 {
NotIInit,
InProgress,
Finish
}