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 @@ -