From efbca96f662c6f9a40aa4e24208fe5c98c51834f Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Sun, 16 Oct 2022 00:03:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BC=D0=BC=D0=B8=D1=82.=20=D0=92=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=83=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0.=20=D0=92=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20(=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=B8=D0=B7=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B8=D0=B7=20=D0=B7?= =?UTF-8?q?=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B5=D1=85=D0=BE=D0=B4=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B0=20IntellIJ=20Ultimate)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AbstractMap.java | 148 +++++++++++++++++++++++++ src/BlockForm.java | 9 ++ src/Direction.java | 3 +- src/DrawingAdvancedWarship.java | 71 ++++++++++++ src/DrawingBlock.java | 11 +- src/DrawingField.java | 62 ----------- src/DrawingMap.java | 71 ++++++++++++ src/DrawingObjectWarship.java | 39 +++++++ src/DrawingRoundBlocks.java | 51 +++++++++ src/DrawingTriangleBlocks.java | 58 ++++++++++ src/DrawingWarship.java | 58 ++++++++-- src/EntityAdvancedWarship.java | 32 ++++++ src/EntityWarship.java | 2 +- src/{FormWarship.form => FormMap.form} | 128 ++++++++++++--------- src/FormMap.java | 76 +++++++++++++ src/FormWarship.java | 86 -------------- src/IDrawingObject.java | 9 ++ src/IDrawingObjectBlock.java | 6 + src/Main.java | 2 +- src/SecondMap.java | 37 +++++++ src/SimpleMap.java | 36 ++++++ 21 files changed, 781 insertions(+), 214 deletions(-) create mode 100644 src/AbstractMap.java create mode 100644 src/BlockForm.java create mode 100644 src/DrawingAdvancedWarship.java delete mode 100644 src/DrawingField.java create mode 100644 src/DrawingMap.java create mode 100644 src/DrawingObjectWarship.java create mode 100644 src/DrawingRoundBlocks.java create mode 100644 src/DrawingTriangleBlocks.java create mode 100644 src/EntityAdvancedWarship.java rename src/{FormWarship.form => FormMap.form} (65%) create mode 100644 src/FormMap.java delete mode 100644 src/FormWarship.java create mode 100644 src/IDrawingObject.java create mode 100644 src/IDrawingObjectBlock.java create mode 100644 src/SecondMap.java create mode 100644 src/SimpleMap.java diff --git a/src/AbstractMap.java b/src/AbstractMap.java new file mode 100644 index 0000000..a35d009 --- /dev/null +++ b/src/AbstractMap.java @@ -0,0 +1,148 @@ +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 _freeWaterArea = 0; + protected final int _land = 1; + + public BufferedImage CreateMap(int width, int height, IDrawingObject drawingObject) + { + _width = width; + _height = height; + _drawingObject = drawingObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + + public BufferedImage MoveObject(Direction direction) + { + float[] dim = _drawingObject.GetCurrentPosition(); + + if (CheckLand(dim[0], dim[1], dim[2], dim[3]) != 0) + { + _drawingObject.MoveObject(SetOppositDirection(direction)); + } + + if (true) + { + _drawingObject.MoveObject(direction); + } + return DrawMapWithObject(); + } + + private boolean SetObjectOnMap() + { + if (_drawingObject==null || _map == null) + { + return false; + } + int x = _random.nextInt(10); + int y = _random.nextInt(10); + _drawingObject.SetObject(x, y, _width, _height); + + float[] dim = _drawingObject.GetCurrentPosition(); + + while (CheckLand(dim[0], dim[1], dim[2], dim[3]) != 2) + { + int res; + do + { + res = CheckLand(dim[0], dim[1], dim[2], dim[3]); + if (res == 0) + { + _drawingObject.SetObject((int)dim[0], (int)dim[1], _width, _height); + return true; + } + else + { + dim[0] += _size_x; + } + } while (res != 2); + dim[0] = x; + dim[1] += _size_y; + } + return false; + } + + 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[i].length; ++j) + { + if (_map[i][j] == _freeWaterArea) + { + DrawWaterPart(gr, i, j); + } + else if (_map[i][j] == _land) + { + DrawLandPart(gr, i, j); + } + } + } + _drawingObject.DrawingObject(gr); + return bmp; + } + private int CheckLand(float Left, float Right, float Top, float Bottom) + { + int RUi = (int)(Left / _size_x); + int RUj = (int)(Right / _size_y); + int LDi = (int)(Top / _size_x); + int LDj = (int)(Bottom / _size_y); + + if (RUi < 0 || RUj < 0 || LDi >= _map[0].length || LDj >= _map.length) + { + return -1; + } + for (int x = RUi; x <= LDi; x++) + { + for (int y = RUj; y <= LDj; y++) + { + if (_map[x][y] == _land) + { + return 1; + } + } + } + return 0; + } + private Direction SetOppositDirection(Direction dir) + { + switch (dir) + { + case Up: + return Direction.Down; + case Down: + return Direction.Up; + case Left: + return Direction.Right; + case Right: + return Direction.Left; + case None: + return Direction.None; + } + return Direction.None; + } + + protected abstract void GenerateMap(); + protected abstract void DrawWaterPart(Graphics gr, int i, int j); + protected abstract void DrawLandPart(Graphics gr, int i, int j); +} diff --git a/src/BlockForm.java b/src/BlockForm.java new file mode 100644 index 0000000..7237f23 --- /dev/null +++ b/src/BlockForm.java @@ -0,0 +1,9 @@ +public enum BlockForm { + Rect(0), + Round(1), + Triangle(2); + public final int Value; + BlockForm(int i) { + Value=i; + } +} diff --git a/src/Direction.java b/src/Direction.java index c7b3e68..5f50742 100644 --- a/src/Direction.java +++ b/src/Direction.java @@ -2,6 +2,7 @@ public enum Direction { Up(1), Down(2), Left(3), - Right(4); + Right(4), + None(0); Direction(int value){} } diff --git a/src/DrawingAdvancedWarship.java b/src/DrawingAdvancedWarship.java new file mode 100644 index 0000000..f92d3e6 --- /dev/null +++ b/src/DrawingAdvancedWarship.java @@ -0,0 +1,71 @@ +import java.awt.*; + +public class DrawingAdvancedWarship extends DrawingWarship { + + public DrawingAdvancedWarship(int speed, float weight, Color bodyColor, Color dopColor, boolean Helipad,boolean Antenna, boolean Missile,int blockForm) { + super(speed, weight, bodyColor, 120, 50); + Warship=new EntityAdvancedWarship(speed,weight,bodyColor,dopColor,Helipad,Antenna,Missile); + Blocks= GetFormOfBlock(blockForm); + Blocks.SetBlockCount(2*(int)(Math.random()*3+1)); + } + + @Override + public void DrawTransport(Graphics g) { + + if (! (Warship instanceof EntityAdvancedWarship)) + { + return; + } + + EntityAdvancedWarship advancedWarship = (EntityAdvancedWarship)Warship; + Graphics2D g2 = (Graphics2D) g; + + int [] x1 ={_startPosX+25,_startPosX+65,_startPosX+75,_startPosX+25,_startPosX+25}; + int [] y1 ={_startPosY,_startPosY,_startPosY+5,_startPosY+5,_startPosY}; + int [] x2 ={_startPosX+25,_startPosX+75,_startPosX+65,_startPosX+25,_startPosX+25}; + int [] y2 ={_startPosY+45,_startPosY+45,_startPosY+50,_startPosY+50,_startPosY+45}; + + if (advancedWarship.Missile) + { + g2.setColor(advancedWarship.GetDopColor()); + g2.fillPolygon(x1,y1,5); + + g2.setColor(Color.BLACK); + g.drawLine(_startPosX + 25, _startPosY + 5 - 5, _startPosX + 65, _startPosY + 5 - 5); + g.drawLine(_startPosX + 65, _startPosY + 5 - 5,_startPosX + 75, _startPosY + 10 - 5); + g.drawLine(_startPosX + 75, _startPosY + 10 - 5, _startPosX + 25, _startPosY + 10 - 5); + g.drawLine(_startPosX + 25, _startPosY + 10 - 5, _startPosX + 25, _startPosY + 5 - 5); + + g2.setColor(advancedWarship.GetDopColor()); + g2.fillPolygon(x2,y2,5); + + g2.setColor(Color.BLACK); + g.drawLine(_startPosX + 25, _startPosY + 50 - 5,_startPosX + 75, _startPosY + 50 - 5); + g.drawLine(_startPosX + 75, _startPosY + 50 - 5,_startPosX + 65, _startPosY + 55 - 5); + g.drawLine(_startPosX + 65, _startPosY + 55 - 5,_startPosX + 25, _startPosY + 55 - 5); + g.drawLine(_startPosX + 25, _startPosY + 55 - 5,_startPosX + 25, _startPosY + 50 - 5); + } + + _startPosY += 5; + super.DrawTransport(g); + _startPosY -= 5; + + if (advancedWarship.Helipad) + { + g2.setColor(advancedWarship.GetDopColor()); + g.fillOval(_startPosX + 85, _startPosY + 20-5, 20, 20); + + g2.setColor(Color.BLACK); + g.drawOval(_startPosX + 85, _startPosY + 20-5, 20, 20); + g.drawLine(_startPosX + 90, _startPosY + 25 - 5, _startPosX + 90, _startPosY + 35 - 5); + g.drawLine(_startPosX + 90+10, _startPosY + 25 - 5, _startPosX + 90+10, _startPosY + 35 - 5); + g.drawLine(_startPosX + 90, _startPosY + 30 - 5, _startPosX + 100, _startPosY + 30 - 5); + } + if (advancedWarship.Antenna) + { + g2.setColor(Color.BLACK); + g.drawLine(_startPosX + 15, _startPosY + 20 - 5, _startPosX + 15, _startPosY + 40 - 5); + g.drawLine(_startPosX +10, _startPosY + 30 - 5, _startPosX + 20, _startPosY + 30 - 5); + } + } +} diff --git a/src/DrawingBlock.java b/src/DrawingBlock.java index 28b00fe..5251684 100644 --- a/src/DrawingBlock.java +++ b/src/DrawingBlock.java @@ -1,9 +1,14 @@ import java.awt.*; -public class DrawingBlock { +public class DrawingBlock implements IDrawingObjectBlock{ - private BlockCount _block; + private BlockCount _block=null; + public DrawingBlock(BlockCount block) { + _block=block; + } + + @Override public void SetBlockCount(int count){ for (BlockCount temp: BlockCount.values()) if (temp.GetBlockCount() == count){ @@ -11,7 +16,7 @@ public class DrawingBlock { return; } } - + @Override public void DrawBlock(Graphics2D g,int _startPosX, int _startPosY) { if (_block.GetBlockCount() >= 2) { g.setColor(Color.GRAY); diff --git a/src/DrawingField.java b/src/DrawingField.java deleted file mode 100644 index 0275530..0000000 --- a/src/DrawingField.java +++ /dev/null @@ -1,62 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.util.Random; - -public class DrawingField extends JPanel { - private final FormWarship Field; - DrawingWarship _warship; - public DrawingField(FormWarship field) { - this.Field = field; - } - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - Graphics2D g2 =(Graphics2D)g; - if (_warship!=null) - _warship.DrawTransport(g2); - else return; - } - public void DrawWarship(Graphics g){ - if (_warship!=null) - _warship.DrawTransport(g); - else return; - } - public void UpButtonAction(){ - if (_warship!=null) - _warship.MoveTransport(Direction.Up); - else - return; - } - public void DownButtonAction(){ - if (_warship!=null) - _warship.MoveTransport(Direction.Down); - else - return; - } - public void RightButtonAction(){ - if (_warship!=null) - _warship.MoveTransport(Direction.Right); - else - return; - } - public void LeftButtonAction(){ - if (_warship!=null) - _warship.MoveTransport(Direction.Left); - else - return; - } - public void CreateButtonAction(){ - Random rand=new Random(); - _warship=new DrawingWarship(); - _warship.Init(rand.nextInt(50)+10,rand.nextInt(3000)+20000,new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256))); - _warship.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight()); - Field.SpeedLabel.setText("Скорость: "+_warship.GetWarship().GetSpeed()); - Field.WeightLabel.setText("Вес: "+_warship.GetWarship().GetWeight()); - Field.BodyColorLabel.setText("Цвет: "+Integer.toHexString(_warship.GetWarship().GetBodyColor().getRGB()).substring(2)); - } - public void ResizeField(){ - if (_warship!=null) - _warship.ChangeBorders(getWidth(),getHeight()); - else return; - } -} diff --git a/src/DrawingMap.java b/src/DrawingMap.java new file mode 100644 index 0000000..36ae792 --- /dev/null +++ b/src/DrawingMap.java @@ -0,0 +1,71 @@ +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 rand=new Random(); + warship.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight()); + Map.SpeedLabel.setText("Скорость: "+warship.GetWarship().GetSpeed()); + Map.WeightLabel.setText("Вес: "+warship.GetWarship().GetWeight()); + Map.BodyColorLabel.setText("Цвет: "+Integer.toHexString(warship.GetWarship().GetBodyColor().getRGB()).substring(2)); + bufferedImage = _abstractMap.CreateMap(700,550,new DrawingObjectWarship(warship)); + } + public void CreateButtonAction(){ + Random rand=new Random(); + Color color1 = JColorChooser.showDialog(Map, "Выберите цвет тела корабля", null); + if(color1==null) + color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); + DrawingWarship warship=new DrawingWarship(rand.nextInt(50)+10,rand.nextInt(3000)+20000,color1,rand.nextInt(3)); + SetData(warship); + } + public void CreateModifButtonAction(){ + Random rand=new Random(); + Color color1=JColorChooser.showDialog(Map, "Выберите цвет тела корабля",null); + Color color2=JColorChooser.showDialog(Map, "Выборите цвет модификаций корабля",null); + if(color1==null) + color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); + if (color2==null) + color2=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); + DrawingWarship warship = new DrawingAdvancedWarship(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, color1, + color2, rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3)); + 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 SecondMap(); + 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..920ae47 --- /dev/null +++ b/src/DrawingObjectWarship.java @@ -0,0 +1,39 @@ +import java.awt.*; + +public class DrawingObjectWarship implements IDrawingObject { + + private DrawingWarship _warship=null; + + public DrawingObjectWarship(DrawingWarship warship) + { + _warship= warship; + } + + public float Step() { + if(_warship != null && _warship.Warship != null) + return _warship.Warship.Step; + return 0; + } + + @Override + public void SetObject(int x, int y, int width, int height) { + _warship.SetPosition(x,y,width,height); + } + + @Override + public void MoveObject(Direction direction) { + _warship.MoveTransport(direction); + } + + @Override + public void DrawingObject(Graphics g) { + _warship.DrawTransport(g); + } + + @Override + public float[] GetCurrentPosition() { + if(_warship!=null) + return _warship.GetCurrentPosition(); + return null; + } +} diff --git a/src/DrawingRoundBlocks.java b/src/DrawingRoundBlocks.java new file mode 100644 index 0000000..fc1e96f --- /dev/null +++ b/src/DrawingRoundBlocks.java @@ -0,0 +1,51 @@ +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 DrawBlock(Graphics2D g, int _startPosX, int _startPosY) { + if (_block.GetBlockCount() >= 2) { + g.setColor(Color.GRAY); + g.fillOval(_startPosX + 25, _startPosY + 10, 10, 10); + g.setColor(Color.BLACK); + g.drawOval(_startPosX + 25, _startPosY + 10, 10, 10); + g.setColor(Color.GRAY); + g.fillOval(_startPosX + 25, _startPosY + 20, 10, 10); + g.setColor(Color.BLACK); + g.drawOval(_startPosX + 25, _startPosY + 20, 10, 10); + } + if (_block.GetBlockCount() >= 4) { + g.setColor(Color.GRAY); + g.fillOval(_startPosX+35,_startPosY+10,10,10); + g.setColor(Color.BLACK); + g.drawOval(_startPosX+35,_startPosY+10,10,10); + g.setColor(Color.GRAY); + g.fillOval(_startPosX+35,_startPosY+20,10,10); + g.setColor(Color.BLACK); + g.drawOval(_startPosX+35,_startPosY+20,10,10); + } + if (_block.GetBlockCount() >= 6) { + g.setColor(Color.GRAY); + g.fillOval(_startPosX+45,_startPosY+10,10,10); + g.setColor(Color.BLACK); + g.drawOval(_startPosX+45,_startPosY+10,10,10); + g.setColor(Color.GRAY); + g.fillOval(_startPosX+45,_startPosY+20,10,10); + g.setColor(Color.BLACK); + g.drawOval(_startPosX+45,_startPosY+20,10,10); + } + } +} diff --git a/src/DrawingTriangleBlocks.java b/src/DrawingTriangleBlocks.java new file mode 100644 index 0000000..0e955c8 --- /dev/null +++ b/src/DrawingTriangleBlocks.java @@ -0,0 +1,58 @@ +import java.awt.*; + +public class DrawingTriangleBlocks implements IDrawingObjectBlock{ + private BlockCount _block; + + public DrawingTriangleBlocks(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 DrawBlock(Graphics2D g, int _startPosX, int _startPosY) { + + int[] x1={_startPosX + 30,_startPosX + 35,_startPosX + 25,_startPosX + 30}; + int[] y1={_startPosY + 10,_startPosY + 20,_startPosY + 20,_startPosY + 10}; + int[] x2={_startPosX + 30+10,_startPosX + 35+10,_startPosX + 25+10,_startPosX + 30+10}; + int[] y2={_startPosY + 10+10,_startPosY + 20+10,_startPosY + 20+10,_startPosY + 10+10}; + int[] x3={_startPosX + 30+10+10,_startPosX + 35+10+10,_startPosX + 25+10+10,_startPosX + 30+10+10}; + + if (_block.GetBlockCount() >= 2) { + g.setColor(Color.GRAY); + g.fillPolygon(x1,y1,4); + g.setColor(Color.BLACK); + g.drawPolygon(x1,y1,4); + g.setColor(Color.GRAY); + g.fillPolygon(x1,y2,4); + g.setColor(Color.BLACK); + g.drawPolygon(x1,y2,4); + } + if (_block.GetBlockCount() >= 4) { + g.setColor(Color.GRAY); + g.fillPolygon(x2,y1,4); + g.setColor(Color.BLACK); + g.drawPolygon(x2,y1,4); + g.setColor(Color.GRAY); + g.fillPolygon(x2,y2,4); + g.setColor(Color.BLACK); + g.drawPolygon(x2,y2,4); + } + if (_block.GetBlockCount() >= 6) { + g.setColor(Color.GRAY); + g.fillPolygon(x3,y1,4); + g.setColor(Color.BLACK); + g.drawPolygon(x3,y1,4); + g.setColor(Color.GRAY); + g.fillPolygon(x3,y2,4); + g.setColor(Color.BLACK); + g.drawPolygon(x3,y2,4); + } + } +} diff --git a/src/DrawingWarship.java b/src/DrawingWarship.java index 440d8ad..12244e8 100644 --- a/src/DrawingWarship.java +++ b/src/DrawingWarship.java @@ -1,26 +1,56 @@ import java.awt.*; public class DrawingWarship { - private EntityWarship Warship; + protected EntityWarship Warship; public EntityWarship GetWarship(){ return Warship; } - public DrawingBlock Blocks; - private int _startPosX; - private int _startPosY; + protected IDrawingObjectBlock Blocks; + private BlockCount _block; + protected int _startPosX; + protected int _startPosY; private Integer _pictureWidth = null; private Integer _pictureHeight = null; - private final int _warshipWidth = 120; - private final int _warshipHeight = 40; + private int _warshipWidth = 120; + 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 DrawingBlock(); + Warship = new EntityWarship(speed, weight, bodyColor); + Blocks= GetFormOfBlock(blockForm); Blocks.SetBlockCount(2*(int)(Math.random()*3+1)); } + 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 DrawingBlock(_block); + case Round: + return new DrawingRoundBlocks(_block); + case Triangle: + return new DrawingTriangleBlocks(_block); + } + return null; + } + + protected DrawingWarship(int speed, float weight, Color bodyColor,int warshipWidth, int warshipHeight) + { + Warship = new EntityWarship(speed, weight, bodyColor); + Blocks= new DrawingBlock(_block); + Blocks.SetBlockCount(2*(int)(Math.random()*3+1)); + _warshipWidth=warshipWidth; + _pictureHeight=warshipHeight; + } + public void SetPosition(int x, int y, int width, int height) { if (x >= 0 && x+_warshipWidth <= width && y >= 0 && y+_warshipHeight <= height) @@ -125,4 +155,12 @@ public class DrawingWarship { _startPosY = _pictureHeight - _warshipHeight; } } + public float[] GetCurrentPosition() { + float[] dim = new float[4]; + dim[0] = _startPosX; + dim[1] =_startPosY; + dim[2] = _startPosX + _warshipWidth; + dim[3] = _startPosY + _warshipHeight; + return dim; + } } diff --git a/src/EntityAdvancedWarship.java b/src/EntityAdvancedWarship.java new file mode 100644 index 0000000..7607aa4 --- /dev/null +++ b/src/EntityAdvancedWarship.java @@ -0,0 +1,32 @@ +import java.awt.*; + +public class EntityAdvancedWarship extends EntityWarship{ + + public Color DopColor; + public Color GetDopColor() { + return DopColor; + } + + public boolean Helipad; + public boolean GetHelipad() { + return Helipad; + } + + public boolean Antenna; + public boolean GetAntenna() { + return Antenna; + } + + public boolean Missile; + public boolean GetMissile() { + return Missile; + } + + public EntityAdvancedWarship(int speed, float weight, Color bodyColor, Color dopColor, boolean helipad, boolean antenna, boolean missile){ + super(speed,weight,bodyColor); + DopColor = dopColor; + Helipad = helipad; + Antenna = antenna; + Missile = missile; + } +} diff --git a/src/EntityWarship.java b/src/EntityWarship.java index 157944d..58a7e1b 100644 --- a/src/EntityWarship.java +++ b/src/EntityWarship.java @@ -19,7 +19,7 @@ public class EntityWarship { public int 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(60)+10 : speed; diff --git a/src/FormWarship.form b/src/FormMap.form similarity index 65% rename from src/FormWarship.form rename to src/FormMap.form index 447e9ae..0617004 100644 --- a/src/FormWarship.form +++ b/src/FormMap.form @@ -1,32 +1,22 @@ -
- + + - + - - - - - - - - - - - + - + - + @@ -34,12 +24,12 @@ - + - + @@ -47,7 +37,7 @@ - + @@ -57,15 +47,25 @@ - + + + + + + + + + + + - + - + @@ -73,17 +73,25 @@ + + + + + + + + - + - + - + @@ -92,25 +100,7 @@ - - - - - - - - - - - - - - - - - - - + @@ -119,38 +109,76 @@ + + + + + + + + + + + + + + + + + + - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + diff --git a/src/FormMap.java b/src/FormMap.java new file mode 100644 index 0000000..603f457 --- /dev/null +++ b/src/FormMap.java @@ -0,0 +1,76 @@ +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.Objects; + +public class FormMap extends JFrame { + private int Width; + private int Height; + DrawingMap map = new DrawingMap(this); + private JButton ButtonDown; + private JButton ButtonRight; + private JButton ButtonLeft; + private JButton ButtonUp; + private JButton ButtonCreate; + private JButton ButtonCreateModif; + public JLabel SpeedLabel; + public JLabel WeightLabel; + public JLabel BodyColorLabel; + private JComboBox ComboBoxSelectorMap; + private JPanel PictureBox; + + public FormMap(){ + super("Военный корабль"); + setContentPane(PictureBox); + setSize(1000,700); + Width = getWidth(); + Height = getHeight(); + ShowWindow(); + map.setBounds(0,0,Width,Height); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + private void ShowWindow(){ + 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) PictureBox.getGraphics(); + graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); + PictureBox.paintComponents(graphics); + map.DrawMap(graphics); + } + +} diff --git a/src/FormWarship.java b/src/FormWarship.java deleted file mode 100644 index 752b5b5..0000000 --- a/src/FormWarship.java +++ /dev/null @@ -1,86 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; - -public class FormWarship extends JFrame{ - JPanel BottomPanel; - JPanel CreatePanel; - JPanel DimentionPanel; - - JLabel SpeedLabel; - JLabel WeightLabel; - JLabel BodyColorLabel; - - private int Width; - private int Height; - DrawingField field = new DrawingField(this); - - JButton ButtonCreate; - JButton ButtonUp; - JButton ButtonDown; - JButton ButtonRight; - JButton ButtonLeft; - JPanel PictureBox; - - - public FormWarship(){ - super("Военный корабль"); - setContentPane(PictureBox); - setSize(700,400); - Width=getWidth(); - Height=getHeight(); - ShowWindow(); - field.setBounds(0,0,Width,Height); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setVisible(true); - } - - public void ShowWindow(){ - - ButtonUp.addActionListener(e->{ - field.UpButtonAction(); - ReDraw(); - }); - - ButtonDown.addActionListener(e->{ - field.DownButtonAction(); - ReDraw(); - }); - - ButtonRight.addActionListener(e->{ - field.RightButtonAction(); - ReDraw(); - }); - - ButtonLeft.addActionListener(e->{ - field.LeftButtonAction(); - ReDraw(); - }); - - ButtonCreate.addActionListener(e->{ - field.CreateButtonAction(); - ReDraw(); - }); - addComponentListener(new ComponentAdapter() { - @Override - public void componentResized(ComponentEvent e) { - super.componentResized(e); - Width=getWidth(); - Height=getHeight(); - - field.ResizeField(); - field.setBounds(0,0,Width,Height); - ReDraw(); - } - }); - } - private void ReDraw() - { - Graphics2D graphics = (Graphics2D) PictureBox.getGraphics(); - graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); - PictureBox.paintComponents(graphics); - field.DrawWarship(graphics); - } -} - diff --git a/src/IDrawingObject.java b/src/IDrawingObject.java new file mode 100644 index 0000000..3df3eb3 --- /dev/null +++ b/src/IDrawingObject.java @@ -0,0 +1,9 @@ +import java.awt.*; + +public interface IDrawingObject { + public float Step=0; + void SetObject(int x, int y, int width, int height); + void MoveObject(Direction direction); + void DrawingObject(Graphics g); + float[] GetCurrentPosition(); +} diff --git a/src/IDrawingObjectBlock.java b/src/IDrawingObjectBlock.java new file mode 100644 index 0000000..0db5118 --- /dev/null +++ b/src/IDrawingObjectBlock.java @@ -0,0 +1,6 @@ +import java.awt.*; + +public interface IDrawingObjectBlock { + void SetBlockCount(int count); + void DrawBlock(Graphics2D g, int _startPosX, int _startPosY); +} diff --git a/src/Main.java b/src/Main.java index ec64b71..d27a952 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,5 @@ public class Main { public static void main(String[] args) { - new FormWarship(); + new FormMap(); } } diff --git a/src/SecondMap.java b/src/SecondMap.java new file mode 100644 index 0000000..030b6a3 --- /dev/null +++ b/src/SecondMap.java @@ -0,0 +1,37 @@ +import java.awt.*; + +public class SecondMap extends AbstractMap{ + + @Override + protected void GenerateMap() { + _map = new int[60][60]; + _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] = _freeWaterArea; + } + } + while (counter < 20) { + int x = _random.nextInt(59); + int y = _random.nextInt(59); + if (_map[x][y] == _freeWaterArea) { + _map[x][y] = _land; + counter++; + } + } + } + + @Override + protected void DrawWaterPart(Graphics gr, int i, int j) { + gr.setColor(new Color(0xFFFFFF)); + gr.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } + + @Override + protected void DrawLandPart(Graphics gr, int i, int j) { + gr.setColor(new Color(0x050303)); + gr.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } +} diff --git a/src/SimpleMap.java b/src/SimpleMap.java new file mode 100644 index 0000000..9d4bae7 --- /dev/null +++ b/src/SimpleMap.java @@ -0,0 +1,36 @@ +import java.awt.*; + +public class SimpleMap extends AbstractMap { + @Override + protected void DrawWaterPart(Graphics gr, int i, int j) { + gr.setColor(new Color(0x5285B6)); + gr.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } + + @Override + protected void DrawLandPart(Graphics gr, int i, int j) { + gr.setColor(new Color(0x422A1D)); + gr.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] = _freeWaterArea; + } + } + while (counter < 50) { + int x = _random.nextInt(99); + int y = _random.nextInt(99); + if (_map[x][y] == _freeWaterArea) { + _map[x][y] = _land; + counter++; + } + } + } +}