diff --git a/src/src/DrawingBlocks.java b/src/src/DifferentBlocks/DrawingBlocks.java similarity index 90% rename from src/src/DrawingBlocks.java rename to src/src/DifferentBlocks/DrawingBlocks.java index 27d1e5c..36e1d84 100644 --- a/src/src/DrawingBlocks.java +++ b/src/src/DifferentBlocks/DrawingBlocks.java @@ -1,25 +1,32 @@ +package DifferentBlocks; + import java.awt.*; -public class DrawingBlocks { - private NumberBlock numBlocks; +public class DrawingBlocks implements IDrawingBlocks { + private NumBlocks numBlocks; //определяет количество блоков public void setNumBlocks(int number) { switch (number) { case 1: - numBlocks = NumberBlock.Two; + numBlocks = NumBlocks.Two; break; case 2: - numBlocks = NumberBlock.Four; + numBlocks = NumBlocks.Four; break; case 3: - numBlocks = NumberBlock.Six; + numBlocks = NumBlocks.Six; break; default: - numBlocks = NumberBlock.Four; + numBlocks = NumBlocks.Four; } } + @Override + public NumBlocks getNumBlocks() { + return numBlocks; + } + public void drawBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { switch (numBlocks) { case Two: diff --git a/src/src/DifferentBlocks/DrawingBlocksType1.java b/src/src/DifferentBlocks/DrawingBlocksType1.java new file mode 100644 index 0000000..86bc3f6 --- /dev/null +++ b/src/src/DifferentBlocks/DrawingBlocksType1.java @@ -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); + } +} \ No newline at end of file diff --git a/src/src/DifferentBlocks/DrawingBlocksType2.java b/src/src/DifferentBlocks/DrawingBlocksType2.java new file mode 100644 index 0000000..9532c5c --- /dev/null +++ b/src/src/DifferentBlocks/DrawingBlocksType2.java @@ -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); + } +} \ No newline at end of file diff --git a/src/src/DifferentBlocks/IDrawingBlocks.java b/src/src/DifferentBlocks/IDrawingBlocks.java new file mode 100644 index 0000000..7a07868 --- /dev/null +++ b/src/src/DifferentBlocks/IDrawingBlocks.java @@ -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); +} \ No newline at end of file diff --git a/src/src/DifferentBlocks/NumBlocks.java b/src/src/DifferentBlocks/NumBlocks.java new file mode 100644 index 0000000..9c7d6c0 --- /dev/null +++ b/src/src/DifferentBlocks/NumBlocks.java @@ -0,0 +1,7 @@ +package DifferentBlocks; + +public enum NumBlocks { + Two, + Four, + Six; +} \ No newline at end of file diff --git a/src/src/DrawingBattleship.java b/src/src/DrawingBattleship.java deleted file mode 100644 index c4eb403..0000000 --- a/src/src/DrawingBattleship.java +++ /dev/null @@ -1,177 +0,0 @@ -import java.awt.*; - -public class DrawingBattleship -{ - public EntityBattleship EntityBattleship; - - private DrawingBlocks drawingBlocks = null; - - private Integer _pictureWidth; - - private Integer _pictureHeight; - - private Integer _startPosX; - - private Integer _startPosY; - - private int _drawingBattleshipWidth = 120; - - private int _drawingBattleshipHeight = 80; - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyDeck, boolean compartment, boolean tower, int numBlocks) - { - EntityBattleship = new EntityBattleship(); - EntityBattleship.Init(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower); - _pictureWidth = null; - _pictureHeight = null; - _startPosX = null; - _startPosY = null; - drawingBlocks = new DrawingBlocks(); - drawingBlocks.setNumBlocks(numBlocks); - } - - public boolean SetPictureSize(int width, int height) - { - if (width < _drawingBattleshipWidth || height < _drawingBattleshipHeight) - { - return false; - } - _pictureWidth = width; - _pictureHeight = height; - if (_startPosX != null || _startPosY != null) - { - - if (_startPosX + _drawingBattleshipWidth > _pictureWidth) - { - _startPosX = _pictureWidth - _drawingBattleshipWidth; - } - else if (_startPosX < 0) _startPosX = 0; - if (_startPosY + _drawingBattleshipHeight > _pictureHeight) - { - _startPosY = _pictureHeight - _drawingBattleshipHeight; - } - else if (_startPosY < 0) _startPosY = 0; - } - return true; - } - - public void SetPosition(int x, int y) { - if (!(_pictureWidth != null && _pictureHeight != null)) return; - if (x + _drawingBattleshipWidth > _pictureWidth) - { - _startPosX = x - (x + _drawingBattleshipWidth - _pictureWidth); - } - else if (x < 0) _startPosX = 0; - else _startPosX = x; - if (y + _drawingBattleshipHeight > _pictureHeight) - { - _startPosY = y - (y + _drawingBattleshipHeight - _pictureHeight); - } - else if (y < 0) _startPosY = 0; - else _startPosY = y; - } - - public boolean MoveTransport(DirectionType direction) { - if (EntityBattleship == null || _startPosX == null || _startPosY == null) return false; - switch (direction) { - case Left: - if (_startPosX - EntityBattleship.Step > 0) { - _startPosX -= (int)EntityBattleship.Step; - } - return true; - case Up: - if (_startPosY - EntityBattleship.Step > 0) - { - _startPosY -= (int)EntityBattleship.Step; - } - return true; - case Right: - if (_startPosX + _drawingBattleshipWidth + (int)EntityBattleship.Step < _pictureWidth - EntityBattleship.Step) - { - _startPosX += (int)EntityBattleship.Step; - } - return true; - case Down: - if (_startPosY + _drawingBattleshipHeight + (int)EntityBattleship.Step < _pictureHeight - EntityBattleship.Step) - { - _startPosY += (int)EntityBattleship.Step; - } - return true; - default: - return false; - } - } - - public void DrawTransport(Graphics2D g) - { - if (EntityBattleship == null || _startPosX == null || _startPosY == null) - { - return; - } - int y = _startPosY; - - Color bodyColor = EntityBattleship.getBodyColor(); - Color additionalColor = EntityBattleship.getAdditionalColor(); - - //границы линкора - g.drawRect(_startPosX + 10, _startPosY, 80, 40); //ширина высота (сама палуба) - //заливка линкора - g.setColor(EntityBattleship.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); //нижний - - // блоки - 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(EntityBattleship.BodyColor); - g.fillPolygon(Points); - - - //круг на палубе - g.drawOval(_startPosX + 78, _startPosY + 12, 15, 15); - //заливка круга - g.setColor(Color.BLUE); - g.fillOval(_startPosX + 78, _startPosY + 12, 15, 15); - - //ПРОВЕРКА - - if (EntityBattleship.Compartment) - { - //отсеки для ракет - //заливка отсеков - g.setColor(EntityBattleship.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 (EntityBattleship.Tower) - { - //границы башни - g.drawRect(_startPosX + 107, _startPosY + 17, 30, 5); - g.fillOval(_startPosX + 98, _startPosY + 15, 10, 10); - - //заливка башни - g.setColor(EntityBattleship.getAdditionalColor()); - g.fillOval(_startPosX + 98, _startPosY + 15, 10, 10); - g.fillRect(_startPosX + 107, _startPosY + 17, 30, 5); - - } - } -} diff --git a/src/src/CanvasBattleship.java b/src/src/Drawings/CanvasBattleship.java similarity index 63% rename from src/src/CanvasBattleship.java rename to src/src/Drawings/CanvasBattleship.java index 509257e..a6cf656 100644 --- a/src/src/CanvasBattleship.java +++ b/src/src/Drawings/CanvasBattleship.java @@ -1,16 +1,20 @@ +package Drawings; + +import Drawings.DrawingWarship; + import javax.swing.*; import java.awt.*; public class CanvasBattleship extends JComponent { - public DrawingBattleship _drawingBattleship; + public DrawingWarship _drawingWarship; public CanvasBattleship(){} public void paintComponent(Graphics g) { - if (_drawingBattleship == null) { + if (_drawingWarship == null) { return; } super.paintComponents(g); Graphics2D g2d = (Graphics2D) g; - _drawingBattleship.DrawTransport(g2d); + _drawingWarship.DrawTransport(g2d); super.repaint(); } } diff --git a/src/src/Drawings/DirectionType.java b/src/src/Drawings/DirectionType.java new file mode 100644 index 0000000..a25f453 --- /dev/null +++ b/src/src/Drawings/DirectionType.java @@ -0,0 +1,15 @@ +package Drawings; + +public enum DirectionType +{ + Unknow, + + Up, + + Down, + + Left, + + Right, + +} diff --git a/src/src/Drawings/DrawingBattleship.java b/src/src/Drawings/DrawingBattleship.java new file mode 100644 index 0000000..38bcf58 --- /dev/null +++ b/src/src/Drawings/DrawingBattleship.java @@ -0,0 +1,51 @@ +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 bodyDeck, boolean compartment, boolean tower) + { + EntityWarship = new EntityBattleship(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower); + DrawBlocks(); + } + + @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); + } + } +} diff --git a/src/src/Drawings/DrawingWarship.java b/src/src/Drawings/DrawingWarship.java new file mode 100644 index 0000000..4f35366 --- /dev/null +++ b/src/src/Drawings/DrawingWarship.java @@ -0,0 +1,172 @@ +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 { + private 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(); + } + ////////////////////// + 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); //нижний + + // блоки + 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); + + } +} diff --git a/src/src/Entities/EntityBattleship.java b/src/src/Entities/EntityBattleship.java new file mode 100644 index 0000000..0e966cf --- /dev/null +++ b/src/src/Entities/EntityBattleship.java @@ -0,0 +1,23 @@ +package Entities; + +import java.awt.*; + +public class EntityBattleship extends EntityWarship{ + public Color AdditionalColor; + public Color getAdditionalColor(){ + return AdditionalColor; + } + public boolean BodyDeck; + public boolean Compartment; + public boolean Tower; + + public EntityBattleship(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyDeck, boolean compartment, boolean tower){ + super(speed, weight, bodyColor); + AdditionalColor = additionalColor; + BodyDeck = bodyDeck; + Compartment = compartment; + Tower = tower; + } +} + +//свойства в джаве нет \ No newline at end of file diff --git a/src/src/Entities/EntityWarship.java b/src/src/Entities/EntityWarship.java new file mode 100644 index 0000000..96499df --- /dev/null +++ b/src/src/Entities/EntityWarship.java @@ -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; + } +} diff --git a/src/src/EntityBattleship.java b/src/src/EntityBattleship.java deleted file mode 100644 index 4b5b2b0..0000000 --- a/src/src/EntityBattleship.java +++ /dev/null @@ -1,28 +0,0 @@ -import java.awt.*; -import java.util.Random; - -public class EntityBattleship -{ - public int Speed; - public double Weight; - public Color BodyColor; - public Color AdditionalColor; - public boolean BodyDeck; - public boolean Compartment; - public boolean Tower; - public double Step; - public Color getBodyColor() { return BodyColor; } - public Color getAdditionalColor() { return AdditionalColor; } - public double getStep() { return Speed * 100 / Weight; } - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyDeck, boolean compartment, boolean tower) { - Random rnd = new Random(); - Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed; - Weight = weight <= 0 ? rnd.nextInt(40,70) : weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - BodyDeck = bodyDeck; - Compartment = compartment; - Tower = tower; - Step = Speed * 100 / Weight; - } -} diff --git a/src/src/FormBattleship.java b/src/src/FormBattleship.java index f6b0df2..b6ae3de 100644 --- a/src/src/FormBattleship.java +++ b/src/src/FormBattleship.java @@ -1,3 +1,9 @@ +import Drawings.CanvasBattleship; +import Drawings.DirectionType; +import Drawings.DrawingWarship; +import Drawings.DrawingBattleship; +import MovementStrategy.*; + import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; @@ -10,75 +16,142 @@ public class FormBattleship extends JFrame { private String title; private Dimension dimension; private int Width, Height; - private CanvasBattleship canvasBattleship = new CanvasBattleship(); - private JButton CreateButton = new JButton("Создать");; + public CanvasBattleship canvasBattleship = new CanvasBattleship(); + private JButton CreateButton = new JButton("Создание линкора");; + private JButton CreateShipButton = new JButton("Создание корабля"); 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; } + private void CreateObject(String typeOfClass) { + int StartPositionX = (int)(Math.random() * 90 + 10); + int StartPositionY = (int)(Math.random() * 90 + 10); + int speed = (int)(Math.random() * 300 + 100); + double weight = (double)(Math.random() * 3000 + 1000); + Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); + switch (typeOfClass) { + case "DrawingWarhip": + canvasBattleship._drawingWarship = new DrawingWarship(speed, weight, bodyColor); + canvasBattleship._drawingWarship.SetPictureSize(Width, Height); + canvasBattleship._drawingWarship.SetPosition(StartPositionX, StartPositionY); + canvasBattleship.repaint(); + break; + case "DrawingBattleship": + Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));; + boolean bodyDeck = new Random().nextBoolean(); + boolean compartment = new Random().nextBoolean();; + boolean tower = new Random().nextBoolean();; + canvasBattleship._drawingWarship = new DrawingBattleship(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower); + canvasBattleship._drawingWarship.SetPictureSize(Width, Height); + canvasBattleship._drawingWarship.SetPosition(StartPositionX, StartPositionY); + canvasBattleship.repaint(); + break; + default: return; + } + _strategy = null; + ComboBoxStrategy.setEnabled(true); + } public void Init() { setTitle(title); setMinimumSize(dimension); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - Width = getWidth() - 15; - Height = getHeight() - 35; + + Width = getWidth() - 10; + Height = getHeight() - 34; + _strategy = null; CreateButton.setName("CREATE"); + CreateShipButton.setName("CREATESHIPBUTTON"); Icon iconUp = new ImageIcon("src/res/arrowUp.png"); UpButton.setIcon(iconUp); - UpButton.setName("Up"); - DownButton.setName("Down"); + UpButton.setName("UP"); + DownButton.setName("DOWN"); Icon iconDown = new ImageIcon("src/res/arrowDown.png"); DownButton.setIcon(iconDown); - LeftButton.setName("Left"); + LeftButton.setName("LEFT"); Icon iconLeft = new ImageIcon("src/res/arrowLeft.png"); LeftButton.setIcon(iconLeft); - RightButton.setName("Right"); + RightButton.setName("RIGHT"); Icon iconRight = new ImageIcon("src/res/arrowRight.png"); RightButton.setIcon(iconRight); CreateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - int StartPositionX = (int)(Math.random() * 90 + 10); - int StartPositionY = (int)(Math.random() * 90 + 10); - int speed = (int)(Math.random() * 300 + 100); - double weight = (double)(Math.random() * 3000 + 1000); - Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); - Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));; - boolean bodyDeck = new Random().nextBoolean(); - boolean compartment = new Random().nextBoolean();; - boolean tower = new Random().nextBoolean(); - int blocksNum = (int)(Math.random() * 6) + 1; - canvasBattleship._drawingBattleship = new DrawingBattleship(); - canvasBattleship._drawingBattleship.Init(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower, blocksNum); - canvasBattleship._drawingBattleship.SetPictureSize(Width, Height); - canvasBattleship._drawingBattleship.SetPosition(StartPositionX, StartPositionY); - canvasBattleship.repaint(); + CreateObject("DrawingBattleship"); + } + }); + + CreateShipButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawingWarhip"); + } + }); + + 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._drawingBattleship == null) return; + if (canvasBattleship._drawingWarship == null) return; boolean result = false; switch ((((JButton)(event.getSource())).getName())) { - case "Up": - result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Up); + case "UP": + result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Up); break; - case "Down": - result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Down); + case "DOWN": + result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Down); break; - case "Left": - result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Left); + case "LEFT": + result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Left); break; - case "Right": - result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Right); + case "RIGHT": + result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Right); break; } if (result) { @@ -94,31 +167,40 @@ public class FormBattleship extends JFrame { setSize(dimension.width,dimension.height); setLayout(null); canvasBattleship.setBounds(0,0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 100, 40); + CreateButton.setBounds(10, getHeight() - 90, 140, 40); + CreateShipButton.setBounds(160, getHeight() - 90, 140, 40); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); RightButton.setBounds(getWidth() - 80, 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); add(CreateButton); + add(CreateShipButton); add(UpButton); add(DownButton); add(RightButton); add(LeftButton); + add(ComboBoxStrategy); + add(ButtonStrategy); add(canvasBattleship); setVisible(true); //обработка события изменения размеров окна addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { - Width = getWidth() - 15; - Height = getHeight() - 35; - if (canvasBattleship._drawingBattleship != null) - canvasBattleship._drawingBattleship.SetPictureSize(Width, Height); + Width = getWidth() - 10; + Height = getHeight() - 34; + if (canvasBattleship._drawingWarship != null) + canvasBattleship._drawingWarship.SetPictureSize(Width, Height); canvasBattleship.setBounds(0,0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 100, 40); + CreateButton.setBounds(10, getHeight() - 90, 140, 40); + CreateShipButton.setBounds(160, getHeight() - 90, 140, 40); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); RightButton.setBounds(getWidth() - 80, 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); } }); } diff --git a/src/src/MovementStrategy/AbstractStrategy.java b/src/src/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..9e72fd2 --- /dev/null +++ b/src/src/MovementStrategy/AbstractStrategy.java @@ -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; } +} diff --git a/src/src/MovementStrategy/IMoveableObjects.java b/src/src/MovementStrategy/IMoveableObjects.java new file mode 100644 index 0000000..92a50be --- /dev/null +++ b/src/src/MovementStrategy/IMoveableObjects.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public interface IMoveableObjects { + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean TryMoveObject(MovementDirection direction); +} \ No newline at end of file diff --git a/src/src/MovementStrategy/MoveToBorder.java b/src/src/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..647a390 --- /dev/null +++ b/src/src/MovementStrategy/MoveToBorder.java @@ -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(); + } +} diff --git a/src/src/MovementStrategy/MoveToCenter.java b/src/src/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..74a093a --- /dev/null +++ b/src/src/MovementStrategy/MoveToCenter.java @@ -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(); + } + } + } +} diff --git a/src/src/MovementStrategy/MoveableWarship.java b/src/src/MovementStrategy/MoveableWarship.java new file mode 100644 index 0000000..2b46dda --- /dev/null +++ b/src/src/MovementStrategy/MoveableWarship.java @@ -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; + } + } +} diff --git a/src/src/MovementStrategy/MovementDirection.java b/src/src/MovementStrategy/MovementDirection.java new file mode 100644 index 0000000..c52f124 --- /dev/null +++ b/src/src/MovementStrategy/MovementDirection.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public enum MovementDirection { + Up, + Down, + Left, + Right +} diff --git a/src/src/MovementStrategy/ObjectParameters.java b/src/src/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..b68fe75 --- /dev/null +++ b/src/src/MovementStrategy/ObjectParameters.java @@ -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; + } +} diff --git a/src/src/MovementStrategy/StrategyStatus.java b/src/src/MovementStrategy/StrategyStatus.java new file mode 100644 index 0000000..950384f --- /dev/null +++ b/src/src/MovementStrategy/StrategyStatus.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public enum StrategyStatus { + NotIInit, + InProgress, + Finish +} diff --git a/src/src/NumberBlock.java b/src/src/NumberBlock.java deleted file mode 100644 index 4efd0c6..0000000 --- a/src/src/NumberBlock.java +++ /dev/null @@ -1,5 +0,0 @@ -public enum NumberBlock { - Two, - Four, - Six -} \ No newline at end of file