diff --git a/.idea/misc.xml b/.idea/misc.xml index 018dc79..7464918 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - - + + \ No newline at end of file diff --git a/src/AbstractMap.java b/src/AbstractMap.java new file mode 100644 index 0000000..0677956 --- /dev/null +++ b/src/AbstractMap.java @@ -0,0 +1,150 @@ + +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; + +public abstract class AbstractMap { + private IDrawingObject _drawingObject = null; + protected int[][] _map = null; + protected int _width; + protected int _height; + protected float _size_x; + protected float _size_y; + protected final Random _random = new Random(); + protected final int _freeRoad = 0; + protected final int _barrier = 1; + + public BufferedImage CreateMap(int width, int height, IDrawingObject drawningObject) + { + _width = width; + _height = height; + _drawingObject = drawningObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + + public BufferedImage MoveObject(Direction direction) + { + if(_drawingObject!=null){ + float[] cortege = _drawingObject.GetCurrentPosition(); + if (Check(cortege[0],cortege[1],cortege[2],cortege[3]) != 0) + { + _drawingObject.MoveObject(GetOpositDirection(direction)); + } + if(true){ + _drawingObject.MoveObject(direction); + } + return DrawMapWithObject(); + } + return null; + } + + private Direction GetOpositDirection(Direction dir) + { + switch (dir) + { + case None: + return Direction.None; + case Up: + return Direction.Down; + case Down: + return Direction.Up; + case Left: + return Direction.Right; + case Right: + return Direction.Left; + } + return Direction.None; + } + + private boolean SetObjectOnMap() + { + if (_drawingObject == null || _map == null) + { + return false; + } + int x = _random.nextInt(0, 10); + int y = _random.nextInt(0, 10); + _drawingObject.SetObject(x, y, _width, _height); + float[] cortege = _drawingObject.GetCurrentPosition(); + + while (Check(cortege[0], cortege[1], cortege[2], cortege[3]) != 2) + { + int result; + do + { + result = Check(cortege[0], cortege[1], cortege[2], cortege[3]); + if (result == 0) + { + _drawingObject.SetObject((int)cortege[0], (int)cortege[1], _width, _height); + return true; + } + else + { + cortege[0] += _size_x; + } + } while (result != 2); + cortege[0] = x; + cortege[1] += _size_y; + } + return false; + } + + private int Check(float Left, float Right, float Top, float Bottom) + { + int startX = (int)(Left / _size_x); + int startY = (int)(Right / _size_y); + int endX = (int)(Top / _size_x); + int endY = (int)(Bottom / _size_y); + if (startX < 0 || startY < 0 || endX >= _map[0].length || endY >= _map.length) + { + return 2; + } + for (int i = startX; i <= endX; i++) + { + for (int j = startY; j <= endY; j++) + { + if (_map[i][j] == _barrier) + { + return 1; + } + } + } + return 0; + } + + private BufferedImage DrawMapWithObject() + { + BufferedImage bmp = new BufferedImage(_width,_height,BufferedImage.TYPE_INT_RGB); + if (_drawingObject == null || _map == null) + { + return bmp; + } + Graphics gr = bmp.getGraphics(); + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map.length; ++j) + { + if (_map[i][j] == _freeRoad) + { + DrawRoadPart(gr, i, j); + } + else if (_map[i][j] == _barrier) + { + DrawBarrierPart(gr, i, j); + } + } + } + _drawingObject.DrawingObject(gr); + return bmp; + } + + protected abstract void GenerateMap(); + protected abstract void DrawRoadPart(Graphics g, int i, int j); + protected abstract void DrawBarrierPart(Graphics g, int i, int j); +} + diff --git a/src/BlockCount.java b/src/BlockCount.java new file mode 100644 index 0000000..3f4fbbd --- /dev/null +++ b/src/BlockCount.java @@ -0,0 +1,13 @@ +public enum BlockCount { + TwoBlocks(2), + FourBlocks(4), + SixBlocks(6); + + private final int Value; + BlockCount(int count){ + Value=count; + } + public int GetBlockCount(){ + return Value; + } +} diff --git a/src/BlockDirection.java b/src/BlockDirection.java deleted file mode 100644 index 8d7250c..0000000 --- a/src/BlockDirection.java +++ /dev/null @@ -1,5 +0,0 @@ -public enum BlockDirection { - TwoBlocks, - FourBlocks, - SixBlocks; -} diff --git a/src/BlockForm.java b/src/BlockForm.java new file mode 100644 index 0000000..cbdf331 --- /dev/null +++ b/src/BlockForm.java @@ -0,0 +1,9 @@ +public enum BlockForm { + Rect(0), + Round(1), + RoundRectangle(2); + public final int Value; + BlockForm(int i) { + Value=i; + } +} diff --git a/src/Direction.java b/src/Direction.java index 5b70d63..12937af 100644 --- a/src/Direction.java +++ b/src/Direction.java @@ -1,4 +1,5 @@ public enum Direction { + None, Up, Down, Left, diff --git a/src/DrawingAircraftCarrier.java b/src/DrawingAircraftCarrier.java new file mode 100644 index 0000000..3a501f7 --- /dev/null +++ b/src/DrawingAircraftCarrier.java @@ -0,0 +1,87 @@ +import java.awt.*; + +public class DrawingAircraftCarrier extends DrawingWarship{ + + public DrawingAircraftCarrier(int speed, float weight, Color bodyColor, Color dopColor, boolean bodyKit, boolean cabin, boolean superEngine) + { + super(speed, weight, bodyColor, 114, 40); + Warship = new EntityAircraftCarrier(speed, weight, bodyColor, dopColor, bodyKit, cabin, superEngine); + } + + public DrawingAircraftCarrier(EntityWarship warship, IDrawingObjectBlock additionalObject) { + super(warship, additionalObject); + Warship = warship; + } + + @Override + public void DrawTransport(Graphics gr){ + if(!(Warship instanceof EntityAircraftCarrier)) + { + return; + } + Graphics2D g2 = (Graphics2D) gr; + EntityAircraftCarrier aircraftCarrier = (EntityAircraftCarrier) Warship; + + if (aircraftCarrier.GetBodyKit()) + { + //боковая площадка + int[] pointXArea = {_startPosX + 94, _startPosX + 74, _startPosX + 24, _startPosX + 4}; + int[] pointYArea = {_startPosY + 40, _startPosY + 60, _startPosY + 60, _startPosY + 40}; + g2.setColor(Warship.GetBodyColor()); + g2.fillPolygon(pointXArea, pointYArea, 4); + g2.setColor(Color.BLACK); + g2.drawPolygon(pointXArea, pointYArea, 4); + + //полоса + int[] pointXLine = {_startPosX + 4, _startPosX + 15, _startPosX + 74, _startPosX + 59}; + int[] pointYLine = {_startPosY, _startPosY, _startPosY + 60, _startPosY + 60}; + g2.setColor(Color.GRAY); + g2.fillPolygon(pointXLine, pointYLine, 4); + g2.setColor(Color.BLACK); + g2.drawPolygon(pointXLine, pointYLine, 4); + } + + if (aircraftCarrier.GetSuperEngine()) + { + g2.setColor(Color.RED); + g2.fillOval(_startPosX, _startPosY, 10, 10); + g2.setColor(Color.BLACK); + g2.drawOval(_startPosX, _startPosY, 10, 10); + + g2.setColor(Color.RED); + g2.fillOval(_startPosX, _startPosY + 10, 10, 10); + g2.setColor(Color.BLACK); + g2.drawOval(_startPosX, _startPosY + 10, 10, 10); + + g2.setColor(Color.RED); + g2.fillOval(_startPosX, _startPosY + 18, 10, 10); + g2.setColor(Color.BLACK); + g2.drawOval(_startPosX, _startPosY + 18, 10, 10); + + g2.setColor(Color.RED); + g2.fillOval(_startPosX, _startPosY + 30, 10, 10); + g2.setColor(Color.BLACK); + g2.drawOval(_startPosX, _startPosY + 30, 10, 10); + } + + super.DrawTransport(g2); + + if (aircraftCarrier.GetCabin()) + { + g2.setColor(Color.GRAY); + g2.fillOval(_startPosX + 80, _startPosY + 13, 10, 14); + g2.setColor(Color.BLACK); + g2.drawOval(_startPosX + 80, _startPosY + 13, 10, 14); + + g2.setColor(Color.GRAY); + g2.fillOval(_startPosX + 90, _startPosY + 13, 10, 14); + g2.setColor(Color.BLACK); + g2.drawOval(_startPosX + 90, _startPosY + 13, 10, 14); + + g2.setColor(Color.GRAY); + g2.fillRect(_startPosX + 85, _startPosY + 13, 10, 14); + g2.setColor(Color.BLACK); + g2.drawRect(_startPosX + 85, _startPosY + 13, 10, 14); + } + } +} \ No newline at end of file diff --git a/src/DrawingBlocks.java b/src/DrawingBlocks.java index 12e68d0..df1d9c8 100644 --- a/src/DrawingBlocks.java +++ b/src/DrawingBlocks.java @@ -1,45 +1,43 @@ import java.awt.*; -public class DrawingBlocks { - BlockDirection blockDirection; +public class DrawingBlocks implements IDrawingObjectBlock{ + private BlockCount _block; + public DrawingBlocks(BlockCount block) { + _block=block; + } + @Override + public void SetBlockCount(int count){ + for (BlockCount temp: BlockCount.values()) + if (temp.GetBlockCount() == count){ + _block=temp; + return; + } + } + @Override public void DrawBlocks(Graphics2D g2, int _startPosX, int _startPosY){ - switch(blockDirection){ - case TwoBlocks: - g2.setColor(Color.GRAY); - g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10); - g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10); - g2.setColor(Color.BLACK); - g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10); - g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10); - break; - case FourBlocks: - g2.setColor(Color.GRAY); - g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10); - g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10); - g2.fillRect(_startPosX + 25, _startPosY + 10, 10, 10); - g2.fillRect(_startPosX + 25, _startPosY + 20, 10, 10); - g2.setColor(Color.BLACK); - g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10); - g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10); - g2.drawRect(_startPosX + 25, _startPosY + 10, 10, 10); - g2.drawRect(_startPosX + 25, _startPosY + 20, 10, 10); - break; - case SixBlocks: - g2.setColor(Color.GRAY); - g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10); - g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10); - g2.fillRect(_startPosX + 25, _startPosY + 10, 10, 10); - g2.fillRect(_startPosX + 25, _startPosY + 20, 10, 10); - g2.fillRect(_startPosX + 35, _startPosY + 10, 10, 10); - g2.fillRect(_startPosX + 35, _startPosY + 20, 10, 10); - g2.setColor(Color.BLACK); - g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10); - g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10); - g2.drawRect(_startPosX + 25, _startPosY + 10, 10, 10); - g2.drawRect(_startPosX + 25, _startPosY + 20, 10, 10); - g2.drawRect(_startPosX + 35, _startPosY + 10, 10, 10); - g2.drawRect(_startPosX + 35, _startPosY + 20, 10, 10); - break; + if (_block.GetBlockCount() >= 2) { + g2.setColor(Color.GRAY); + g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10); + g2.setColor(Color.BLACK); + g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10); + } + if (_block.GetBlockCount() >= 4) { + g2.setColor(Color.GRAY); + g2.fillRect(_startPosX + 25, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 25, _startPosY + 20, 10, 10); + g2.setColor(Color.BLACK); + g2.drawRect(_startPosX + 25, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 25, _startPosY + 20, 10, 10); + } + if (_block.GetBlockCount() >= 6) { + g2.setColor(Color.GRAY); + g2.fillRect(_startPosX + 35, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 35, _startPosY + 20, 10, 10); + g2.setColor(Color.BLACK); + g2.drawRect(_startPosX + 35, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 35, _startPosY + 20, 10, 10); } } } diff --git a/src/DrawingComponents.java b/src/DrawingComponents.java deleted file mode 100644 index bd909a7..0000000 --- a/src/DrawingComponents.java +++ /dev/null @@ -1,22 +0,0 @@ -import javax.swing.*; -import java.awt.*; - -public class DrawingComponents extends JComponent { - public DrawingWarship warship; - public DrawingComponents(){ - super(); - } - public void SetDrawingWarship(DrawingWarship warship){ - this.warship = warship; - } - - @Override - public void paintComponent(Graphics g){ - super.paintComponent(g); - Graphics2D g2 = (Graphics2D) g; - if(warship != null){ - warship.DrawTransport(g2); - } - repaint(); - } -} diff --git a/src/DrawingObjectWarship.java b/src/DrawingObjectWarship.java new file mode 100644 index 0000000..3fa8b93 --- /dev/null +++ b/src/DrawingObjectWarship.java @@ -0,0 +1,37 @@ +import java.awt.*; + +public class DrawingObjectWarship implements IDrawingObject { + private DrawingWarship _warship = null; + + public DrawingObjectWarship(DrawingWarship warship) + { + _warship = warship; + } + + public float Step(){ + if(_warship !=null && _warship.Warship != null) + return _warship.Warship.Step; + return 0; + } + + @Override + public void SetObject(int x, int y, int width, int height) { + _warship.SetPosition(x, y, width, height); + } + + @Override + public void MoveObject(Direction direction) { + _warship.MoveTransport(direction); + } + + @Override + public void DrawingObject(Graphics g) { + _warship.DrawTransport(g); + } + + @Override + public float[] GetCurrentPosition() { + if(_warship != null) return _warship.GetCurrentPosition(); + return null; + } +} diff --git a/src/DrawingRoundBlocks.java b/src/DrawingRoundBlocks.java new file mode 100644 index 0000000..f57d40d --- /dev/null +++ b/src/DrawingRoundBlocks.java @@ -0,0 +1,43 @@ +import java.awt.*; + +public class DrawingRoundBlocks implements IDrawingObjectBlock{ + private BlockCount _block; + public DrawingRoundBlocks(BlockCount block){ + _block=block; + } + @Override + public void SetBlockCount(int count){ + for (BlockCount temp: BlockCount.values()) + if (temp.GetBlockCount() == count){ + _block=temp; + return; + } + } + @Override + public void DrawBlocks(Graphics2D g2, int _startPosX, int _startPosY){ + if (_block.GetBlockCount() >= 2) { + g2.setColor(Color.GRAY); + g2.fillOval(_startPosX + 15, _startPosY + 10, 10, 10); + g2.fillOval(_startPosX + 15, _startPosY + 20, 10, 10); + g2.setColor(Color.BLACK); + g2.drawOval(_startPosX + 15, _startPosY + 10, 10, 10); + g2.drawOval(_startPosX + 15, _startPosY + 20, 10, 10); + } + if (_block.GetBlockCount() >= 4) { + g2.setColor(Color.GRAY); + g2.fillOval(_startPosX + 25, _startPosY + 10, 10, 10); + g2.fillOval(_startPosX + 25, _startPosY + 20, 10, 10); + g2.setColor(Color.BLACK); + g2.drawOval(_startPosX + 25, _startPosY + 10, 10, 10); + g2.drawOval(_startPosX + 25, _startPosY + 20, 10, 10); + } + if (_block.GetBlockCount() >= 6) { + g2.setColor(Color.GRAY); + g2.fillOval(_startPosX + 35, _startPosY + 10, 10, 10); + g2.fillOval(_startPosX + 35, _startPosY + 20, 10, 10); + g2.setColor(Color.BLACK); + g2.drawOval(_startPosX + 35, _startPosY + 10, 10, 10); + g2.drawOval(_startPosX + 35, _startPosY + 20, 10, 10); + } + } +} diff --git a/src/DrawingRoundRectangleBlocks.java b/src/DrawingRoundRectangleBlocks.java new file mode 100644 index 0000000..78d196c --- /dev/null +++ b/src/DrawingRoundRectangleBlocks.java @@ -0,0 +1,43 @@ +import java.awt.*; + +public class DrawingRoundRectangleBlocks implements IDrawingObjectBlock { + private BlockCount _block; + public DrawingRoundRectangleBlocks(BlockCount block) { + _block=block; + } + @Override + public void SetBlockCount(int count){ + for (BlockCount temp: BlockCount.values()) + if (temp.GetBlockCount() == count){ + _block=temp; + return; + } + } + @Override + public void DrawBlocks(Graphics2D g2, int _startPosX, int _startPosY){ + if (_block.GetBlockCount() >= 2) { + g2.setColor(Color.GRAY); + g2.fillRoundRect(_startPosX + 15, _startPosY + 10, 10, 10, 7, 7); + g2.fillRoundRect(_startPosX + 15, _startPosY + 20, 10, 10, 7, 7); + g2.setColor(Color.BLACK); + g2.drawRoundRect(_startPosX + 15, _startPosY + 10, 10, 10, 7, 7); + g2.drawRoundRect(_startPosX + 15, _startPosY + 20, 10, 10, 7, 7); + } + if (_block.GetBlockCount() >= 4) { + g2.setColor(Color.GRAY); + g2.fillRoundRect(_startPosX + 25, _startPosY + 10, 10, 10, 7, 7); + g2.fillRoundRect(_startPosX + 25, _startPosY + 20, 10, 10, 7, 7); + g2.setColor(Color.BLACK); + g2.drawRoundRect(_startPosX + 25, _startPosY + 10, 10, 10, 7, 7); + g2.drawRoundRect(_startPosX + 25, _startPosY + 20, 10, 10, 7, 7); + } + if (_block.GetBlockCount() >= 6) { + g2.setColor(Color.GRAY); + g2.fillRoundRect(_startPosX + 35, _startPosY + 10, 10, 10, 7, 7); + g2.fillRoundRect(_startPosX + 35, _startPosY + 20, 10, 10, 7, 7); + g2.setColor(Color.BLACK); + g2.drawRoundRect(_startPosX + 35, _startPosY + 10, 10, 10, 7, 7); + g2.drawRoundRect(_startPosX + 35, _startPosY + 20, 10, 10, 7, 7); + } + } +} diff --git a/src/DrawingWarship.java b/src/DrawingWarship.java index 04c5463..b54df4c 100644 --- a/src/DrawingWarship.java +++ b/src/DrawingWarship.java @@ -1,25 +1,69 @@ import java.awt.*; import java.util.Random; public class DrawingWarship { - private EntityWarship Warship; + protected EntityWarship Warship; public EntityWarship GetWarship(){return Warship;} - public DrawingBlocks Blocks; + protected IDrawingObjectBlock Blocks; + private BlockCount _block; - private int _startPosX; - private int _startPosY; + protected int _startPosX; + protected int _startPosY; private Integer _pictureWidth = null; private Integer _pictureHeight = null; - private final int _warshipWidth = 114; - private final int _warshipHeight = 40; + private int _warshipWidth = 114; + private int _warshipHeight = 40; - public void Init(int speed, float weight, Color bodyColor) + public DrawingWarship(int speed, float weight, Color bodyColor, int blockForm) { - Warship = new EntityWarship(); - Warship.Init(speed, weight, bodyColor); - Blocks = new DrawingBlocks(); - Blocks.blockDirection = BlockRandom(); + Warship = new EntityWarship(speed, weight, bodyColor); + Blocks = GetFormOfBlock(blockForm); + Blocks.SetBlockCount(BlockRandom()); + } + + protected DrawingWarship(int speed, float weight, Color bodyColor, int warshipWidth, int warshipHeight) + { + Warship = new EntityWarship(speed, weight, bodyColor); + Blocks = new DrawingBlocks(_block); + Blocks.SetBlockCount(BlockRandom()); + _warshipWidth = warshipWidth; + _warshipHeight = warshipHeight; + } + + public DrawingWarship(EntityWarship warship,IDrawingObjectBlock block){ + Warship = warship; + Blocks = block; + } + + public int BlockRandom(){ + Random rnd = new Random(); + int count = rnd.nextInt(0,3); + if(count == 0) return 2; + if(count == 1) return 4; + if(count == 2) return 6; + return 0; + } + + public IDrawingObjectBlock GetFormOfBlock(int FormOfBlock){ + BlockForm temp = null; + for (BlockForm form:BlockForm.values()) { + if(form.Value==FormOfBlock){ + temp = form; + break; + } + } + if(temp==null) + return null; + switch (temp){ + case Rect: + return new DrawingBlocks(_block); + case Round: + return new DrawingRoundBlocks(_block); + case RoundRectangle: + return new DrawingRoundRectangleBlocks(_block); + } + return null; } public void SetPosition(int x, int y, int width, int height) @@ -32,6 +76,7 @@ public class DrawingWarship { _pictureHeight = height; } } + public void MoveTransport(Direction direction) { if (_pictureWidth == null || _pictureHeight == null) @@ -70,11 +115,13 @@ public class DrawingWarship { break; } } - public void DrawTransport(Graphics2D g2){ + + public void DrawTransport(Graphics gr){ if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) { return; } + Graphics2D g2 = (Graphics2D) gr; //главная палуба int[] pointXWarship = {_startPosX + 4, _startPosX + 94, _startPosX + 114, _startPosX + 94, _startPosX + 4}; int[] pointYWarship = {_startPosY, _startPosY, _startPosY + 20, _startPosY + 40, _startPosY + 40}; @@ -107,15 +154,6 @@ public class DrawingWarship { Blocks.DrawBlocks(g2,_startPosX, _startPosY); } - public BlockDirection BlockRandom(){ - Random rand = new Random(); - int resRand = rand.nextInt(3); - if(resRand == 0) return BlockDirection.TwoBlocks; - if(resRand == 1) return BlockDirection.FourBlocks; - if(resRand == 2) return BlockDirection.SixBlocks; - return null; - } - public void ChangeBorders(int width, int height) { _pictureWidth = width; @@ -135,4 +173,13 @@ public class DrawingWarship { _startPosY = _pictureHeight - _warshipHeight; } } + + public float[] GetCurrentPosition(){ + float[] cortege = new float[4]; + cortege[0] = _startPosX; + cortege[1] =_startPosY; + cortege[2] = _startPosX + _warshipWidth; + cortege[3] = _startPosY + _warshipHeight; + return cortege; + } } diff --git a/src/EntityAircraftCarrier.java b/src/EntityAircraftCarrier.java new file mode 100644 index 0000000..2a66ae2 --- /dev/null +++ b/src/EntityAircraftCarrier.java @@ -0,0 +1,24 @@ +import java.awt.*; + +public class EntityAircraftCarrier extends EntityWarship { + private Color DopColor; + public Color GetDopColor(){return DopColor;} + + private boolean BodyKit; + public boolean GetBodyKit(){return BodyKit;} + + private boolean Cabin; + public boolean GetCabin(){return Cabin;} + + private boolean SuperEngine; + public boolean GetSuperEngine(){return SuperEngine;} + + public EntityAircraftCarrier(int speed, float weight, Color bodyColor, Color dopColor, boolean bodyKit, boolean cabin, boolean superEngine) + { + super(speed, weight, bodyColor); + DopColor = dopColor; + BodyKit = bodyKit; + Cabin = cabin; + SuperEngine = superEngine; + } +} diff --git a/src/EntityWarship.java b/src/EntityWarship.java index 092a65b..c092fa2 100644 --- a/src/EntityWarship.java +++ b/src/EntityWarship.java @@ -11,11 +11,12 @@ public class EntityWarship { public Color GetBodyColor (){return BodyColor;} public float Step; - public void Init(int speed, float weight, Color bodyColor) + + public EntityWarship(int speed, float weight, Color bodyColor) { Random rnd = new Random(); - Speed = speed <= 0 ? rnd.nextInt(100) + 50 : speed; - Weight = weight <= 0 ? rnd.nextInt(30)+40 : weight; + Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed; + Weight = weight <= 0 ? rnd.nextInt(40, 70) : weight; BodyColor= bodyColor; Step = Speed * 100 / Weight; } diff --git a/src/FormMapWithSetWarships.form b/src/FormMapWithSetWarships.form new file mode 100644 index 0000000..26b6e7e --- /dev/null +++ b/src/FormMapWithSetWarships.form @@ -0,0 +1,158 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/FormMapWithSetWarships.java b/src/FormMapWithSetWarships.java new file mode 100644 index 0000000..3f18958 --- /dev/null +++ b/src/FormMapWithSetWarships.java @@ -0,0 +1,166 @@ +import javax.swing.*; +import java.awt.*; + +public class FormMapWithSetWarships extends JFrame{ + private JPanel mainPanel; + private JPanel PictureBox; + private JPanel GroupBoxTools; + private JComboBox СomboBoxSelectorMap; + private JButton ButtonAddWarship; + private JButton ButtonRemoveWarship; + private JButton ButtonShowStorage; + private JButton ButtonShowOnMap; + private JTextField TextBoxPosition; + private JButton ButtonRight; + private JButton ButtonUp; + private JButton ButtonDown; + private JButton ButtonLeft; + private Image bufferedImage; + private MapWithSetWarshipsGeneric _mapWarshipsCollectionGeneric; + + public FormMapWithSetWarships(){ + InitializeComponent(); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + if (bufferedImage != null) { + PictureBox.paintComponents(bufferedImage.getGraphics()); + PictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null); + } + } + + public void InitializeComponent(){ + setContentPane(mainPanel); + setTitle("Warship"); + setSize(1000, 693); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setVisible(true); + + Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg"); + ButtonUp.setIcon(iconUp); + Icon iconDown = new ImageIcon("src\\Images\\ArrowDown.jpg"); + ButtonDown.setIcon(iconDown); + Icon iconLeft = new ImageIcon("src\\Images\\ArrowLeft.jpg"); + ButtonLeft.setIcon(iconLeft); + Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg"); + ButtonRight.setIcon(iconRight); + + СomboBoxSelectorMap.addActionListener(e -> { + AbstractMap map = switch (СomboBoxSelectorMap.getSelectedItem().toString()) { + case "Простая карта" -> new SimpleMap(); + case "Преграды-линии" -> new LineMap(); + default -> null; + }; + if( map != null){ + _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric( + PictureBox.getWidth(), PictureBox.getHeight(), map); + } + else + { + _mapWarshipsCollectionGeneric = null; + } + }); + + ButtonAddWarship.addActionListener(e -> { + if(_mapWarshipsCollectionGeneric == null){ + return; + } + FormWarshipCreator warshipCreator = new FormWarshipCreator(); + warshipCreator.setSize(1200,700); + warshipCreator.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + warshipCreator.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + warshipCreator.setVisible(true); + if (warshipCreator.getSelectedWarship() != null) { + DrawingObjectWarship warship = new DrawingObjectWarship(warshipCreator.getSelectedWarship()); + + if (_mapWarshipsCollectionGeneric.Plus(warship) >= 0) { + JOptionPane.showMessageDialog(this, + "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + repaint(); + } else { + JOptionPane.showMessageDialog(this, + "Не удалось добавить объект", "Ошибка",JOptionPane.INFORMATION_MESSAGE); + } + } + }); + + ButtonRemoveWarship.addActionListener(e -> { + String text = TextBoxPosition.getText(); + if (text == null || _mapWarshipsCollectionGeneric == null || text.isEmpty()) + { + return; + } + int result = JOptionPane.showConfirmDialog( + null, + "Удалить объект?", + "Удаление", + JOptionPane.YES_NO_CANCEL_OPTION); + if (result == JOptionPane.NO_OPTION) + { + return; + } + int pos = Integer.parseInt(text); + if (_mapWarshipsCollectionGeneric.Minus(pos) != null) + { + JOptionPane.showMessageDialog(this, + "Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + repaint(); + } + else + { + JOptionPane.showMessageDialog(this, + "Не удалось удалить объект","Ошибка",JOptionPane.INFORMATION_MESSAGE); + } + }); + + ButtonShowStorage.addActionListener(e -> { + if(_mapWarshipsCollectionGeneric == null) + { + return; + } + bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + repaint(); + }); + + ButtonShowOnMap.addActionListener(e -> { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + bufferedImage = _mapWarshipsCollectionGeneric.ShowOnMap(); + repaint(); + }); + + ButtonUp.addActionListener(e -> { + if (_mapWarshipsCollectionGeneric != null) { + bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Up); + repaint(); + } + }); + + ButtonDown.addActionListener(e -> { + if (_mapWarshipsCollectionGeneric != null) { + bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Down); + repaint(); + } + }); + + ButtonRight.addActionListener(e -> { + if (_mapWarshipsCollectionGeneric != null) { + bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Right); + repaint(); + } + }); + + ButtonLeft.addActionListener(e -> { + if (_mapWarshipsCollectionGeneric != null) { + bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Left); + repaint(); + } + }); + } +} diff --git a/src/FormWarship.form b/src/FormWarship.form index 12eb718..257dce6 100644 --- a/src/FormWarship.form +++ b/src/FormWarship.form @@ -1,41 +1,109 @@
- + - + - + + - + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -62,51 +130,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/src/FormWarship.java b/src/FormWarship.java index 404303a..cc451ec 100644 --- a/src/FormWarship.java +++ b/src/FormWarship.java @@ -1,15 +1,18 @@ import javax.swing.*; 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.util.Random; public class FormWarship extends JFrame { private DrawingWarship _warship; - public DrawingComponents drawingComponents; + public DrawingWarship selectedWarship; + public DrawingWarship getSelectedCar(){ + return selectedWarship; + } + public boolean DialogResult = false; private JPanel mainPanel; + private JPanel drawPanel; private JButton buttonCreate; private JButton buttonLeft; private JButton buttonUp; @@ -19,19 +22,38 @@ public class FormWarship extends JFrame { private JLabel toolBarLabelSpeed; private JLabel toolBarLabelWieght; private JLabel toolBarLabelColor; - private JPanel drawPanel; + private JButton buttonCreateModif; + private JButton buttonSelect; public FormWarship(){ InitializeComponent(); } + private void Draw(){ - drawingComponents.repaint(); + Graphics2D graphics = (Graphics2D) drawPanel.getGraphics(); + graphics.clearRect(0, 0, drawPanel.getWidth(), drawPanel.getHeight()); + drawPanel.paintComponents(graphics); + _warship.DrawTransport(graphics); } - private void InitializeComponent(){ + private void SetData(){ + Random rnd = new Random(); + _warship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), mainPanel.getWidth(), mainPanel.getHeight()); + toolBarLabelSpeed.setText("Color: " + _warship.GetWarship().GetSpeed() + " "); + toolBarLabelWieght.setText("Weight: " + _warship.GetWarship().GetWeight() + " "); + toolBarLabelColor.setText("Color: " + _warship.GetWarship().GetBodyColor().getRed() + " " + + _warship.GetWarship().GetBodyColor().getGreen() + " " + _warship.GetWarship().GetBodyColor().getBlue()); + } + + private void resizeWindow() { + _warship.ChangeBorders(drawPanel.getWidth(), drawPanel.getHeight()); + Draw(); + } + + private void InitializeComponent() { setContentPane(mainPanel); setTitle("Warship"); - setSize(900,700); + setSize(900, 700); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); @@ -44,62 +66,58 @@ public class FormWarship extends JFrame { Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg"); buttonRight.setIcon(iconRight); - drawingComponents = new DrawingComponents(); - drawPanel.add(drawingComponents); - buttonCreate.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - Random rnd = new Random(); - _warship = new DrawingWarship(); - _warship.Init(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, - new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256))); - _warship.SetPosition(rnd.nextInt(90) + 10, rnd.nextInt(90) + 10, drawPanel.getWidth(), drawPanel.getHeight()); - toolBarLabelSpeed.setText("Color: " + _warship.GetWarship().GetSpeed() + " "); - toolBarLabelWieght.setText("Weight: " + _warship.GetWarship().GetWeight() + " "); - toolBarLabelColor.setText("Color: " + _warship.GetWarship().GetBodyColor().getRed() + " " + - _warship.GetWarship().GetBodyColor().getGreen() + " " + _warship.GetWarship().GetBodyColor().getBlue()); - drawingComponents.SetDrawingWarship(_warship); - Draw(); + //кнопка добавления объекта + buttonCreate.addActionListener(e -> { + Random rnd = new Random(); + _warship = new DrawingWarship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), rnd.nextInt(3)); + SetData(); + Draw(); + }); + + //добавление модифицированного объекта + buttonCreateModif.addActionListener(e -> { + Random rnd = new Random(); + _warship = new DrawingAircraftCarrier(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextBoolean(), rnd.nextBoolean(), rnd.nextBoolean()); + SetData(); + Draw(); + }); + + buttonSelect.addActionListener(e -> { + if(_warship != null){ + selectedWarship =_warship; + DialogResult = true; } }); - buttonLeft.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(_warship != null) _warship.MoveTransport(Direction.Left); - Draw(); - } + buttonLeft.addActionListener(e -> { + if (_warship != null) _warship.MoveTransport(Direction.Left); + Draw(); }); - buttonRight.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(_warship != null) _warship.MoveTransport(Direction.Right); - Draw(); - } + buttonRight.addActionListener(e -> { + if (_warship != null) _warship.MoveTransport(Direction.Right); + Draw(); }); - buttonUp.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(_warship != null) _warship.MoveTransport(Direction.Up); - Draw(); - } + buttonUp.addActionListener(e -> { + if (_warship != null) _warship.MoveTransport(Direction.Up); + Draw(); }); - buttonDown.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(_warship != null) _warship.MoveTransport(Direction.Down); - Draw(); - } + buttonDown.addActionListener(e -> { + if (_warship != null) _warship.MoveTransport(Direction.Down); + Draw(); }); - drawPanel.addComponentListener(new ComponentAdapter() { + mainPanel.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { - _warship.ChangeBorders(drawPanel.getWidth(),drawPanel.getHeight()); - Draw(); + super.componentResized(e); + resizeWindow(); } }); } diff --git a/src/FormWarshipCreator.form b/src/FormWarshipCreator.form new file mode 100644 index 0000000..bcd4f74 --- /dev/null +++ b/src/FormWarshipCreator.form @@ -0,0 +1,205 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormWarshipCreator.java b/src/FormWarshipCreator.java new file mode 100644 index 0000000..38653c6 --- /dev/null +++ b/src/FormWarshipCreator.java @@ -0,0 +1,140 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class FormWarshipCreator extends JDialog{ + private JPanel PictureBox; + private JPanel SettingsPanel; + private JPanel BlocksTypePanel; + private JPanel CountOfBlocksPanel; + private JPanel ModificationPanel; + private JPanel TypePanel; + private JRadioButton RoundRectangleFormRadioButton; + private JRadioButton RoundFormRadioButton; + private JRadioButton RectangleFormRadioButton; + private JRadioButton SixRadioButton; + private JRadioButton FourRadioButton; + private JRadioButton TwoRadioButton; + private JButton CreateButton; + private JButton SelectButton; + private JCheckBox EngineCheckBox; + private JCheckBox CabinCheckBox; + private JCheckBox AreaCheckBox; + private JComboBox comboBoxType; + private BlockCount _block = null; + private IDrawingObjectBlock objectBlock = null; + private final WarshipCreatorGeneric warshipCreator =new WarshipCreatorGeneric<>(40,40); + private DrawingWarship _warship; + private DrawingWarship selectedWarship; + private boolean isModified = false; + + public FormWarshipCreator(){ + InitializeComponent(); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + Graphics2D g2d = (Graphics2D) PictureBox.getGraphics(); + if (_warship != null) { + _warship.DrawTransport(g2d); + } + } + + public DrawingWarship getSelectedWarship() { + return selectedWarship; + } + + private void InitializeComponent(){ + setContentPane(PictureBox); + setTitle("Warship"); + setResizable(false); + setSize(1000, 600); + + //Определение типа объекта + comboBoxType.addItemListener(e -> isModified = e.getItem().toString().equals("AircraftCarrier")); + + //количество блоков + TwoRadioButton.addActionListener(e -> { + _block = BlockCount.TwoBlocks; + }); + FourRadioButton.addActionListener(e -> { + _block = BlockCount.FourBlocks; + }); + SixRadioButton.addActionListener(e -> { + _block = BlockCount.SixBlocks; + }); + + //Квадратная форма блоков + RectangleFormRadioButton.addActionListener(e -> { + if(_block == null) return; + + objectBlock = new DrawingBlocks(_block); + objectBlock.SetBlockCount(_block.GetBlockCount()); + warshipCreator.AddBlock(objectBlock); + }); + + //Круглая форма блоков + RoundFormRadioButton.addActionListener(e -> { + if(_block ==null){ + return; + } + objectBlock = new DrawingRoundBlocks(_block); + objectBlock.SetBlockCount(_block.GetBlockCount()); + warshipCreator.AddBlock(objectBlock); + }); + + //Закруглено-квадратная форма блоков + RoundRectangleFormRadioButton.addActionListener(e -> { + if(_block == null){ + return; + } + objectBlock = new DrawingRoundBlocks(_block); + objectBlock.SetBlockCount(_block.GetBlockCount()); + warshipCreator.AddBlock(objectBlock); + }); + + CreateButton.addActionListener(e -> { + Random rnd = new Random(); + if (_block != null && objectBlock != null) { + Color color = JColorChooser.showDialog(this, "Choose color", Color.WHITE); + + if (isModified) { + Color additionalColor = JColorChooser.showDialog(this, "Choose color", Color.GRAY); + + warshipCreator.AddWarship( + new EntityAircraftCarrier( + rnd.nextInt(10, 300), + rnd.nextFloat(10, 3000), + color, + additionalColor, + AreaCheckBox.isSelected(), + CabinCheckBox.isSelected(), + EngineCheckBox.isSelected() + ) + ); + } + else { + warshipCreator.AddWarship( + new EntityWarship( + rnd.nextInt(10, 300), + rnd.nextFloat(10, 3000), + color) + ); + } + + _warship = warshipCreator.NewWarshipCreating(); + _warship.SetPosition( + rnd.nextInt(20), + rnd.nextInt(20), + PictureBox.getWidth(), + PictureBox.getHeight()); + repaint(); + } + }); + SelectButton.addActionListener(e -> { + selectedWarship = _warship; + dispose(); + }); + } +} diff --git a/src/IDrawingObject.java b/src/IDrawingObject.java new file mode 100644 index 0000000..e32a8d8 --- /dev/null +++ b/src/IDrawingObject.java @@ -0,0 +1,15 @@ +import java.awt.*; + +public interface IDrawingObject { + // Шаг перемещения объекта + public float Step = 0; + // Установка позиции объекта + void SetObject(int x, int y, int width, int height); + // Изменение направления пермещения объекта + void MoveObject(Direction direction); + // Отрисовка объекта + void DrawingObject(Graphics g); + // Получение текущей позиции объекта + // /Left, Right, Top, Bottom) + float[] GetCurrentPosition(); +} diff --git a/src/IDrawingObjectBlock.java b/src/IDrawingObjectBlock.java new file mode 100644 index 0000000..a9a8c4c --- /dev/null +++ b/src/IDrawingObjectBlock.java @@ -0,0 +1,6 @@ +import java.awt.*; + +public interface IDrawingObjectBlock { + void SetBlockCount(int count); + void DrawBlocks(Graphics2D g, int _startPosX, int _startPosY); +} diff --git a/src/LineMap.java b/src/LineMap.java new file mode 100644 index 0000000..8bb3801 --- /dev/null +++ b/src/LineMap.java @@ -0,0 +1,53 @@ +import java.awt.*; + +public class LineMap extends AbstractMap{ + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) + { + Graphics2D g2 = (Graphics2D) g; + g2.setColor(Color.gray); + g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } + @Override + protected void DrawRoadPart(Graphics g, int i, int j) + { + Graphics2D g2 = (Graphics2D) g; + g2.setColor(Color.BLUE); + g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } + @Override + protected void GenerateMap() + { + _map = new int[100][100]; + _size_x = (float)_width / _map.length; + _size_y = (float)_height / _map[0].length; + int counter = 0; + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[i].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + boolean flag = true; + while (counter < 20) + { + int lineX = _random.nextInt(11, 89); + int lineY = _random.nextInt(11, 89); + + if (flag) + { + for (int i = lineY; i <= lineY + 10; i++) + _map[lineX][i] = _barrier; + flag = false; + } + else + { + for (int i = lineX; i <= lineX + 10; i++) + _map[i][lineY] = _barrier; + flag = true; + } + counter++; + } + } +} diff --git a/src/Main.java b/src/Main.java index 2603a8a..17772ee 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,5 @@ public class Main { public static void main(String[] args) { - new FormWarship(); + new FormMapWithSetWarships(); } - } diff --git a/src/MapWithSetWarshipsGeneric.java b/src/MapWithSetWarshipsGeneric.java new file mode 100644 index 0000000..471859b --- /dev/null +++ b/src/MapWithSetWarshipsGeneric.java @@ -0,0 +1,123 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetWarshipsGeneric { + private final int _pictureWidth; + private final int _pictureHeight; + private final int _placeSizeWidth = 120; + private final int _placeSizeHeight = 50; + private final SetWarshipsGeneric _setWarships; + private final U _map; + + public MapWithSetWarshipsGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight/_placeSizeHeight; + _setWarships = new SetWarshipsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public int Plus(T warship) + { + return _setWarships.Insert(warship); + } + + public T Minus(int position) + { + return _setWarships.Remove(position); + } + + public Image ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureWidth,BufferedImage.TYPE_INT_ARGB); + Graphics gr = bmp.getGraphics(); + DrawBackground(gr); + DrawWarship(gr); + return bmp; + } + + public Image ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setWarships.getCount(); i++) + { + T warship = _setWarships.Get(i); + if (warship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, warship); + } + } + return new BufferedImage(_pictureWidth, _pictureHeight, 1); + } + + public Image MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new BufferedImage(_pictureWidth, _pictureHeight, 1); + } + + public void Shaking() + { + int j = _setWarships.getCount() - 1; + for (int i = 0; i < _setWarships.getCount(); i++) + { + if (_setWarships.Get(i) == null) + { + for (; j > i; j--) + { + var warship = _setWarships.Get(j); + if (warship != null) + { + _setWarships.Insert(warship, i); + _setWarships.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics gr) + { + Graphics2D g = (Graphics2D)gr; + + Color brush=Color.BLACK; + Stroke penWide = new BasicStroke(3); + + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j <= _pictureHeight / _placeSizeHeight ; ++j) + { + g.setColor(brush); + g.setStroke(penWide); + g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight+2, i * _placeSizeWidth + (int)(_placeSizeWidth*0.8), j * _placeSizeHeight+2); + g.drawLine(i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight/2); + g.drawLine(i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth+ (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight); + } + } + } + + private void DrawWarship(Graphics gr) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + for (int i = 0; i < _setWarships.getCount(); i++) + { + if (_setWarships.Get(i) != null) + { + _setWarships.Get(i).SetObject(i % width * _placeSizeWidth, ((height - 1 - i / width) * _placeSizeHeight) + 6, _pictureWidth, _pictureHeight); + _setWarships.Get(i).DrawingObject(gr); + } + } + } +} diff --git a/src/SetWarshipsGeneric.java b/src/SetWarshipsGeneric.java new file mode 100644 index 0000000..bea9594 --- /dev/null +++ b/src/SetWarshipsGeneric.java @@ -0,0 +1,57 @@ +public class SetWarshipsGeneric{ + private final Object[] _places; + + public int getCount() { + return _places.length; + } + + public SetWarshipsGeneric(int count) + { + _places = new Object[count]; + } + + public int Insert(T warship) + { + return Insert(warship,0); + } + + public int Insert(T warship, int position) + { + if (position < 0 || position >= getCount()) + return -1; + + int empty = -1; + for (int i = position + 1; i < getCount(); i++) + { + if (_places[i] == null) + empty = i; + } + if (empty == -1) + return 0; + else + { + for (int i = empty; i > position; i--) + _places[i] = _places[i - 1]; + } + _places[position] = warship; + return 1; + } + + public T Remove(int position) + { + if (position >= getCount() || position < 0 || _places[position] == null) + return null; + + T deleted =(T) _places[position]; + _places[position] = null; + return deleted; + } + + public T Get(int position) + { + if (position >= getCount() || position < 0) + return null; + + return (T) _places[position]; + } +} diff --git a/src/SimpleMap.java b/src/SimpleMap.java new file mode 100644 index 0000000..32f9f76 --- /dev/null +++ b/src/SimpleMap.java @@ -0,0 +1,43 @@ +import java.awt.*; + +public class SimpleMap extends AbstractMap{ + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) + { + Graphics2D g2 = (Graphics2D) g; + g2.setColor(Color.gray); + g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } + @Override + protected void DrawRoadPart(Graphics g, int i, int j) + { + Graphics2D g2 = (Graphics2D) g; + g2.setColor(Color.BLUE); + g2.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } + @Override + protected void GenerateMap() + { + _map = new int[100][100]; + _size_x = (float)_width / _map.length; + _size_y = (float)_height / _map[0].length; + int counter = 0; + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[i].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + while (counter < 50) + { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + counter++; + } + } + } +} diff --git a/src/WarshipCreatorGeneric.java b/src/WarshipCreatorGeneric.java new file mode 100644 index 0000000..2c67a1b --- /dev/null +++ b/src/WarshipCreatorGeneric.java @@ -0,0 +1,43 @@ +import java.util.ArrayList; +import java.util.Random; + +public class WarshipCreatorGeneric { + private final ArrayList Warships; + private final ArrayList Blocks; + private int WarshipsCount = 0; + private int BlocksCount = 0; + public WarshipCreatorGeneric(int warshipsCount, int blocksCount){ + Warships = new ArrayList<>(warshipsCount); + Blocks = new ArrayList<>(blocksCount); + } + + public int AddWarship(T warship){ + if(WarshipsCount <= Warships.size()){ + Warships.add(warship); + WarshipsCount++; + return WarshipsCount - 1; + } + return -1; + } + + public int AddBlock(U block){ + if(BlocksCount <= Blocks.size()){ + Blocks.add(block); + BlocksCount++; + return BlocksCount - 1; + } + return -1; + } + + public DrawingWarship NewWarshipCreating() + { + Random rand=new Random(); + T warship = (T)Warships.get(rand.nextInt(WarshipsCount)); + U block = (U)Blocks.get(rand.nextInt(BlocksCount)); + + if(warship instanceof EntityAircraftCarrier){ + return new DrawingAircraftCarrier(warship, block); + } + return new DrawingWarship(warship,block); + } +}