From df4d079c07706d9eae8a4b55f577a0bf63e6bd21 Mon Sep 17 00:00:00 2001 From: dimazhelovanov Date: Tue, 6 Dec 2022 16:23:28 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=BD=D1=8F=D1=82=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AbstractMap.java | 158 ++++++++++++++++++++++++++++++++++ DrawningBattleship.java | 49 ++++++++--- DrawningBlocks.java | 67 ++++++++++---- DrawningLinkor.java | 44 ++++++++++ DrawningObjectBattleship.java | 38 ++++++++ DrawningRoundBlocks.java | 48 +++++++++++ DrawningTriangleBlocks.java | 63 ++++++++++++++ EntityBattleship.java | 2 +- EntityLinkor.java | 15 ++++ FormBattleship.java | 26 +++++- FormMap.java | 156 +++++++++++++++++++++++++++++++++ IDrawningBlocks.java | 7 ++ IDrawningObject.java | 9 ++ SeaMap.java | 46 ++++++++++ SimpleMap.java | 43 +++++++++ 15 files changed, 739 insertions(+), 32 deletions(-) create mode 100644 AbstractMap.java create mode 100644 DrawningLinkor.java create mode 100644 DrawningObjectBattleship.java create mode 100644 DrawningRoundBlocks.java create mode 100644 DrawningTriangleBlocks.java create mode 100644 EntityLinkor.java create mode 100644 FormMap.java create mode 100644 IDrawningBlocks.java create mode 100644 IDrawningObject.java create mode 100644 SeaMap.java create mode 100644 SimpleMap.java diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..9e9e2a0 --- /dev/null +++ b/AbstractMap.java @@ -0,0 +1,158 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.BufferedReader; +import java.util.Random; + +public abstract class AbstractMap { + private IDrawningObject _drawningObject = 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, IDrawningObject drawningObject){ + _width = width; + _height = height; + _drawningObject = drawningObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + public BufferedImage MoveObject(Direction direction) + { + int _startX = (int) (_drawningObject.GetCurrentPosition()[3] / _size_x); + int _startY = (int)(_drawningObject.GetCurrentPosition()[0] / _size_y); + int _objWidth = (int)(_drawningObject.GetCurrentPosition()[1] / _size_x); + int _objHeight = (int)(_drawningObject.GetCurrentPosition()[2] / _size_y); + + boolean isMove = true; + switch (direction) + { + case Right: + for (int i = _objWidth; i <= _objWidth + (int)(_drawningObject.getStep() / _size_x); i++) + { + for (int j = _startY; j <= _objHeight; j++) + { + if (_map[i][j] == _barrier) + { + isMove = false; + break; + } + } + } + break; + + case Left: + for (int i = _startX; i >= (int)(_drawningObject.getStep() / _size_x); i--) + { + for (int j = _startY; j <= _objHeight; j++) + { + if (_map[i][j] == _barrier) + { + isMove = false; + break; + } + } + } + break; + + case Up: + for (int i = _startX; i <= _objWidth; i++) + { + for (int j = _startY; j >= (int)(_drawningObject.getStep() / _size_y); j--) + { + if (_map[i][j] == _barrier) + { + isMove = false; + break; + } + } + } + break; + + case Down: + for (int i = _startX; i <= _objWidth; i++) + { + for (int j = _objHeight; j <= _objHeight + (int)(_drawningObject.getStep() / _size_y); j++) + { + if (_map[i][j] == _barrier) + { + isMove = false; + break; + } + } + } + break; + } + + + if (isMove) + { + _drawningObject.MoveObject(direction); + } + return DrawMapWithObject(); + } + private boolean SetObjectOnMap() + { + if (_drawningObject == null || _map == null) + { + return false; + } + int x = _random.nextInt(0, 10); + int y = _random.nextInt(0, 10); + _drawningObject.SetObject(x, y, _width, _height); + // TODO првоерка, что объект не "накладывается" на закрытые участки + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + if (i * _size_x >= x && j * _size_y >= y && + i * _size_x <= x + _drawningObject.GetCurrentPosition()[1] && + j * _size_y <= j + _drawningObject.GetCurrentPosition()[2]) + { + if (_map[i][j] == _barrier) + { + return false; + } + } + } + } + + return true; + } + private BufferedImage DrawMapWithObject() + { + BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB); + if (_drawningObject == null || _map == null) + { + return bmp; + } + Graphics gr = bmp.getGraphics(); + for (int i = 0; i < _map.length; i++) + { + for (int j = 0; j < _map[0].length; j++) + { + if (_map[i][j] == _freeRoad) + { + DrawRoadPart(gr, i, j); + } + else if (_map[i][j] == _barrier) + { + DrawBarrierPart(gr, i, j); + } + } + } + _drawningObject.DrawningObject(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/DrawningBattleship.java b/DrawningBattleship.java index 3bc4844..cc6d3e2 100644 --- a/DrawningBattleship.java +++ b/DrawningBattleship.java @@ -1,18 +1,20 @@ import java.awt.*; +import java.util.Random; public class DrawningBattleship { EntityBattleship Battleship; private DrawningBlocks drawingBlocks; + private IDrawningBlocks iDrawingBlocks; public EntityBattleship Battleship() {return Battleship; } /// /// Левая координата отрисовки корабля /// - private float _startPosX; + protected float _startPosX; /// /// Верхняя кооридната отрисовки корабля /// - private float _startPosY; + protected float _startPosY; /// /// Ширина окна отрисовки /// @@ -24,23 +26,44 @@ public class DrawningBattleship { /// /// Ширина отрисовки корабля /// - private final int _battleshipWidth = 120; + private int _battleshipWidth = 120; /// /// Высота отрисовки корабля /// - private final int _battleshipHeight = 50; + private int _battleshipHeight = 50; /// /// Инициализация свойств /// /// Скорость /// Вес корабля /// Цвет корпуса - public void Init(int speed, float weight, Color bodyColor, EntityBattleship entityBattleship, int blocks) + public DrawningBattleship(int speed, float weight, Color bodyColor) { - Battleship = new EntityBattleship(); - Battleship.Init(speed, weight, bodyColor); - drawingBlocks = new DrawningBlocks(); - drawingBlocks.Init(blocks, Color.black); + + //Battleship.Init(speed, weight, bodyColor); + Random random = new Random(); + int[] ArrayBlocks = new int[]{2, 4, 6}; + + switch (random.nextInt(3)){ + case 0: + iDrawingBlocks = new DrawningBlocks(ArrayBlocks[random.nextInt(0, 3)], Color.BLACK); + break; + case 1: + iDrawingBlocks = new DrawningRoundBlocks(ArrayBlocks[random.nextInt(0, 3)]); + break; + case 2: + iDrawingBlocks = new DrawningTriangleBlocks(ArrayBlocks[random.nextInt(0, 3)]); + break; + + } + + Battleship = new EntityBattleship(speed, weight, bodyColor); + } + protected DrawningBattleship(int speed, float weight, Color bodyColor, int battleshipWidth, int battleshipHeight) + { + this(speed, weight, bodyColor); + _battleshipWidth = battleshipWidth; + _battleshipHeight = battleshipHeight; } /// /// Установка позиции корабля @@ -51,7 +74,6 @@ public class DrawningBattleship { /// Высота картинки public void SetPosition(int x, int y, int width, int height) { - if ((x > 0 && y > 0) && (_battleshipHeight + y < height) && (_battleshipWidth + x < width)) { _startPosX = x; @@ -137,7 +159,7 @@ public class DrawningBattleship { g.setColor(Color.BLACK); g.fillRect((int)_startPosX-5, (int)_startPosY+10, 5, 5); g.fillRect((int)_startPosX - 5, (int)_startPosY + 35, 5, 5); - drawingBlocks.Draw(g, (int) _startPosX, (int) _startPosY); + iDrawingBlocks.DrawBlocks(g, (int) _startPosX, (int) _startPosY, Color.BLACK); } /// @@ -163,5 +185,10 @@ public class DrawningBattleship { { _startPosY = _pictureHeight - _battleshipHeight; } + + } + public float[] GetCurrentPosition() + { + return new float[] {_startPosY, _startPosX + _battleshipWidth, /*DOWN*/ _startPosY + _battleshipHeight, /*LEFT*/ _startPosX}; } } diff --git a/DrawningBlocks.java b/DrawningBlocks.java index 61b2d20..727b698 100644 --- a/DrawningBlocks.java +++ b/DrawningBlocks.java @@ -1,7 +1,7 @@ import java.awt.*; -public class DrawningBlocks { - private DirectionBlocksOnDeck blocksCount; +public class DrawningBlocks implements IDrawningBlocks { + private DirectionBlocksOnDeck blocksCount = DirectionBlocksOnDeck.Two; private Color color; public void SetNewBlocks(int countBlocks){ if (countBlocks == 4) { @@ -13,28 +13,63 @@ public class DrawningBlocks { blocksCount = DirectionBlocksOnDeck.Two; } } - public void Init(int blocksCount, Color color) - { - SetNewBlocks(blocksCount); + public DrawningBlocks(int count, Color color){ + SetBlocks(count); this.color = color; - } + } public void Draw(Graphics2D g, int x, int y) { g.setColor(color != null ? color : Color.BLACK); switch (blocksCount) { case Four -> { - g.fillRect(x + 26, y + 10, 10, 5); - g.fillRect(x + 38, y + 10, 10, 5); - g.fillRect(x + 38, y + 35, 10, 5); - g.fillRect(x + 26, y + 35, 10, 5); + g.fillRect(x + 36, y + 5, 10, 5); + g.fillRect(x + 48, y + 5, 10, 5); + g.fillRect(x + 38, y + 40, 10, 5); + g.fillRect(x + 46, y + 40, 10, 5); } case Six -> { - g.fillRect(x + 14, y + 10, 10, 5); - g.fillRect(x + 26, y + 10, 10, 5); - g.fillRect(x + 38, y + 10, 10, 5); - g.fillRect(x + 38, y + 35, 10, 5); - g.fillRect(x + 26, y + 35, 10, 5); - g.fillRect(x + 14, y + 35, 10, 5); + g.fillRect(x + 14, y + 5, 10, 5); + g.fillRect(x + 26, y + 5, 10, 5); + g.fillRect(x + 38, y + 5, 10, 5); + g.fillRect(x + 38, y + 40, 10, 5); + g.fillRect(x + 26, y + 40, 10, 5); + g.fillRect(x + 14, y + 40, 10, 5); } } } + + @Override + public void DrawBlocks(Graphics g, int x, int y, Color bodyColor) { + g.setColor(color != null ? color : Color.BLACK); + switch (blocksCount) { + case Four -> { + g.fillRect(x + 58, y, 8, 5); + g.fillRect(x + 70, y, 8, 5); + g.fillRect(x + 70, y + 40, 8, 5); + g.fillRect(x + 58, y + 40, 8, 5); + } + case Six -> { + g.fillRect(x + 46, y, 8, 5); + g.fillRect(x + 58, y , 8, 5); + g.fillRect(x + 70, y, 8, 5); + g.fillRect(x + 46, y + 45, 8, 5); + g.fillRect(x + 58, y + 45, 8, 5); + g.fillRect(x + 70, y + 45, 8, 5); + } + } + } + + @Override + public void SetBlocks(int count) { + switch(count) + { + case 4: + blocksCount = DirectionBlocksOnDeck.Four; + break; + case 6: + blocksCount = DirectionBlocksOnDeck.Six; + break; + default: + break; + } + } } diff --git a/DrawningLinkor.java b/DrawningLinkor.java new file mode 100644 index 0000000..8be96de --- /dev/null +++ b/DrawningLinkor.java @@ -0,0 +1,44 @@ +import java.awt.*; + +public class DrawningLinkor extends DrawningBattleship { + + public DrawningLinkor(int speed, float weight, Color bodyColor, Color dopColor, boolean turret, boolean missileBay) { + super(speed, weight, bodyColor, 120, 50); + Battleship = new EntityLinkor(speed, weight, bodyColor, dopColor, turret, missileBay); + } + + @Override + public void DrawTransport(Graphics2D g) { + + if(!(Battleship instanceof EntityLinkor linkor)) + { + return; + } + super.DrawTransport(g); + EntityLinkor entityLinkor = (EntityLinkor) Battleship; + + if (entityLinkor.turret) + { + //Brush dopBrushRed = new SolidBrush(Color.Red); + g.setColor(entityLinkor.dopColor); + g.fillOval((int)_startPosX + 15, (int)_startPosY, 20, 20); + g.fillOval((int) _startPosX + 15, (int)_startPosY + 30, 20, 20); + g.setColor(Color.BLACK); + g.fillRect((int)_startPosX + 20, (int)_startPosY+5, 23, 7); + g.fillRect((int)_startPosX + 20,(int) _startPosY + 37, 23, 7); + + + } + if (entityLinkor.missileBay) + { + + g.setColor(Color.BLACK); + g.fillOval((int)_startPosX, (int)_startPosY + 15, 20, 20); + g.setColor(entityLinkor.dopColor); + g.fillOval((int)_startPosX+5,(int) _startPosY + 20, 10, 10); + + + + } + } +} diff --git a/DrawningObjectBattleship.java b/DrawningObjectBattleship.java new file mode 100644 index 0000000..16a01a8 --- /dev/null +++ b/DrawningObjectBattleship.java @@ -0,0 +1,38 @@ +import java.awt.*; + +public class DrawningObjectBattleship implements IDrawningObject { + + private DrawningBattleship _battleship = null; + public DrawningObjectBattleship(DrawningBattleship battleship){ +_battleship = battleship; + } + @Override + public float getStep() { + if (_battleship.Battleship != null) { + return _battleship.Battleship.GetStep(); + } + return 0; + } + + @Override + public void SetObject(int x, int y, int width, int height) { + if (_battleship != null) _battleship.SetPosition(x, y, width, height); + } + + @Override + public void MoveObject(Direction direction) { + if (_battleship!= null) _battleship.MoveTransport(direction); + } + + @Override + public void DrawningObject(Graphics g) { + if (_battleship != null) _battleship.DrawTransport((Graphics2D) g); + } + + @Override + public float[] GetCurrentPosition() { + if (_battleship != null) {return _battleship.GetCurrentPosition();} + return null; + } + +} diff --git a/DrawningRoundBlocks.java b/DrawningRoundBlocks.java new file mode 100644 index 0000000..48da31d --- /dev/null +++ b/DrawningRoundBlocks.java @@ -0,0 +1,48 @@ +import java.awt.*; + +public class DrawningRoundBlocks implements IDrawningBlocks{ + + private DirectionBlocksOnDeck blocksOnDeck = DirectionBlocksOnDeck.Two; + @Override + public void DrawBlocks(Graphics g, int x, int y, Color bodyColor) { + g.setColor(Color.black); + switch(blocksOnDeck){ + case Four -> { + g.fillOval(x + 56, y , 8, 8); + g.fillOval(x + 68, y , 8, 8); + g.fillOval(x + 68, y +40, 8, 8); + g.fillOval(x + 56, y + 40, 8, 8); + } + case Six -> { + g.fillOval(x + 54, y , 8, 8); + g.fillOval(x + 66, y , 8, 8); + g.fillOval(x + 78, y , 8, 8); + g.fillOval(x + 78, y + 40, 8, 8); + g.fillOval(x + 66, y + 40, 8, 8); + g.fillOval(x + 54, y + 40, 8, 8); + }} + } + + @Override + public void SetBlocks(int count) { + switch(count) + { + case 2: + blocksOnDeck = DirectionBlocksOnDeck.Two; + break; + case 4: + blocksOnDeck = DirectionBlocksOnDeck.Four; + break; + case 6: + blocksOnDeck = DirectionBlocksOnDeck.Six; + break; + default: + break; + } + } + + public DrawningRoundBlocks (int num) { + SetBlocks(num); + + } +} diff --git a/DrawningTriangleBlocks.java b/DrawningTriangleBlocks.java new file mode 100644 index 0000000..cf4cd97 --- /dev/null +++ b/DrawningTriangleBlocks.java @@ -0,0 +1,63 @@ +import java.awt.*; + +public class DrawningTriangleBlocks implements IDrawningBlocks { + + private DirectionBlocksOnDeck blocksOnDeck = DirectionBlocksOnDeck.Two; + + @Override + public void DrawBlocks(Graphics g, int x, int y, Color bodyColor) { + g.setColor(Color.black); + switch(blocksOnDeck){ + case Four -> { + g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 7, y, y + 7}, 3); + g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 49, y + 42, y + 49}, 3); + + g.fillPolygon(new int[]{x + 58, x + 66, x+ 72}, new int[]{y + 7, y, y + 7}, 3); + g.fillPolygon(new int[]{x + 58, x + 66, x+72}, new int[]{y + 49, y + 42, y + 49}, 3); + +// g.fillOval(x + 26, y + 10, 8, 8); +// g.fillOval(x + 38, y + 10, 8, 8); +// g.fillOval(x + 38, y + 35, 8, 8); +// g.fillOval(x + 26, y + 35, 8, 8); + + } + case Six -> { + g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 7, y, y + 7}, 3); + g.fillPolygon(new int[]{x + 66, x + 64, x+ 80 }, new int[]{y + 7, y, y + 7}, 3); + g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 49, y + 42, y + 49}, 3); + g.fillPolygon(new int[]{x + 66, x + 64, x+ 80 }, new int[]{y + 49, y + 42, y + 49}, 3); + g.fillPolygon(new int[]{x + 58, x + 66, x+ 72}, new int[]{y + 7, y, y + 7}, 3); + g.fillPolygon(new int[]{x + 58, x + 66, x+72}, new int[]{y + 49, y + 42, y + 49}, 3); + // g.fillOval(x + 14, y + 10, 8, 8); +// g.fillOval(x + 26, y + 10, 8, 8); +// g.fillOval(x + 38, y + 10, 8, 8); +// g.fillOval(x + 38, y + 35, 8, 8); +// g.fillOval(x + 26, y + 35, 8, 8); +// g.fillOval(x + 14, y + 35, 8, 8); + + }} + } + + @Override + public void SetBlocks(int count) { + switch(count) + { + case 2: + blocksOnDeck = DirectionBlocksOnDeck.Two; + break; + case 4: + blocksOnDeck = DirectionBlocksOnDeck.Four; + break; + case 6: + blocksOnDeck = DirectionBlocksOnDeck.Six; + break; + default: + break; + } + + } + public DrawningTriangleBlocks (int num) { + SetBlocks(num); + + } +} diff --git a/EntityBattleship.java b/EntityBattleship.java index 7f26f3e..fe8a1c5 100644 --- a/EntityBattleship.java +++ b/EntityBattleship.java @@ -30,7 +30,7 @@ public class EntityBattleship { /// /// /// - public void Init(int speed, float weight, Color bodyColor) + public EntityBattleship(int speed, float weight, Color bodyColor) { Random rnd = new Random(); this.speed = speed <= 0 ? rnd.nextInt(950) + 1050 : speed; diff --git a/EntityLinkor.java b/EntityLinkor.java new file mode 100644 index 0000000..59f0341 --- /dev/null +++ b/EntityLinkor.java @@ -0,0 +1,15 @@ +import java.awt.*; + +public class EntityLinkor extends EntityBattleship { + public final boolean turret; + public final boolean missileBay; + public final Color dopColor; + + public EntityLinkor(int speed, float weight, Color bodyColor, Color DopColor, boolean Turret, boolean MissileBay) { + super(speed, weight, bodyColor); + dopColor = DopColor; + missileBay = MissileBay; + turret = Turret; + + } +} diff --git a/FormBattleship.java b/FormBattleship.java index cf501cc..b9fa9da 100644 --- a/FormBattleship.java +++ b/FormBattleship.java @@ -9,7 +9,7 @@ public class FormBattleship extends JComponent { private DrawningBattleship _battleship; private EntityBattleship _entityBattleship; public static void main(String[] args) { - FormBattleship formBattleship = new FormBattleship(); + FormMap formMap = new FormMap(); } public FormBattleship() { JFrame form = new JFrame("Военный корабль"); @@ -51,10 +51,9 @@ public class FormBattleship extends JComponent { int[] countBlocks = {2, 4, 6}; Random rnd = new Random(); - _battleship = new DrawningBattleship(); - _battleship.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + _battleship = new DrawningBattleship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256), - rnd.nextInt(0, 256)), _entityBattleship, countBlocks[rnd.nextInt(0, 3)]); + rnd.nextInt(0, 256))); _battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), form.getWidth(), form.getHeight() - 75); speedLabel.setText("Скорость: " + _battleship.Battleship.GetSpeed()); weightLabel.setText("Вес: " + (int)_battleship.Battleship.GetWeight()); @@ -63,7 +62,26 @@ public class FormBattleship extends JComponent { repaint(); }); + JButton modifiedButton = new JButton("Модификация"); + modifiedButton.addActionListener(e -> { + Random rnd = new Random(); + + Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); + Color colorSecond = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); + + _battleship = new DrawningLinkor(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, + colorFirst, + colorSecond, + rnd.nextBoolean(), + rnd.nextBoolean()); + _battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75); +// speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed()); +// weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight()); +// colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() ); + repaint(); + }); statusPanel.add(createButton); + statusPanel.add(modifiedButton); statusPanel.add(speedLabel); statusPanel.add(weightLabel); statusPanel.add(colorLabel); diff --git a/FormMap.java b/FormMap.java new file mode 100644 index 0000000..cf2bd04 --- /dev/null +++ b/FormMap.java @@ -0,0 +1,156 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentEvent; +import java.awt.event.ComponentListener; +import java.awt.image.BufferedImage; +import java.util.Random; +public class FormMap extends JComponent { + private AbstractMap _abstractMap; + private DrawningBattleship _battleship; + private BufferedImage bufferedImage; + public static void main(String[] args) { + FormMap formMap = new FormMap(); + } + public FormMap() { + JFrame form = new JFrame("Карта"); + form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + form.setSize(1000, 500); + form.setVisible(true); + form.setLocationRelativeTo(null); +// form.addComponentListener(new ComponentListener() { +// @Override +// public void componentResized(ComponentEvent e) { +// if(_battleship != null) _battleship.ChangeBorders(getWidth(), getHeight()); +// repaint(); +// } +// +// @Override +// public void componentMoved(ComponentEvent e) { +// } +// +// @Override +// public void componentShown(ComponentEvent e) { +// } +// +// @Override +// public void componentHidden(ComponentEvent e) { +// } +// }); + Panel statusPanel = new Panel(); + statusPanel.setBackground(Color.WHITE); + statusPanel.setLayout(new FlowLayout()); + setLayout(new BorderLayout()); + form.add(statusPanel, BorderLayout.SOUTH); + Label speedLabel = new Label("Скорость: "); + Label weightLabel = new Label("Вес: "); + Label colorLabel = new Label("Цвет: "); + + String[] maps = { + "Simple Map", + "Sea Map" + + }; + JComboBox mapSelectComboBox = new JComboBox(maps); + mapSelectComboBox.setEditable(true); + mapSelectComboBox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String item = (String)mapSelectComboBox.getSelectedItem(); + if (item == null) return; + switch (item) { + case ("Simple Map"): + _abstractMap = new SimpleMap(); + break; + case ("Sea Map"): + _abstractMap = new SeaMap(); + break; + + } + } + }); + JButton createButton = new JButton("Создать"); + createButton.addActionListener(e -> { + + Random rnd = new Random(); + + var battleship = new DrawningBattleship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256), + rnd.nextInt(0, 256))); + speedLabel.setText("Скорость: " + battleship.Battleship.GetSpeed()); + weightLabel.setText("Вес: " + (int)battleship.Battleship.GetWeight()); + colorLabel.setText("Цвет: " + battleship.Battleship.GetBodyColor().getRed() + " " + + battleship.Battleship.GetBodyColor().getGreen() + " " + battleship.Battleship.GetBodyColor().getBlue() ); + if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(1000, 490, new DrawningObjectBattleship(battleship)); + repaint(); + + }); + JButton modifiedButton = new JButton("Модификация"); + modifiedButton.addActionListener(e -> { + Random rnd = new Random(); + + Color colorFirst = new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)); + Color colorSecond = new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)); + + var battleship = new DrawningLinkor(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, + colorFirst, + colorSecond, + rnd.nextBoolean(), + rnd.nextBoolean()); + battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75); + speedLabel.setText("Speed: " + battleship.Battleship.GetSpeed()); + weightLabel.setText("Weight: " + battleship.Battleship.GetWeight()); + colorLabel.setText("Color: " + battleship.Battleship.GetBodyColor().getRed() + " " + battleship.Battleship.GetBodyColor().getGreen() + " " + battleship.Battleship.GetBodyColor().getBlue() ); + if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(1000, 490, new DrawningObjectBattleship(battleship)); + repaint(); + }); + statusPanel.add(mapSelectComboBox); + statusPanel.add(createButton); + statusPanel.add(modifiedButton); + statusPanel.add(speedLabel); + statusPanel.add(weightLabel); + statusPanel.add(colorLabel); + JButton upButton = new JButton("↑"); + JButton rightButton = new JButton("→"); + JButton leftButton = new JButton("←"); + JButton downButton = new JButton("↓"); + upButton.addActionListener(e -> { + if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Up); + repaint(); + }); + + + rightButton.addActionListener(e -> { + if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Right); + repaint(); + }); + + + leftButton.addActionListener(e -> { + if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Left); + repaint(); + }); + + + downButton.addActionListener(e -> { + if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Down); + repaint(); + }); + + statusPanel.add(leftButton); + statusPanel.add(upButton); + statusPanel.add(rightButton); + statusPanel.add(downButton); + + form.getContentPane().add(this); + super.repaint(); + } + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if (bufferedImage != null) g2.drawImage(bufferedImage, 0,0,1000,490,null); + super.repaint(); + } + +} diff --git a/IDrawningBlocks.java b/IDrawningBlocks.java new file mode 100644 index 0000000..1b96a44 --- /dev/null +++ b/IDrawningBlocks.java @@ -0,0 +1,7 @@ +import java.awt.*; + +public interface IDrawningBlocks { + void DrawBlocks(Graphics g, int startX, int startY, Color bodyColor); + void SetBlocks(int count); + +} diff --git a/IDrawningObject.java b/IDrawningObject.java new file mode 100644 index 0000000..af06c5b --- /dev/null +++ b/IDrawningObject.java @@ -0,0 +1,9 @@ +import java.awt.*; + +public interface IDrawningObject { + float getStep(); + void SetObject(int x, int y, int width, int height); + void MoveObject(Direction direction); + void DrawningObject(Graphics g); + float[] GetCurrentPosition(); +} diff --git a/SeaMap.java b/SeaMap.java new file mode 100644 index 0000000..f35f2a7 --- /dev/null +++ b/SeaMap.java @@ -0,0 +1,46 @@ +import java.awt.*; + +public class SeaMap extends AbstractMap { + + @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[0].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + while (counter < 10) + { + int x = _random.nextInt(1, 99); + int y = _random.nextInt(1, 100); + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + _map[x + 1][y] = _barrier; + counter++; + } + } + } + + @Override + protected void DrawRoadPart(Graphics g, int i, int j) { + g.setColor(Color.BLUE); + g.fillRect((int) (i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1))); + + + + } + + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) { + g.setColor(Color.WHITE); + g.fillPolygon(new int[]{i * (int) _size_x, i * (int) _size_x + 18, i * (int) _size_x - 18},new int[]{ j * (int) _size_y, j * (int) _size_y + 20, + j * (int) _size_y + 20}, 3); + } +} diff --git a/SimpleMap.java b/SimpleMap.java new file mode 100644 index 0000000..5f0da93 --- /dev/null +++ b/SimpleMap.java @@ -0,0 +1,43 @@ +import java.awt.*; + +public class SimpleMap extends AbstractMap { + + Color roadColor = Color.GRAY; + Color barrierColor = Color.BLACK; + @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[0].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++; + } + } + } + + @Override + protected void DrawRoadPart(Graphics g, int i, int j) { + g.setColor(roadColor); + g.fillRect((int) (i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1))); + } + + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) { + g.setColor(barrierColor); + g.fillRect( (int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1))); + } +} -- 2.25.1 From e71eaf0f2daecb41b1b9c7c4d123ffe5a6636968 Mon Sep 17 00:00:00 2001 From: dimazhelovanov Date: Thu, 15 Dec 2022 23:04:08 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AbstractMap.java | 1 - DrawningBlocks.java | 30 --------- DrawningLinkor.java | 3 - DrawningTriangleBlocks.java | 11 +-- EntityLinkor.java | 1 - FormBattleship.java | 129 ------------------------------------ FormMap.java | 33 ++------- SeaMap.java | 2 - 8 files changed, 8 insertions(+), 202 deletions(-) delete mode 100644 FormBattleship.java diff --git a/AbstractMap.java b/AbstractMap.java index 9e9e2a0..a672a1f 100644 --- a/AbstractMap.java +++ b/AbstractMap.java @@ -91,7 +91,6 @@ public abstract class AbstractMap { break; } - if (isMove) { _drawningObject.MoveObject(direction); diff --git a/DrawningBlocks.java b/DrawningBlocks.java index 727b698..fec7e46 100644 --- a/DrawningBlocks.java +++ b/DrawningBlocks.java @@ -3,40 +3,10 @@ import java.awt.*; public class DrawningBlocks implements IDrawningBlocks { private DirectionBlocksOnDeck blocksCount = DirectionBlocksOnDeck.Two; private Color color; - public void SetNewBlocks(int countBlocks){ - if (countBlocks == 4) { - blocksCount = DirectionBlocksOnDeck.Four; - } else if (countBlocks == 6) { - blocksCount = DirectionBlocksOnDeck.Six; - } - else { - blocksCount = DirectionBlocksOnDeck.Two; - } - } public DrawningBlocks(int count, Color color){ SetBlocks(count); this.color = color; } - public void Draw(Graphics2D g, int x, int y) { - g.setColor(color != null ? color : Color.BLACK); - switch (blocksCount) { - case Four -> { - g.fillRect(x + 36, y + 5, 10, 5); - g.fillRect(x + 48, y + 5, 10, 5); - g.fillRect(x + 38, y + 40, 10, 5); - g.fillRect(x + 46, y + 40, 10, 5); - } - case Six -> { - g.fillRect(x + 14, y + 5, 10, 5); - g.fillRect(x + 26, y + 5, 10, 5); - g.fillRect(x + 38, y + 5, 10, 5); - g.fillRect(x + 38, y + 40, 10, 5); - g.fillRect(x + 26, y + 40, 10, 5); - g.fillRect(x + 14, y + 40, 10, 5); - } - } - } - @Override public void DrawBlocks(Graphics g, int x, int y, Color bodyColor) { g.setColor(color != null ? color : Color.BLACK); diff --git a/DrawningLinkor.java b/DrawningLinkor.java index 8be96de..565fd10 100644 --- a/DrawningLinkor.java +++ b/DrawningLinkor.java @@ -27,7 +27,6 @@ public class DrawningLinkor extends DrawningBattleship { g.fillRect((int)_startPosX + 20, (int)_startPosY+5, 23, 7); g.fillRect((int)_startPosX + 20,(int) _startPosY + 37, 23, 7); - } if (entityLinkor.missileBay) { @@ -37,8 +36,6 @@ public class DrawningLinkor extends DrawningBattleship { g.setColor(entityLinkor.dopColor); g.fillOval((int)_startPosX+5,(int) _startPosY + 20, 10, 10); - - } } } diff --git a/DrawningTriangleBlocks.java b/DrawningTriangleBlocks.java index cf4cd97..4557e57 100644 --- a/DrawningTriangleBlocks.java +++ b/DrawningTriangleBlocks.java @@ -15,10 +15,7 @@ public class DrawningTriangleBlocks implements IDrawningBlocks { g.fillPolygon(new int[]{x + 58, x + 66, x+ 72}, new int[]{y + 7, y, y + 7}, 3); g.fillPolygon(new int[]{x + 58, x + 66, x+72}, new int[]{y + 49, y + 42, y + 49}, 3); -// g.fillOval(x + 26, y + 10, 8, 8); -// g.fillOval(x + 38, y + 10, 8, 8); -// g.fillOval(x + 38, y + 35, 8, 8); -// g.fillOval(x + 26, y + 35, 8, 8); + } case Six -> { @@ -28,12 +25,6 @@ public class DrawningTriangleBlocks implements IDrawningBlocks { g.fillPolygon(new int[]{x + 66, x + 64, x+ 80 }, new int[]{y + 49, y + 42, y + 49}, 3); g.fillPolygon(new int[]{x + 58, x + 66, x+ 72}, new int[]{y + 7, y, y + 7}, 3); g.fillPolygon(new int[]{x + 58, x + 66, x+72}, new int[]{y + 49, y + 42, y + 49}, 3); - // g.fillOval(x + 14, y + 10, 8, 8); -// g.fillOval(x + 26, y + 10, 8, 8); -// g.fillOval(x + 38, y + 10, 8, 8); -// g.fillOval(x + 38, y + 35, 8, 8); -// g.fillOval(x + 26, y + 35, 8, 8); -// g.fillOval(x + 14, y + 35, 8, 8); }} } diff --git a/EntityLinkor.java b/EntityLinkor.java index 59f0341..f6edc34 100644 --- a/EntityLinkor.java +++ b/EntityLinkor.java @@ -10,6 +10,5 @@ public class EntityLinkor extends EntityBattleship { dopColor = DopColor; missileBay = MissileBay; turret = Turret; - } } diff --git a/FormBattleship.java b/FormBattleship.java deleted file mode 100644 index b9fa9da..0000000 --- a/FormBattleship.java +++ /dev/null @@ -1,129 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.ComponentEvent; -import java.awt.event.ComponentListener; -import java.util.Random; -public class FormBattleship extends JComponent { - private DrawningBattleship _battleship; - private EntityBattleship _entityBattleship; - public static void main(String[] args) { - FormMap formMap = new FormMap(); - } - public FormBattleship() { - JFrame form = new JFrame("Военный корабль"); - form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - form.setSize(800, 500); - form.setVisible(true); - form.setLocationRelativeTo(null); - form.addComponentListener(new ComponentListener() { - @Override - public void componentResized(ComponentEvent e) { - if(_battleship != null) _battleship.ChangeBorders(getWidth(), getHeight()); - repaint(); - } - - @Override - public void componentMoved(ComponentEvent e) { - } - - @Override - public void componentShown(ComponentEvent e) { - } - - @Override - public void componentHidden(ComponentEvent e) { - } - }); - Panel statusPanel = new Panel(); - statusPanel.setBackground(Color.WHITE); - statusPanel.setLayout(new FlowLayout()); - setLayout(new BorderLayout()); - form.add(statusPanel, BorderLayout.SOUTH); - Label speedLabel = new Label("Скорость: "); - Label weightLabel = new Label("Вес: "); - Label colorLabel = new Label("Цвет: "); - - - JButton createButton = new JButton("Создать"); - createButton.addActionListener(e -> { - int[] countBlocks = {2, 4, 6}; - Random rnd = new Random(); - - _battleship = new DrawningBattleship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), - Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256), - rnd.nextInt(0, 256))); - _battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), form.getWidth(), form.getHeight() - 75); - speedLabel.setText("Скорость: " + _battleship.Battleship.GetSpeed()); - weightLabel.setText("Вес: " + (int)_battleship.Battleship.GetWeight()); - colorLabel.setText("Цвет: " + _battleship.Battleship.GetBodyColor().getRed() + " " + - _battleship.Battleship.GetBodyColor().getGreen() + " " + _battleship.Battleship.GetBodyColor().getBlue() ); - repaint(); - - }); - JButton modifiedButton = new JButton("Модификация"); - modifiedButton.addActionListener(e -> { - Random rnd = new Random(); - - Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); - Color colorSecond = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); - - _battleship = new DrawningLinkor(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, - colorFirst, - colorSecond, - rnd.nextBoolean(), - rnd.nextBoolean()); - _battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75); -// speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed()); -// weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight()); -// colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() ); - repaint(); - }); - statusPanel.add(createButton); - statusPanel.add(modifiedButton); - statusPanel.add(speedLabel); - statusPanel.add(weightLabel); - statusPanel.add(colorLabel); - JButton upButton = new JButton("↑"); - JButton rightButton = new JButton("→"); - JButton leftButton = new JButton("←"); - JButton downButton = new JButton("↓"); - upButton.addActionListener(e -> { - if (_battleship != null) _battleship.MoveTransport(Direction.Up); - repaint(); - }); - - - rightButton.addActionListener(e -> { - if (_battleship != null) _battleship.MoveTransport(Direction.Right); - repaint(); - }); - - - leftButton.addActionListener(e -> { - if (_battleship != null) _battleship.MoveTransport(Direction.Left); - repaint(); - }); - - - downButton.addActionListener(e -> { - if (_battleship != null) _battleship.MoveTransport(Direction.Down); - repaint(); - }); - - statusPanel.add(leftButton); - statusPanel.add(upButton); - statusPanel.add(rightButton); - statusPanel.add(downButton); - - form.getContentPane().add(this); - } - protected void paintComponent(Graphics g) { - super.paintComponent(g); - Graphics2D g2 = (Graphics2D)g; - if (_battleship != null) _battleship.DrawTransport(g2); - super.repaint(); - } - -} diff --git a/FormMap.java b/FormMap.java index cf2bd04..286fce5 100644 --- a/FormMap.java +++ b/FormMap.java @@ -19,25 +19,6 @@ public class FormMap extends JComponent { form.setSize(1000, 500); form.setVisible(true); form.setLocationRelativeTo(null); -// form.addComponentListener(new ComponentListener() { -// @Override -// public void componentResized(ComponentEvent e) { -// if(_battleship != null) _battleship.ChangeBorders(getWidth(), getHeight()); -// repaint(); -// } -// -// @Override -// public void componentMoved(ComponentEvent e) { -// } -// -// @Override -// public void componentShown(ComponentEvent e) { -// } -// -// @Override -// public void componentHidden(ComponentEvent e) { -// } -// }); Panel statusPanel = new Panel(); statusPanel.setBackground(Color.WHITE); statusPanel.setLayout(new FlowLayout()); @@ -48,8 +29,8 @@ public class FormMap extends JComponent { Label colorLabel = new Label("Цвет: "); String[] maps = { - "Simple Map", - "Sea Map" + "Простая карта", + "Морская карта" }; JComboBox mapSelectComboBox = new JComboBox(maps); @@ -60,10 +41,10 @@ public class FormMap extends JComponent { String item = (String)mapSelectComboBox.getSelectedItem(); if (item == null) return; switch (item) { - case ("Simple Map"): + case ("Простая карта"): _abstractMap = new SimpleMap(); break; - case ("Sea Map"): + case ("Морская карта"): _abstractMap = new SeaMap(); break; @@ -99,9 +80,9 @@ public class FormMap extends JComponent { rnd.nextBoolean(), rnd.nextBoolean()); battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75); - speedLabel.setText("Speed: " + battleship.Battleship.GetSpeed()); - weightLabel.setText("Weight: " + battleship.Battleship.GetWeight()); - colorLabel.setText("Color: " + battleship.Battleship.GetBodyColor().getRed() + " " + battleship.Battleship.GetBodyColor().getGreen() + " " + battleship.Battleship.GetBodyColor().getBlue() ); + speedLabel.setText("Скорость: " + battleship.Battleship.GetSpeed()); + weightLabel.setText("Вес: " + battleship.Battleship.GetWeight()); + colorLabel.setText("Цвет: " + battleship.Battleship.GetBodyColor().getRed() + " " + battleship.Battleship.GetBodyColor().getGreen() + " " + battleship.Battleship.GetBodyColor().getBlue() ); if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(1000, 490, new DrawningObjectBattleship(battleship)); repaint(); }); diff --git a/SeaMap.java b/SeaMap.java index f35f2a7..501ba8f 100644 --- a/SeaMap.java +++ b/SeaMap.java @@ -33,8 +33,6 @@ public class SeaMap extends AbstractMap { g.setColor(Color.BLUE); g.fillRect((int) (i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1))); - - } @Override -- 2.25.1