From 973cec8de79ee2fecd3758afc311999b9d1d9d17 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Sat, 29 Oct 2022 23:33:01 +0400 Subject: [PATCH 1/8] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=84=D0=BE=D1=80=D0=BC=D1=8B=20=D0=B8=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D1=85=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 4 +- src/AbstractMap.java | 155 ++++++++++++++++++++++++++ src/BlockCount.java | 13 +++ src/BlockDirection.java | 5 - src/BlockForm.java | 9 ++ src/Direction.java | 1 + src/DrawingAircraftCarrier.java | 82 ++++++++++++++ src/DrawingBlocks.java | 76 +++++++------ src/DrawingComponents.java | 22 ---- src/DrawingMap.java | 67 ++++++++++++ src/DrawingObjectWarship.java | 37 +++++++ src/DrawingRoundBlocks.java | 43 ++++++++ src/DrawingRoundRectangleBlocks.java | 43 ++++++++ src/DrawingWarship.java | 84 ++++++++++---- src/EntityAircraftCarrier.java | 24 ++++ src/EntityWarship.java | 7 +- src/FormMap.form | 137 +++++++++++++++++++++++ src/FormMap.java | 84 ++++++++++++++ src/FormWarship.form | 157 +++++++++++++++------------ src/FormWarship.java | 72 ++++++++---- src/IDrawingObject.java | 15 +++ src/IDrawingObjectBlock.java | 6 + src/LineMap.java | 53 +++++++++ src/Main.java | 2 +- src/SimpleMap.java | 43 ++++++++ 25 files changed, 1054 insertions(+), 187 deletions(-) create mode 100644 src/AbstractMap.java create mode 100644 src/BlockCount.java delete mode 100644 src/BlockDirection.java create mode 100644 src/BlockForm.java create mode 100644 src/DrawingAircraftCarrier.java delete mode 100644 src/DrawingComponents.java create mode 100644 src/DrawingMap.java create mode 100644 src/DrawingObjectWarship.java create mode 100644 src/DrawingRoundBlocks.java create mode 100644 src/DrawingRoundRectangleBlocks.java create mode 100644 src/EntityAircraftCarrier.java create mode 100644 src/FormMap.form create mode 100644 src/FormMap.java create mode 100644 src/IDrawingObject.java create mode 100644 src/IDrawingObjectBlock.java create mode 100644 src/LineMap.java create mode 100644 src/SimpleMap.java 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..a57cb37 --- /dev/null +++ b/src/AbstractMap.java @@ -0,0 +1,155 @@ + +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) + { + if (true) + { + _drawingObject.MoveObject(direction); + } + float[] cortege = _drawingObject.GetCurrentPosition(); + if (Check(cortege[0],cortege[1],cortege[2],cortege[3])!= 0) + { + _drawingObject.MoveObject(GetOpositDirection(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(); + float nowX = cortege[0]; + float nowY = cortege[1]; + float lenX = cortege[2]-cortege[0]; + float lenY = cortege[3] - cortege[1]; + while (Check(nowX, nowY, nowX + lenX, nowY + lenY) != 2) + { + int result; + do + { + result = Check(nowX, nowY, nowX + lenX, nowY + lenY); + if (result == 0) + { + _drawingObject.SetObject((int)nowX, (int)nowY, _width, _height); + return true; + } + else + { + nowX += _size_x; + } + } while (result != 2); + nowX = x; + nowY += _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.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/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..97659ff --- /dev/null +++ b/src/DrawingAircraftCarrier.java @@ -0,0 +1,82 @@ +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); + } + + @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); + } + } +} 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/DrawingMap.java b/src/DrawingMap.java new file mode 100644 index 0000000..a46780c --- /dev/null +++ b/src/DrawingMap.java @@ -0,0 +1,67 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; + +public class DrawingMap extends JPanel{ + private final FormMap Map; + private AbstractMap _abstractMap; + BufferedImage bufferedImage; + public DrawingMap(FormMap map){ + Map = map; + _abstractMap = new SimpleMap(); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.drawImage(bufferedImage,0,0,null); + } + + private void SetData(DrawingWarship warship) { + Random rnd = new Random(); + warship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), getWidth(),getHeight()); + Map.toolBarLabelSpeed.setText("Color: " + warship.GetWarship().GetSpeed() + " "); + Map.toolBarLabelWieght.setText("Weight: " + warship.GetWarship().GetWeight() + " "); + Map.toolBarLabelColor.setText("Color: " + warship.GetWarship().GetBodyColor().getRed() + " " + + warship.GetWarship().GetBodyColor().getGreen() + " " + warship.GetWarship().GetBodyColor().getBlue()); + bufferedImage = _abstractMap.CreateMap(700,550,new DrawingObjectWarship(warship)); + } + //Создание обычного корабля + public void CreateButtonAction(){ + Random rnd=new Random(); + DrawingWarship 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(warship); + } + //Создание модифицированного корабля + public void CreateModifButtonAction(){ + Random rnd = new Random(); + DrawingWarship 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(warship); + } + + public void DirectionButtonAction(Direction side){ + if(_abstractMap != null){ + bufferedImage = _abstractMap.MoveObject(side); + } + } + + public void ComboBoxSelectorMapAction(String name){ + switch (name){ + case "Простая карта": + _abstractMap = new SimpleMap(); + break; + case "Вторая карта": + _abstractMap = new LineMap(); + break; + } + } + + public void DrawMap(Graphics g){ + g.drawImage(bufferedImage,0,0,null); + } +} diff --git a/src/DrawingObjectWarship.java b/src/DrawingObjectWarship.java new file mode 100644 index 0000000..02d0a4a --- /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 DrawningObject(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..23db1e3 100644 --- a/src/DrawingWarship.java +++ b/src/DrawingWarship.java @@ -1,25 +1,64 @@ 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 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 +71,7 @@ public class DrawingWarship { _pictureHeight = height; } } + public void MoveTransport(Direction direction) { if (_pictureWidth == null || _pictureHeight == null) @@ -70,11 +110,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 +149,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 +168,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/FormMap.form b/src/FormMap.form new file mode 100644 index 0000000..5e59229 --- /dev/null +++ b/src/FormMap.form @@ -0,0 +1,137 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormMap.java b/src/FormMap.java new file mode 100644 index 0000000..c63e07e --- /dev/null +++ b/src/FormMap.java @@ -0,0 +1,84 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Objects; + +public class FormMap extends JFrame{ + DrawingMap map = new DrawingMap(this); + private JPanel mainPanel; + private JToolBar toolBar; + public JLabel toolBarLabelSpeed; + public JLabel toolBarLabelWieght; + public JLabel toolBarLabelColor; + private JButton buttonCreate; + private JButton buttonCreateModif; + private JButton buttonRight; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonLeft; + private JComboBox ComboBoxSelectorMap; + private JPanel drawPanel; + + public FormMap(){ + InitializeComponent(); + } + + private void InitializeComponent() { + setContentPane(mainPanel); + setTitle("Warship"); + setSize(900, 700); + 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); + + buttonCreate.addActionListener(e -> { + map.CreateButtonAction(); + ReDraw(); + }); + + buttonCreateModif.addActionListener(e -> { + map.CreateModifButtonAction(); + ReDraw(); + }); + + buttonUp.addActionListener(e -> { + map.DirectionButtonAction(Direction.Up); + ReDraw(); + }); + + buttonLeft.addActionListener(e -> { + map.DirectionButtonAction(Direction.Left); + ReDraw(); + }); + + buttonRight.addActionListener(e -> { + map.DirectionButtonAction(Direction.Right); + ReDraw(); + }); + + buttonDown.addActionListener(e -> { + map.DirectionButtonAction(Direction.Down); + ReDraw(); + }); + + ComboBoxSelectorMap.addActionListener(e -> { + map.ComboBoxSelectorMapAction(Objects.requireNonNull(ComboBoxSelectorMap.getSelectedItem()).toString()); + ReDraw(); + }); + } + + private void ReDraw() + { + Graphics2D graphics = (Graphics2D) drawPanel.getGraphics(); + graphics.clearRect(0, 0, drawPanel.getWidth(), drawPanel.getHeight()); + drawPanel.paintComponents(graphics); + map.DrawMap(graphics); + } +} diff --git a/src/FormWarship.form b/src/FormWarship.form index 12eb718..bbdcbed 100644 --- a/src/FormWarship.form +++ b/src/FormWarship.form @@ -1,41 +1,101 @@
- + - + - + + - + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -62,51 +122,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/src/FormWarship.java b/src/FormWarship.java index 404303a..987582a 100644 --- a/src/FormWarship.java +++ b/src/FormWarship.java @@ -4,12 +4,13 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; +import java.awt.image.BufferedImage; import java.util.Random; public class FormWarship extends JFrame { private DrawingWarship _warship; - public DrawingComponents drawingComponents; private JPanel mainPanel; + private JPanel drawPanel; private JButton buttonCreate; private JButton buttonLeft; private JButton buttonUp; @@ -19,19 +20,37 @@ public class FormWarship extends JFrame { private JLabel toolBarLabelSpeed; private JLabel toolBarLabelWieght; private JLabel toolBarLabelColor; - private JPanel drawPanel; + private JButton buttonCreateModif; 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,21 +63,28 @@ 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); + _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(new ActionListener() { + @Override + public void actionPerformed(ActionEvent 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(); } }); @@ -66,7 +92,7 @@ public class FormWarship extends JFrame { buttonLeft.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_warship != null) _warship.MoveTransport(Direction.Left); + if (_warship != null) _warship.MoveTransport(Direction.Left); Draw(); } }); @@ -74,7 +100,7 @@ public class FormWarship extends JFrame { buttonRight.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_warship != null) _warship.MoveTransport(Direction.Right); + if (_warship != null) _warship.MoveTransport(Direction.Right); Draw(); } }); @@ -82,7 +108,7 @@ public class FormWarship extends JFrame { buttonUp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_warship != null) _warship.MoveTransport(Direction.Up); + if (_warship != null) _warship.MoveTransport(Direction.Up); Draw(); } }); @@ -90,16 +116,16 @@ public class FormWarship extends JFrame { buttonDown.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_warship != null) _warship.MoveTransport(Direction.Down); + 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/IDrawingObject.java b/src/IDrawingObject.java new file mode 100644 index 0000000..f65592b --- /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 DrawningObject(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..68ad080 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,6 @@ public class Main { public static void main(String[] args) { - new FormWarship(); + new FormMap(); } } 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++; + } + } + } +} -- 2.25.1 From 34e21f25077f2abea89e96e3414acc27224f4bea Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Sun, 30 Oct 2022 19:26:11 +0400 Subject: [PATCH 2/8] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=BD=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AbstractMap.java | 31 +++++++++++++------------------ src/DrawingMap.java | 2 +- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/AbstractMap.java b/src/AbstractMap.java index a57cb37..b494ef9 100644 --- a/src/AbstractMap.java +++ b/src/AbstractMap.java @@ -29,18 +29,16 @@ public abstract class AbstractMap { public BufferedImage MoveObject(Direction direction) { - if (_drawingObject != null) - { - if (true) - { - _drawingObject.MoveObject(direction); - } + if(_drawingObject!=null){ float[] cortege = _drawingObject.GetCurrentPosition(); - if (Check(cortege[0],cortege[1],cortege[2],cortege[3])!= 0) + if (Check(cortege[0],cortege[1],cortege[2],cortege[3]) != 0) { _drawingObject.MoveObject(GetOpositDirection(direction)); } - return DrawMapWithObject(); + if(true){ + _drawingObject.MoveObject(direction); + } + return DrawMapWithObject(); } return null; } @@ -73,28 +71,25 @@ public abstract class AbstractMap { int y = _random.nextInt(0, 10); _drawingObject.SetObject(x, y, _width, _height); float[] cortege = _drawingObject.GetCurrentPosition(); - float nowX = cortege[0]; - float nowY = cortege[1]; - float lenX = cortege[2]-cortege[0]; - float lenY = cortege[3] - cortege[1]; - while (Check(nowX, nowY, nowX + lenX, nowY + lenY) != 2) + + while (Check(cortege[0], cortege[1], cortege[2], cortege[3]) != 2) { int result; do { - result = Check(nowX, nowY, nowX + lenX, nowY + lenY); + result = Check(cortege[0], cortege[1], cortege[2], cortege[3]); if (result == 0) { - _drawingObject.SetObject((int)nowX, (int)nowY, _width, _height); + _drawingObject.SetObject((int)cortege[0], (int)cortege[1], _width, _height); return true; } else { - nowX += _size_x; + cortege[0] += _size_x; } } while (result != 2); - nowX = x; - nowY += _size_y; + cortege[0] = x; + cortege[1] += _size_y; } return false; } diff --git a/src/DrawingMap.java b/src/DrawingMap.java index a46780c..2cec1b0 100644 --- a/src/DrawingMap.java +++ b/src/DrawingMap.java @@ -25,7 +25,7 @@ public class DrawingMap extends JPanel{ Map.toolBarLabelWieght.setText("Weight: " + warship.GetWarship().GetWeight() + " "); Map.toolBarLabelColor.setText("Color: " + warship.GetWarship().GetBodyColor().getRed() + " " + warship.GetWarship().GetBodyColor().getGreen() + " " + warship.GetWarship().GetBodyColor().getBlue()); - bufferedImage = _abstractMap.CreateMap(700,550,new DrawingObjectWarship(warship)); + bufferedImage = _abstractMap.CreateMap(900,550,new DrawingObjectWarship(warship)); } //Создание обычного корабля public void CreateButtonAction(){ -- 2.25.1 From 4c43d95862e9152dd16d1fbc29ff4d64458bae05 Mon Sep 17 00:00:00 2001 From: Inohara Date: Tue, 1 Nov 2022 20:06:28 +0400 Subject: [PATCH 3/8] =?UTF-8?q?=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=BD=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/DrawingMap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DrawingMap.java b/src/DrawingMap.java index 2cec1b0..005bd6e 100644 --- a/src/DrawingMap.java +++ b/src/DrawingMap.java @@ -25,7 +25,7 @@ public class DrawingMap extends JPanel{ Map.toolBarLabelWieght.setText("Weight: " + warship.GetWarship().GetWeight() + " "); Map.toolBarLabelColor.setText("Color: " + warship.GetWarship().GetBodyColor().getRed() + " " + warship.GetWarship().GetBodyColor().getGreen() + " " + warship.GetWarship().GetBodyColor().getBlue()); - bufferedImage = _abstractMap.CreateMap(900,550,new DrawingObjectWarship(warship)); + bufferedImage = _abstractMap.CreateMap(750,550,new DrawingObjectWarship(warship)); } //Создание обычного корабля public void CreateButtonAction(){ -- 2.25.1 From 2a90e0d4b01bdb5f027f5829dcf444585e28319d Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Tue, 1 Nov 2022 20:50:11 +0400 Subject: [PATCH 4/8] Generic classes --- src/AbstractMap.java | 2 +- src/DrawingObjectWarship.java | 2 +- src/IDrawingObject.java | 2 +- src/MapWithSetWarshipsGeneric.java | 126 +++++++++++++++++++++++++++++ src/SetWarshipsGeneric.java | 58 +++++++++++++ 5 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 src/MapWithSetWarshipsGeneric.java create mode 100644 src/SetWarshipsGeneric.java diff --git a/src/AbstractMap.java b/src/AbstractMap.java index b494ef9..0677956 100644 --- a/src/AbstractMap.java +++ b/src/AbstractMap.java @@ -139,7 +139,7 @@ public abstract class AbstractMap { } } } - _drawingObject.DrawningObject(gr); + _drawingObject.DrawingObject(gr); return bmp; } diff --git a/src/DrawingObjectWarship.java b/src/DrawingObjectWarship.java index 02d0a4a..3fa8b93 100644 --- a/src/DrawingObjectWarship.java +++ b/src/DrawingObjectWarship.java @@ -25,7 +25,7 @@ public class DrawingObjectWarship implements IDrawingObject { } @Override - public void DrawningObject(Graphics g) { + public void DrawingObject(Graphics g) { _warship.DrawTransport(g); } diff --git a/src/IDrawingObject.java b/src/IDrawingObject.java index f65592b..e32a8d8 100644 --- a/src/IDrawingObject.java +++ b/src/IDrawingObject.java @@ -8,7 +8,7 @@ public interface IDrawingObject { // Изменение направления пермещения объекта void MoveObject(Direction direction); // Отрисовка объекта - void DrawningObject(Graphics g); + void DrawingObject(Graphics g); // Получение текущей позиции объекта // /Left, Right, Top, Bottom) float[] GetCurrentPosition(); diff --git a/src/MapWithSetWarshipsGeneric.java b/src/MapWithSetWarshipsGeneric.java new file mode 100644 index 0000000..b4f55a0 --- /dev/null +++ b/src/MapWithSetWarshipsGeneric.java @@ -0,0 +1,126 @@ +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 add(T warship) + { + return _setWarships.Insert(warship); + } + + public T remove(int position) + { + return _setWarships.Remove(position); + } + + public Image ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureWidth,BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + DrawBackground(gr); + DrawWarship(gr); + return bmp; + } + + public Image ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setWarships.getCount(); i++) + { + var warship = _setWarships.Get(i); + if (warship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, warship); + } + } + return new BufferedImage(_pictureWidth, _pictureHeight,BufferedImage.TYPE_INT_RGB); + } + + public Image MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new BufferedImage(_pictureWidth, _pictureHeight,BufferedImage.TYPE_INT_RGB); + } + + 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(5); + Stroke penThin = new BasicStroke(1); + + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++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 + 20, j * _placeSizeHeight + _placeSizeHeight/2+2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight/2+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); + g.setStroke(penThin); + } + } + } + + 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, _pictureWidth, _pictureHeight); + _setWarships.Get(i).DrawingObject(gr); + } + } + } +} diff --git a/src/SetWarshipsGeneric.java b/src/SetWarshipsGeneric.java new file mode 100644 index 0000000..e217ade --- /dev/null +++ b/src/SetWarshipsGeneric.java @@ -0,0 +1,58 @@ +public class SetWarshipsGeneric{ + private T[] _places; + + public int getCount() { + return _places != null ? _places.length : 0; + } + + public SetWarshipsGeneric(int count) + { + _places = (T[]) new Object[count]; + } + + public int Insert(T warship) + { + return Insert(warship,0); + } + + public int Insert(T warship, int position) + { + if (position < 0 || position >= getCount() || _places[position] == null) + 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 =_places[position]; + _places[position] = null; + return deleted; + } + + public T Get(int position) + { + if (position >= getCount() || position < 0) + return null; + + return _places[position]; + } +} -- 2.25.1 From 0391fff9dc3e3ed3cd61ba5cc8c33a67b88a0d5c Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Tue, 1 Nov 2022 22:47:54 +0400 Subject: [PATCH 5/8] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D1=85=20=D1=84=D0=BE=D1=80=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FormMapWithSetWarships.form | 158 +++++++++++++++++++++ src/FormMapWithSetWarships.java | 155 ++++++++++++++++++++ src/FormWarship.form | 18 ++- src/FormWarship.java | 17 ++- src/FormWarshipCreator.form | 242 ++++++++++++++++++++++++++++++++ src/FormWarshipCreator.java | 30 ++++ src/Main.java | 2 +- src/WarshipCreatorGeneric.java | 41 ++++++ 8 files changed, 656 insertions(+), 7 deletions(-) create mode 100644 src/FormMapWithSetWarships.form create mode 100644 src/FormMapWithSetWarships.java create mode 100644 src/FormWarshipCreator.form create mode 100644 src/FormWarshipCreator.java create mode 100644 src/WarshipCreatorGeneric.java diff --git a/src/FormMapWithSetWarships.form b/src/FormMapWithSetWarships.form new file mode 100644 index 0000000..d85ed02 --- /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..5b08851 --- /dev/null +++ b/src/FormMapWithSetWarships.java @@ -0,0 +1,155 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +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(); + + + } + + public void InitializeComponent(){ + setContentPane(mainPanel); + setTitle("Warship"); + setSize(1000, 685); + 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(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + AbstractMap map = null; + switch ((String) СomboBoxSelectorMap.getSelectedItem()){ + case "Простая карта": + map = new SimpleMap(); + break; + case "Преграды-линии": + map = new LineMap(); + break; + } + if( map != null){ + _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric( + PictureBox.getWidth(), PictureBox.getHeight(), map); + } + else + { + _mapWarshipsCollectionGeneric = null; + } + } + }); + + ButtonAddWarship.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_mapWarshipsCollectionGeneric == null){ + return; + } + FormWarship form = new FormWarship(); + form.setSize(1200,700); + form.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + form.setVisible(true); + form.addWindowListener(new WindowAdapter() { + @Override + public void windowDeactivated(WindowEvent e) { + DrawingObjectWarship warship = new DrawingObjectWarship(form.getSelectedCar()); + if (_mapWarshipsCollectionGeneric.add(warship) > 0) + { + if (form.DialogResult) + JOptionPane.showMessageDialog(null, "Объект добавлен"); + bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + repaint(); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + } + }); + } + }); + + ButtonRemoveWarship.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent 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.remove(pos) != null) + { + JOptionPane.showMessageDialog(null,"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + repaint(); + } + else + { + JOptionPane.showMessageDialog(null,"Не удалось удалить объект","Ошибка",JOptionPane.INFORMATION_MESSAGE); + } + } + }); + + ButtonShowStorage.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_mapWarshipsCollectionGeneric == null) + { + return; + } + bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + repaint(); + } + }); + + ButtonShowOnMap.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + bufferedImage = _mapWarshipsCollectionGeneric.ShowOnMap(); + repaint(); + } + }); + } +} diff --git a/src/FormWarship.form b/src/FormWarship.form index bbdcbed..257dce6 100644 --- a/src/FormWarship.form +++ b/src/FormWarship.form @@ -8,7 +8,7 @@ - + @@ -44,7 +44,7 @@ - + @@ -56,7 +56,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -81,7 +81,7 @@ - + @@ -91,6 +91,14 @@ + + + + + + + + diff --git a/src/FormWarship.java b/src/FormWarship.java index 987582a..c690411 100644 --- a/src/FormWarship.java +++ b/src/FormWarship.java @@ -4,11 +4,15 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; -import java.awt.image.BufferedImage; import java.util.Random; public class FormWarship extends JFrame { private DrawingWarship _warship; + public DrawingWarship selectedWarship; + public DrawingWarship getSelectedCar(){ + return selectedWarship; + } + public boolean DialogResult = false; private JPanel mainPanel; private JPanel drawPanel; private JButton buttonCreate; @@ -21,6 +25,7 @@ public class FormWarship extends JFrame { private JLabel toolBarLabelWieght; private JLabel toolBarLabelColor; private JButton buttonCreateModif; + private JButton buttonSelect; public FormWarship(){ InitializeComponent(); @@ -89,6 +94,16 @@ public class FormWarship extends JFrame { } }); + buttonSelect.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_warship != null){ + selectedWarship =_warship; + DialogResult = true; + } + } + }); + buttonLeft.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { diff --git a/src/FormWarshipCreator.form b/src/FormWarshipCreator.form new file mode 100644 index 0000000..be70362 --- /dev/null +++ b/src/FormWarshipCreator.form @@ -0,0 +1,242 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormWarshipCreator.java b/src/FormWarshipCreator.java new file mode 100644 index 0000000..31f1f1b --- /dev/null +++ b/src/FormWarshipCreator.java @@ -0,0 +1,30 @@ +import javax.swing.*; + +public class FormWarshipCreator extends JDialog{ + private JPanel PictureBox; + private JPanel PropertiesPanel; + private JRadioButton BasicRadioButton; + private JRadioButton AdvancedRadioButton; + private JTextField WeightTextField; + private JTextField SpeedTextField; + private JLabel SetWeightLabel; + private JLabel SetSpeedLabel; + private JPanel CargoPanel; + private JRadioButton TriangleFormRadioButton; + private JRadioButton RoundFormRadioButton; + private JRadioButton RectangleFormRadioButton; + private JPanel CountOfBlocksPanel; + private JRadioButton SixRadioButton; + private JRadioButton FourRadioButton; + private JRadioButton TwoRadioButton; + private JButton ShowWarshipButton; + private JButton ChooseButton; + private JCheckBox MissileCheckBox; + private JCheckBox AntennaCheckBox; + private JCheckBox HelipadCheckBox; + BlockCount block=null; + IDrawingObjectBlock fblock=null; + private final CreaterGeneric createrGeneric=new CreaterGeneric<>(40,40); + private DrawingWarship _warship; + private DrawingWarship selectedWarship; +} diff --git a/src/Main.java b/src/Main.java index 68ad080..c97b9f2 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,6 @@ public class Main { public static void main(String[] args) { - new FormMap(); + new FormMapWithSetWarships(); } } diff --git a/src/WarshipCreatorGeneric.java b/src/WarshipCreatorGeneric.java new file mode 100644 index 0000000..8e79140 --- /dev/null +++ b/src/WarshipCreatorGeneric.java @@ -0,0 +1,41 @@ +import java.util.Random; + +public class WarshipCreatorGeneric { + T[] Warships; + U[] Blocks; + int WarshipsCount = 0; + int BlocksCount = 0; + public WarshipCreatorGeneric(int warshipsCount, int blocksCount){ + Warships = (T[])new Object[warshipsCount]; + Blocks = (U[]) new Object[blocksCount]; + WarshipsCount = warshipsCount; + BlocksCount = blocksCount; + } + + public int AddWarship(T warship){ + if(WarshipsCount Date: Thu, 3 Nov 2022 18:06:25 +0400 Subject: [PATCH 6/8] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B2=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=85,?= =?UTF-8?q?=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=B4=D0=BE=D0=BF.=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/DrawingAircraftCarrier.java | 7 +- src/DrawingMap.java | 67 ------------- src/DrawingWarship.java | 5 + src/FormMap.form | 137 -------------------------- src/FormMap.java | 84 ---------------- src/FormMapWithSetWarships.form | 4 +- src/FormMapWithSetWarships.java | 165 ++++++++++++++++++-------------- src/FormWarship.java | 87 +++++++---------- src/FormWarshipCreator.form | 64 +++---------- src/FormWarshipCreator.java | 109 ++++++++++++++++++++- src/Main.java | 1 - src/WarshipCreatorGeneric.java | 33 ++++--- 12 files changed, 269 insertions(+), 494 deletions(-) delete mode 100644 src/DrawingMap.java delete mode 100644 src/FormMap.form delete mode 100644 src/FormMap.java diff --git a/src/DrawingAircraftCarrier.java b/src/DrawingAircraftCarrier.java index 97659ff..3a501f7 100644 --- a/src/DrawingAircraftCarrier.java +++ b/src/DrawingAircraftCarrier.java @@ -8,6 +8,11 @@ public class DrawingAircraftCarrier extends DrawingWarship{ 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)) @@ -79,4 +84,4 @@ public class DrawingAircraftCarrier extends DrawingWarship{ g2.drawRect(_startPosX + 85, _startPosY + 13, 10, 14); } } -} +} \ No newline at end of file diff --git a/src/DrawingMap.java b/src/DrawingMap.java deleted file mode 100644 index 005bd6e..0000000 --- a/src/DrawingMap.java +++ /dev/null @@ -1,67 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.util.Random; - -public class DrawingMap extends JPanel{ - private final FormMap Map; - private AbstractMap _abstractMap; - BufferedImage bufferedImage; - public DrawingMap(FormMap map){ - Map = map; - _abstractMap = new SimpleMap(); - } - - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - g.drawImage(bufferedImage,0,0,null); - } - - private void SetData(DrawingWarship warship) { - Random rnd = new Random(); - warship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), getWidth(),getHeight()); - Map.toolBarLabelSpeed.setText("Color: " + warship.GetWarship().GetSpeed() + " "); - Map.toolBarLabelWieght.setText("Weight: " + warship.GetWarship().GetWeight() + " "); - Map.toolBarLabelColor.setText("Color: " + warship.GetWarship().GetBodyColor().getRed() + " " + - warship.GetWarship().GetBodyColor().getGreen() + " " + warship.GetWarship().GetBodyColor().getBlue()); - bufferedImage = _abstractMap.CreateMap(750,550,new DrawingObjectWarship(warship)); - } - //Создание обычного корабля - public void CreateButtonAction(){ - Random rnd=new Random(); - DrawingWarship 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(warship); - } - //Создание модифицированного корабля - public void CreateModifButtonAction(){ - Random rnd = new Random(); - DrawingWarship 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(warship); - } - - public void DirectionButtonAction(Direction side){ - if(_abstractMap != null){ - bufferedImage = _abstractMap.MoveObject(side); - } - } - - public void ComboBoxSelectorMapAction(String name){ - switch (name){ - case "Простая карта": - _abstractMap = new SimpleMap(); - break; - case "Вторая карта": - _abstractMap = new LineMap(); - break; - } - } - - public void DrawMap(Graphics g){ - g.drawImage(bufferedImage,0,0,null); - } -} diff --git a/src/DrawingWarship.java b/src/DrawingWarship.java index 23db1e3..b54df4c 100644 --- a/src/DrawingWarship.java +++ b/src/DrawingWarship.java @@ -31,6 +31,11 @@ public class DrawingWarship { _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); diff --git a/src/FormMap.form b/src/FormMap.form deleted file mode 100644 index 5e59229..0000000 --- a/src/FormMap.form +++ /dev/null @@ -1,137 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/src/FormMap.java b/src/FormMap.java deleted file mode 100644 index c63e07e..0000000 --- a/src/FormMap.java +++ /dev/null @@ -1,84 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.util.Objects; - -public class FormMap extends JFrame{ - DrawingMap map = new DrawingMap(this); - private JPanel mainPanel; - private JToolBar toolBar; - public JLabel toolBarLabelSpeed; - public JLabel toolBarLabelWieght; - public JLabel toolBarLabelColor; - private JButton buttonCreate; - private JButton buttonCreateModif; - private JButton buttonRight; - private JButton buttonUp; - private JButton buttonDown; - private JButton buttonLeft; - private JComboBox ComboBoxSelectorMap; - private JPanel drawPanel; - - public FormMap(){ - InitializeComponent(); - } - - private void InitializeComponent() { - setContentPane(mainPanel); - setTitle("Warship"); - setSize(900, 700); - 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); - - buttonCreate.addActionListener(e -> { - map.CreateButtonAction(); - ReDraw(); - }); - - buttonCreateModif.addActionListener(e -> { - map.CreateModifButtonAction(); - ReDraw(); - }); - - buttonUp.addActionListener(e -> { - map.DirectionButtonAction(Direction.Up); - ReDraw(); - }); - - buttonLeft.addActionListener(e -> { - map.DirectionButtonAction(Direction.Left); - ReDraw(); - }); - - buttonRight.addActionListener(e -> { - map.DirectionButtonAction(Direction.Right); - ReDraw(); - }); - - buttonDown.addActionListener(e -> { - map.DirectionButtonAction(Direction.Down); - ReDraw(); - }); - - ComboBoxSelectorMap.addActionListener(e -> { - map.ComboBoxSelectorMapAction(Objects.requireNonNull(ComboBoxSelectorMap.getSelectedItem()).toString()); - ReDraw(); - }); - } - - private void ReDraw() - { - Graphics2D graphics = (Graphics2D) drawPanel.getGraphics(); - graphics.clearRect(0, 0, drawPanel.getWidth(), drawPanel.getHeight()); - drawPanel.paintComponents(graphics); - map.DrawMap(graphics); - } -} diff --git a/src/FormMapWithSetWarships.form b/src/FormMapWithSetWarships.form index d85ed02..26b6e7e 100644 --- a/src/FormMapWithSetWarships.form +++ b/src/FormMapWithSetWarships.form @@ -43,8 +43,8 @@ - - + + diff --git a/src/FormMapWithSetWarships.java b/src/FormMapWithSetWarships.java index 5b08851..f5ca3d8 100644 --- a/src/FormMapWithSetWarships.java +++ b/src/FormMapWithSetWarships.java @@ -2,8 +2,6 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; public class FormMapWithSetWarships extends JFrame{ private JPanel mainPanel; @@ -24,8 +22,15 @@ public class FormMapWithSetWarships extends JFrame{ 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(){ @@ -48,7 +53,7 @@ public class FormMapWithSetWarships extends JFrame{ @Override public void actionPerformed(ActionEvent e) { AbstractMap map = null; - switch ((String) СomboBoxSelectorMap.getSelectedItem()){ + switch (СomboBoxSelectorMap.getSelectedItem().toString()){ case "Простая карта": map = new SimpleMap(); break; @@ -67,87 +72,103 @@ public class FormMapWithSetWarships extends JFrame{ } }); - ButtonAddWarship.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(_mapWarshipsCollectionGeneric == null){ - return; - } - FormWarship form = new FormWarship(); - form.setSize(1200,700); - form.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - form.setVisible(true); - form.addWindowListener(new WindowAdapter() { - @Override - public void windowDeactivated(WindowEvent e) { - DrawingObjectWarship warship = new DrawingObjectWarship(form.getSelectedCar()); - if (_mapWarshipsCollectionGeneric.add(warship) > 0) - { - if (form.DialogResult) - JOptionPane.showMessageDialog(null, "Объект добавлен"); - bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); - repaint(); - } - else - { - JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); - } - } - }); + ButtonAddWarship.addActionListener(e -> { + if(_mapWarshipsCollectionGeneric == null){ + System.out.println("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()); - ButtonRemoveWarship.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent 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.remove(pos) != null) - { - JOptionPane.showMessageDialog(null,"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE); + if (_mapWarshipsCollectionGeneric.add(warship) >= 0) { + JOptionPane.showMessageDialog(this, + "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); repaint(); - } - else - { - JOptionPane.showMessageDialog(null,"Не удалось удалить объект","Ошибка",JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(this, + "Не удалось добавить объект", "Ошибка",JOptionPane.INFORMATION_MESSAGE); } } }); - ButtonShowStorage.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(_mapWarshipsCollectionGeneric == null) - { - return; - } + 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.remove(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(); + } }); - ButtonShowOnMap.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if (_mapWarshipsCollectionGeneric == null) - { - return; - } - bufferedImage = _mapWarshipsCollectionGeneric.ShowOnMap(); + 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.java b/src/FormWarship.java index c690411..cc451ec 100644 --- a/src/FormWarship.java +++ b/src/FormWarship.java @@ -1,7 +1,5 @@ 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; @@ -69,71 +67,50 @@ public class FormWarship extends JFrame { buttonRight.setIcon(iconRight); //кнопка добавления объекта - buttonCreate.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent 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(); - } + 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(new ActionListener() { - @Override - public void actionPerformed(ActionEvent 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(); + 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; } }); - buttonSelect.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(_warship != null){ - selectedWarship =_warship; - DialogResult = true; - } - } + buttonLeft.addActionListener(e -> { + if (_warship != null) _warship.MoveTransport(Direction.Left); + Draw(); }); - buttonLeft.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if (_warship != null) _warship.MoveTransport(Direction.Left); - Draw(); - } + buttonRight.addActionListener(e -> { + if (_warship != null) _warship.MoveTransport(Direction.Right); + Draw(); }); - buttonRight.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if (_warship != null) _warship.MoveTransport(Direction.Right); - Draw(); - } + buttonUp.addActionListener(e -> { + if (_warship != null) _warship.MoveTransport(Direction.Up); + Draw(); }); - buttonUp.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent 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(); }); mainPanel.addComponentListener(new ComponentAdapter() { diff --git a/src/FormWarshipCreator.form b/src/FormWarshipCreator.form index be70362..aa52652 100644 --- a/src/FormWarshipCreator.form +++ b/src/FormWarshipCreator.form @@ -8,7 +8,7 @@ - + @@ -20,13 +20,13 @@ - + - + @@ -49,52 +49,10 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -129,7 +87,7 @@ - + @@ -163,7 +121,7 @@ - + @@ -190,16 +148,16 @@ - + - + - + @@ -224,8 +182,8 @@ - diff --git a/src/FormWarshipCreator.java b/src/FormWarshipCreator.java index 31f1f1b..f08b869 100644 --- a/src/FormWarshipCreator.java +++ b/src/FormWarshipCreator.java @@ -1,4 +1,6 @@ import javax.swing.*; +import java.awt.*; +import java.util.Random; public class FormWarshipCreator extends JDialog{ private JPanel PictureBox; @@ -7,8 +9,6 @@ public class FormWarshipCreator extends JDialog{ private JRadioButton AdvancedRadioButton; private JTextField WeightTextField; private JTextField SpeedTextField; - private JLabel SetWeightLabel; - private JLabel SetSpeedLabel; private JPanel CargoPanel; private JRadioButton TriangleFormRadioButton; private JRadioButton RoundFormRadioButton; @@ -22,9 +22,108 @@ public class FormWarshipCreator extends JDialog{ private JCheckBox MissileCheckBox; private JCheckBox AntennaCheckBox; private JCheckBox HelipadCheckBox; - BlockCount block=null; - IDrawingObjectBlock fblock=null; - private final CreaterGeneric createrGeneric=new CreaterGeneric<>(40,40); + BlockCount _block = null; + IDrawingObjectBlock objectBlock = null; + private final WarshipCreatorGeneric createrGeneric=new WarshipCreatorGeneric<>(40,40); private DrawingWarship _warship; private DrawingWarship selectedWarship; + + 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(900, 700); + + 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); + if(objectBlock ==null) + return; + objectBlock.SetBlockCount(_block.GetBlockCount()); + createrGeneric.AddBlock(objectBlock); + }); + + RoundFormRadioButton.addActionListener(e -> { + if(_block ==null){ + return; + } + objectBlock =new DrawingRoundBlocks(_block); + if(objectBlock ==null) + return; + objectBlock.SetBlockCount(_block.GetBlockCount()); + createrGeneric.AddBlock(objectBlock); + }); + + TriangleFormRadioButton.addActionListener(e -> { + if(_block ==null){ + return; + } + objectBlock =new DrawingRoundBlocks(_block); + if(objectBlock ==null) + return; + objectBlock.SetBlockCount(_block.GetBlockCount()); + createrGeneric.AddBlock(objectBlock); + }); + + BasicRadioButton.addActionListener(e -> { + Color color=JColorChooser.showDialog(this,"Выберите цвет корпуса корабля",Color.WHITE); + if(Integer.parseInt(SpeedTextField.getText())==0 || Integer.parseInt(WeightTextField.getText())==0 || color==null){ + return; + } + createrGeneric.AddWarship(new EntityWarship(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),color)); + }); + + AdvancedRadioButton.addActionListener(e -> { + Color color1=JColorChooser.showDialog(this,"Выберите цвет корпуса корабля",Color.WHITE); + if(Integer.parseInt(SpeedTextField.getText())==0 || Integer.parseInt(WeightTextField.getText())==0 || color1==null){ + return; + } + Color color2=JColorChooser.showDialog(this,"Выберите цвет модификаций корабля",Color.WHITE); + if(color2==null){ + return; + } + createrGeneric.AddWarship(new EntityAircraftCarrier(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()), + color1,color2,HelipadCheckBox.isSelected(),AntennaCheckBox.isSelected(),MissileCheckBox.isSelected())); + }); + + ShowWarshipButton.addActionListener(e -> { + Random rand=new Random(); + _warship=createrGeneric.NewWarshipCreating(); + _warship.SetPosition(rand.nextInt(100),rand.nextInt(100),getWidth(),getHeight()); + repaint(); + }); + ChooseButton.addActionListener(e -> { + selectedWarship=_warship; + dispose(); + }); + } } diff --git a/src/Main.java b/src/Main.java index c97b9f2..17772ee 100644 --- a/src/Main.java +++ b/src/Main.java @@ -2,5 +2,4 @@ public class Main { public static void main(String[] args) { new FormMapWithSetWarships(); } - } diff --git a/src/WarshipCreatorGeneric.java b/src/WarshipCreatorGeneric.java index 8e79140..a4495ea 100644 --- a/src/WarshipCreatorGeneric.java +++ b/src/WarshipCreatorGeneric.java @@ -1,40 +1,39 @@ import java.util.Random; public class WarshipCreatorGeneric { - T[] Warships; - U[] Blocks; - int WarshipsCount = 0; - int BlocksCount = 0; + private final EntityWarship[] Warships; + private final IDrawingObjectBlock[] Blocks; + private int WarshipsCount = 0; + private int BlocksCount = 0; public WarshipCreatorGeneric(int warshipsCount, int blocksCount){ - Warships = (T[])new Object[warshipsCount]; - Blocks = (U[]) new Object[blocksCount]; - WarshipsCount = warshipsCount; - BlocksCount = blocksCount; + Warships = new EntityWarship[warshipsCount]; + Blocks = new IDrawingObjectBlock[blocksCount]; } public int AddWarship(T warship){ - if(WarshipsCount Date: Tue, 15 Nov 2022 17:46:54 +0400 Subject: [PATCH 7/8] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B2=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=85=20?= =?UTF-8?q?=D0=B8=20=D0=B4=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D0=BE=D0=BC=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FormMapWithSetWarships.java | 42 +++----- src/FormWarshipCreator.form | 155 +++++++++++++++-------------- src/FormWarshipCreator.java | 138 +++++++++++++------------ src/MapWithSetWarshipsGeneric.java | 24 ++--- src/SetWarshipsGeneric.java | 14 +-- src/WarshipCreatorGeneric.java | 23 +++-- 6 files changed, 202 insertions(+), 194 deletions(-) diff --git a/src/FormMapWithSetWarships.java b/src/FormMapWithSetWarships.java index f5ca3d8..3f18958 100644 --- a/src/FormMapWithSetWarships.java +++ b/src/FormMapWithSetWarships.java @@ -1,7 +1,5 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; public class FormMapWithSetWarships extends JFrame{ private JPanel mainPanel; @@ -36,7 +34,7 @@ public class FormMapWithSetWarships extends JFrame{ public void InitializeComponent(){ setContentPane(mainPanel); setTitle("Warship"); - setSize(1000, 685); + setSize(1000, 693); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); @@ -49,32 +47,24 @@ public class FormMapWithSetWarships extends JFrame{ Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg"); ButtonRight.setIcon(iconRight); - СomboBoxSelectorMap.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - AbstractMap map = null; - switch (СomboBoxSelectorMap.getSelectedItem().toString()){ - case "Простая карта": - map = new SimpleMap(); - break; - case "Преграды-линии": - map = new LineMap(); - break; - } - if( map != null){ - _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric( - PictureBox.getWidth(), PictureBox.getHeight(), map); - } - else - { - _mapWarshipsCollectionGeneric = null; - } + С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){ - System.out.println("null"); return; } FormWarshipCreator warshipCreator = new FormWarshipCreator(); @@ -85,7 +75,7 @@ public class FormMapWithSetWarships extends JFrame{ if (warshipCreator.getSelectedWarship() != null) { DrawingObjectWarship warship = new DrawingObjectWarship(warshipCreator.getSelectedWarship()); - if (_mapWarshipsCollectionGeneric.add(warship) >= 0) { + if (_mapWarshipsCollectionGeneric.Plus(warship) >= 0) { JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); @@ -113,7 +103,7 @@ public class FormMapWithSetWarships extends JFrame{ return; } int pos = Integer.parseInt(text); - if (_mapWarshipsCollectionGeneric.remove(pos) != null) + if (_mapWarshipsCollectionGeneric.Minus(pos) != null) { JOptionPane.showMessageDialog(this, "Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE); diff --git a/src/FormWarshipCreator.form b/src/FormWarshipCreator.form index aa52652..bcd4f74 100644 --- a/src/FormWarshipCreator.form +++ b/src/FormWarshipCreator.form @@ -8,7 +8,7 @@ - + @@ -16,53 +16,22 @@ - + - - - - - - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + @@ -70,8 +39,8 @@ - @@ -79,18 +48,51 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + @@ -118,72 +120,75 @@ - + + + + + + + + + + + + + + + + - + - + - + - + - - - - - - - - - - - - - - + - + - + - - + - - + - diff --git a/src/FormWarshipCreator.java b/src/FormWarshipCreator.java index f08b869..880517b 100644 --- a/src/FormWarshipCreator.java +++ b/src/FormWarshipCreator.java @@ -4,29 +4,30 @@ import java.util.Random; public class FormWarshipCreator extends JDialog{ private JPanel PictureBox; - private JPanel PropertiesPanel; - private JRadioButton BasicRadioButton; - private JRadioButton AdvancedRadioButton; - private JTextField WeightTextField; - private JTextField SpeedTextField; - private JPanel CargoPanel; - private JRadioButton TriangleFormRadioButton; + 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 JPanel CountOfBlocksPanel; private JRadioButton SixRadioButton; private JRadioButton FourRadioButton; private JRadioButton TwoRadioButton; - private JButton ShowWarshipButton; - private JButton ChooseButton; - private JCheckBox MissileCheckBox; - private JCheckBox AntennaCheckBox; - private JCheckBox HelipadCheckBox; - BlockCount _block = null; - IDrawingObjectBlock objectBlock = null; - private final WarshipCreatorGeneric createrGeneric=new WarshipCreatorGeneric<>(40,40); + 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(); @@ -49,80 +50,91 @@ public class FormWarshipCreator extends JDialog{ setContentPane(PictureBox); setTitle("Warship"); setResizable(false); - setSize(900, 700); + setSize(1000, 600); + //Определение типа объекта + comboBoxType.addItemListener(e -> isModified = e.getItem().toString().equals("AircraftCarrier")); + + //количество блоков TwoRadioButton.addActionListener(e -> { - _block =BlockCount.TwoBlocks; + _block = BlockCount.TwoBlocks; }); FourRadioButton.addActionListener(e -> { - _block =BlockCount.FourBlocks; + _block = BlockCount.FourBlocks; }); SixRadioButton.addActionListener(e -> { - _block =BlockCount.SixBlocks; + _block = BlockCount.SixBlocks; }); + //Квадратная форма блоков RectangleFormRadioButton.addActionListener(e -> { - if(_block ==null){ - return; - } - objectBlock =new DrawingBlocks(_block); - if(objectBlock ==null) - return; + if(_block == null) return; + + objectBlock = new DrawingBlocks(_block); objectBlock.SetBlockCount(_block.GetBlockCount()); - createrGeneric.AddBlock(objectBlock); + warshipCreator.AddBlock(objectBlock); }); + //Круглая форма блоков RoundFormRadioButton.addActionListener(e -> { if(_block ==null){ return; } - objectBlock =new DrawingRoundBlocks(_block); - if(objectBlock ==null) - return; + objectBlock = new DrawingRoundBlocks(_block); objectBlock.SetBlockCount(_block.GetBlockCount()); - createrGeneric.AddBlock(objectBlock); + warshipCreator.AddBlock(objectBlock); }); - TriangleFormRadioButton.addActionListener(e -> { - if(_block ==null){ + //Закруглено-квадратная форма блоков + RoundRectangleFormRadioButton.addActionListener(e -> { + if(_block == null){ return; } - objectBlock =new DrawingRoundBlocks(_block); - if(objectBlock ==null) - return; + objectBlock = new DrawingRoundBlocks(_block); objectBlock.SetBlockCount(_block.GetBlockCount()); - createrGeneric.AddBlock(objectBlock); + warshipCreator.AddBlock(objectBlock); }); - BasicRadioButton.addActionListener(e -> { - Color color=JColorChooser.showDialog(this,"Выберите цвет корпуса корабля",Color.WHITE); - if(Integer.parseInt(SpeedTextField.getText())==0 || Integer.parseInt(WeightTextField.getText())==0 || color==null){ - return; - } - createrGeneric.AddWarship(new EntityWarship(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),color)); - }); + CreateButton.addActionListener(e -> { + Random rnd = new Random(); + if (_block != null && objectBlock != null) { + Color color = JColorChooser.showDialog(this, "Choose color", Color.WHITE); - AdvancedRadioButton.addActionListener(e -> { - Color color1=JColorChooser.showDialog(this,"Выберите цвет корпуса корабля",Color.WHITE); - if(Integer.parseInt(SpeedTextField.getText())==0 || Integer.parseInt(WeightTextField.getText())==0 || color1==null){ - return; - } - Color color2=JColorChooser.showDialog(this,"Выберите цвет модификаций корабля",Color.WHITE); - if(color2==null){ - return; - } - createrGeneric.AddWarship(new EntityAircraftCarrier(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()), - color1,color2,HelipadCheckBox.isSelected(),AntennaCheckBox.isSelected(),MissileCheckBox.isSelected())); - }); + if (isModified) { + Color additionalColor = JColorChooser.showDialog(this, "Choose color", Color.GRAY); - ShowWarshipButton.addActionListener(e -> { - Random rand=new Random(); - _warship=createrGeneric.NewWarshipCreating(); - _warship.SetPosition(rand.nextInt(100),rand.nextInt(100),getWidth(),getHeight()); - repaint(); + 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(); + } }); - ChooseButton.addActionListener(e -> { - selectedWarship=_warship; + SelectButton.addActionListener(e -> { + selectedWarship = _warship; dispose(); }); } diff --git a/src/MapWithSetWarshipsGeneric.java b/src/MapWithSetWarshipsGeneric.java index b4f55a0..15a95b9 100644 --- a/src/MapWithSetWarshipsGeneric.java +++ b/src/MapWithSetWarshipsGeneric.java @@ -19,19 +19,19 @@ public class MapWithSetWarshipsGeneric { - private T[] _places; +public class SetWarshipsGeneric{ + private final Object[] _places; public int getCount() { - return _places != null ? _places.length : 0; + return _places.length; } public SetWarshipsGeneric(int count) { - _places = (T[]) new Object[count]; + _places = new Object[count]; } public int Insert(T warship) @@ -17,7 +17,7 @@ public class SetWarshipsGeneric{ public int Insert(T warship, int position) { - if (position < 0 || position >= getCount() || _places[position] == null) + if (position < 0 || position >= getCount()) return -1; int empty = -1; @@ -43,7 +43,7 @@ public class SetWarshipsGeneric{ if (position >= getCount() || position < 0 || _places[position] == null) return null; - T deleted =_places[position]; + T deleted =(T) _places[position]; _places[position] = null; return deleted; } @@ -53,6 +53,6 @@ public class SetWarshipsGeneric{ if (position >= getCount() || position < 0) return null; - return _places[position]; + return (T) _places[position]; } } diff --git a/src/WarshipCreatorGeneric.java b/src/WarshipCreatorGeneric.java index a4495ea..2c67a1b 100644 --- a/src/WarshipCreatorGeneric.java +++ b/src/WarshipCreatorGeneric.java @@ -1,36 +1,39 @@ +import java.util.ArrayList; import java.util.Random; public class WarshipCreatorGeneric { - private final EntityWarship[] Warships; - private final IDrawingObjectBlock[] Blocks; + private final ArrayList Warships; + private final ArrayList Blocks; private int WarshipsCount = 0; private int BlocksCount = 0; public WarshipCreatorGeneric(int warshipsCount, int blocksCount){ - Warships = new EntityWarship[warshipsCount]; - Blocks = new IDrawingObjectBlock[blocksCount]; + Warships = new ArrayList<>(warshipsCount); + Blocks = new ArrayList<>(blocksCount); } public int AddWarship(T warship){ - if(WarshipsCount < Warships.length){ - Warships[WarshipsCount] = warship; + if(WarshipsCount <= Warships.size()){ + Warships.add(warship); WarshipsCount++; return WarshipsCount - 1; } return -1; } + public int AddBlock(U block){ - if(BlocksCount < Blocks.length){ - Blocks[BlocksCount] = 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[rand.nextInt(WarshipsCount)]; - U block = (U)Blocks[rand.nextInt(BlocksCount)]; + 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); -- 2.25.1 From 930a436de9d9339b3980a0e8e1445a1c92070367 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Tue, 15 Nov 2022 18:17:45 +0400 Subject: [PATCH 8/8] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FormWarshipCreator.java | 1 - src/MapWithSetWarshipsGeneric.java | 1 - src/SetWarshipsGeneric.java | 1 - 3 files changed, 3 deletions(-) diff --git a/src/FormWarshipCreator.java b/src/FormWarshipCreator.java index 880517b..38653c6 100644 --- a/src/FormWarshipCreator.java +++ b/src/FormWarshipCreator.java @@ -28,7 +28,6 @@ public class FormWarshipCreator extends JDialog{ private DrawingWarship selectedWarship; private boolean isModified = false; - public FormWarshipCreator(){ InitializeComponent(); } diff --git a/src/MapWithSetWarshipsGeneric.java b/src/MapWithSetWarshipsGeneric.java index 15a95b9..471859b 100644 --- a/src/MapWithSetWarshipsGeneric.java +++ b/src/MapWithSetWarshipsGeneric.java @@ -104,7 +104,6 @@ public class MapWithSetWarshipsGeneric { return 1; } - public T Remove(int position) { if (position >= getCount() || position < 0 || _places[position] == null) -- 2.25.1