From 9f2db8fe31e69b8ad01756ee946de4d77c904aa8 Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Thu, 10 Nov 2022 15:44:45 +0300 Subject: [PATCH] lab 2 --- AbstractMap.java | 188 +++++++++++++++++++++++++++++++++++ DrawingAircraft.java | 46 +++++++-- DrawingEngines.java | 17 ++-- DrawingModernAircraft.java | 78 +++++++++++++++ DrawingObjectAircraft.java | 42 ++++++++ DrawingTruncatedEngines.java | 34 +++++++ DrawingWavyEngines.java | 46 +++++++++ EntityAircraft.java | 2 +- EntityModernAircraft.java | 18 ++++ FormAircraft.java | 4 +- IDrawingEngines.java | 6 ++ IDrawingObject.java | 11 ++ Main.java | 2 +- MyMap.java | 72 ++++++++++++++ SimpleMap.java | 47 +++++++++ formMap.form | 173 ++++++++++++++++++++++++++++++++ formMap.java | 119 ++++++++++++++++++++++ 17 files changed, 884 insertions(+), 21 deletions(-) create mode 100644 AbstractMap.java create mode 100644 DrawingModernAircraft.java create mode 100644 DrawingObjectAircraft.java create mode 100644 DrawingTruncatedEngines.java create mode 100644 DrawingWavyEngines.java create mode 100644 EntityModernAircraft.java create mode 100644 IDrawingEngines.java create mode 100644 IDrawingObject.java create mode 100644 MyMap.java create mode 100644 SimpleMap.java create mode 100644 formMap.form create mode 100644 formMap.java diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..0667a5f --- /dev/null +++ b/AbstractMap.java @@ -0,0 +1,188 @@ +import java.awt.*; +import java.util.Random; + +public abstract class AbstractMap +{ + private IDrawingObject _drawningObject = null; + protected int[][] _map = null; + protected int _width; + protected int _height; + protected float _size_x; + protected float _size_y; + protected Random _random = new Random(); + protected int _freeRoad = 0; + protected int _barrier = 1; + public void CreateMap(int width, int height, IDrawingObject drawningObject) + { + _width = width; + _height = height; + _drawningObject = drawningObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + } + + private Point checkBarrier(Point leftTop, Point rightBottom) + { + return checkBarrier(leftTop, rightBottom, false, false, true, false); + } + private Point checkBarrier(Point leftTop, Point rightBottom, boolean minLeft, boolean maxLeft, boolean isTop, boolean isBottom) + { + Point res = new Point(-1, -1); + + for (int i = (int)(leftTop.y / _size_y); i <= (int)(rightBottom.y / _size_y) && i < _map.length; ++i) + { + for (int j = (int)(leftTop.x / _size_x); j <= (int)(rightBottom.x / _size_x) && j < _map[0].length; ++j) + { + if (j < 0) j = 0; + if (i < 0) i = 0; + if (_map[i][j] != _barrier) continue; + + if (res.y == -1) res = new Point(j, i); + if (minLeft && res.x > j) res = new Point(j, i); + if (maxLeft && res.x < j) res = new Point(j, i); + if(isBottom) res = new Point(j, i); + if (isTop) return new Point(j, i); + } + } + + return res; + } + public void MoveObject(Direction direction) + { + // TODO проверка, что объект может переместится в требуемом + + Point leftTop = _drawningObject.GetLeftTop(); + Point rightBottom = _drawningObject.GetRightBottom(); + + float drawningWidth = rightBottom.x - leftTop.x; + float drawningHeight = rightBottom.y - leftTop.y; + + boolean minLeft = false; + boolean maxLeft = false; + boolean isTop = false; + boolean isBottom = false; + + if (direction == Direction.Left) + { + leftTop.x -= _drawningObject.getStep(); + maxLeft = true; + } + if (direction == Direction.Right) + { + leftTop.x += _drawningObject.getStep(); + minLeft = true; + } + if (direction == Direction.Up) { + leftTop.y -= _drawningObject.getStep(); + isTop = true; + } + if (direction == Direction.Down) + { + leftTop.y += _drawningObject.getStep(); + isBottom = true; + } + + rightBottom.x = leftTop.x + (int)drawningWidth; + rightBottom.y = leftTop.y + (int)drawningHeight; + + Point currentBarrier = checkBarrier(leftTop, rightBottom, minLeft, maxLeft, isTop, isBottom); + + if (currentBarrier.x == -1) + { + _drawningObject.MoveObject(direction); + } + + else if (direction == Direction.Left) + leftTop.x = (int)((currentBarrier.x + 1) * _size_x) + 1; + + else if (direction == Direction.Right) + leftTop.x = (int)(currentBarrier.x * _size_x) - (int)drawningWidth - 1; + + else if (direction == Direction.Up) + leftTop.y = (int)((currentBarrier.y + 1) * _size_y) + 1; + + else if (direction == Direction.Down) + leftTop.y = (int)(currentBarrier.y * _size_y) - (int)drawningHeight - 1; + + if (currentBarrier.y != -1) + _drawningObject.SetObject(leftTop.x, leftTop.y, _width, _height); + + } + private boolean SetObjectOnMap() + { + if (_drawningObject == null || _map == null) + { + return false; + } + int x = _random.nextInt(0, 10); + int y = _random.nextInt(0, 10); + _drawningObject.SetObject(x, y, _width, _height); + // TODO првоерка, что объект не "накладывается" на закрытые участки + + Point leftTop = _drawningObject.GetLeftTop(); + Point rightBottom = _drawningObject.GetRightBottom(); + + float drawningWidth = rightBottom.x - leftTop.x; + float drawningHeight = rightBottom.y - leftTop.y; + + Point currentBarrier = checkBarrier(leftTop, rightBottom); + int minRowIndex = _map.length; + + while(currentBarrier.y != -1) + { + minRowIndex = currentBarrier.y < minRowIndex ? currentBarrier.y : minRowIndex; + + leftTop.x = (int)((currentBarrier.x + 1) * _size_x) + 1; + rightBottom.x = leftTop.x + (int)drawningWidth; + + if(rightBottom.x > _width) + { + leftTop.y = (int)((minRowIndex + 1) * _size_y) + 1; + rightBottom.y = leftTop.y + (int)drawningHeight; + + leftTop.x = 0; + rightBottom.x = (int)drawningWidth; + + minRowIndex = _map.length; + } + + if (rightBottom.y > _height) { + return false; + } + + currentBarrier = checkBarrier(leftTop, rightBottom); + } + + _drawningObject.SetObject((int)leftTop.x, (int)leftTop.y, _width, _height); + + return true; + } + public void DrawMapWithObject(Graphics2D gr) + { + if (_drawningObject == null || _map == null) + { + return; + } + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + if (_map[i][j] == _freeRoad) + { + DrawRoadPart(gr, i, j); + } + else if (_map[i][j] == _barrier) + { + DrawBarrierPart(gr, i, j); + } + } + } + _drawningObject.DrawningObject(gr); + } + protected abstract void GenerateMap(); + protected abstract void DrawRoadPart(Graphics2D g, int i, int j); + protected abstract void DrawBarrierPart(Graphics2D g, int i, int j); +} diff --git a/DrawingAircraft.java b/DrawingAircraft.java index 48b301a..b3625a3 100644 --- a/DrawingAircraft.java +++ b/DrawingAircraft.java @@ -4,10 +4,10 @@ import java.util.Random; class DrawingAircraft { public EntityAircraft AirFighter; - public DrawingEngines drawingEngines = new DrawingEngines(); + public IDrawingEngines drawingEngines; - private float _startPosX; - private float _startPosY; + protected float _startPosX; + protected float _startPosY; private int _pictureWidth = -1; private int _pictureHeight = -1; @@ -15,15 +15,38 @@ class DrawingAircraft private int _airFighterWidth = 195; private int _airFighterHeight = 166; - public void Init(int speed, float weight, Color bodyColor) - { + private void peekRandomEngines(Color color) { Random rnd = new Random(); + int randEngine = rnd.nextInt(1, 4); - AirFighter = new EntityAircraft(); - AirFighter.Init(speed, weight, bodyColor); - drawingEngines.Init(rnd.nextInt(1, 8), bodyColor); + switch(randEngine) { + case 1: + drawingEngines = new DrawingEngines(rnd.nextInt(1, 8), color); + break; + case 2: + drawingEngines = new DrawingTruncatedEngines(rnd.nextInt(1, 8), color); + break; + case 3: + drawingEngines = new DrawingWavyEngines(rnd.nextInt(1, 8), color); + break; + } } + public DrawingAircraft(int speed, float weight, Color bodyColor) + { + AirFighter = new EntityAircraft(speed, weight, bodyColor); + peekRandomEngines(bodyColor); + } + + + public DrawingAircraft(int speed, float weight, Color bodyColor, int airFighterWidth, int airFighterHeight) + { + this(speed, weight, bodyColor); + _airFighterWidth = airFighterWidth; + _airFighterHeight = airFighterHeight; + } + + public void SetPosition(int x, int y, int width, int height) { if (width < _airFighterWidth || height < _airFighterHeight) return; @@ -149,5 +172,12 @@ class DrawingAircraft } } + public Point getLeftTop() { + return new Point((int)_startPosX, (int)_startPosY); + } + + public Point getRightBottom() { + return new Point((int)_startPosX + _airFighterWidth, (int)_startPosY + _airFighterHeight); + } } diff --git a/DrawingEngines.java b/DrawingEngines.java index baa2d39..8e186ad 100644 --- a/DrawingEngines.java +++ b/DrawingEngines.java @@ -1,14 +1,15 @@ import java.awt.*; -public class DrawingEngines { +public class DrawingEngines implements IDrawingEngines { private EnginesCount enginesCount; private Color color; - public void Init(int count, Color bodyColor) { + public DrawingEngines(int count, Color bodyColor) { setCount(count); color = bodyColor; } + @Override public void setCount(int count) { if(count <= 2) enginesCount = EnginesCount.Two; else if(count >= 6) enginesCount = EnginesCount.Six; @@ -17,17 +18,17 @@ public class DrawingEngines { public void draw(Graphics2D g, int startPosX, int startPosY) { g.setPaint(color); - g.fillOval(startPosX + 90, startPosY + 10, 30, 15); - g.fillOval(startPosX + 90, startPosY + 141, 30, 15); + g.fillOval(startPosX + 80, startPosY + 10, 30, 15); + g.fillOval(startPosX + 80, startPosY + 141, 30, 15); if(enginesCount == EnginesCount.Two) return; - g.fillOval(startPosX + 90, startPosY + 30, 30, 15); - g.fillOval(startPosX + 90, startPosY + 121, 30, 15); + g.fillOval(startPosX + 80, startPosY + 30, 30, 15); + g.fillOval(startPosX + 80, startPosY + 121, 30, 15); if(enginesCount == EnginesCount.Four) return; - g.fillOval(startPosX + 90, startPosY + 50, 30, 15); - g.fillOval(startPosX + 90, startPosY + 101, 30, 15); + g.fillOval(startPosX + 80, startPosY + 50, 30, 15); + g.fillOval(startPosX + 80, startPosY + 101, 30, 15); } } diff --git a/DrawingModernAircraft.java b/DrawingModernAircraft.java new file mode 100644 index 0000000..a5de0e0 --- /dev/null +++ b/DrawingModernAircraft.java @@ -0,0 +1,78 @@ +import java.awt.*; + +public class DrawingModernAircraft extends DrawingAircraft +{ + public DrawingModernAircraft(int speed, float weight, Color bodyColor, Color dopColor, boolean dopWings, boolean rockets) + { + super(speed, weight, bodyColor, 195, 166); + AirFighter = new EntityModernAircraft(speed, weight, bodyColor, dopColor, dopWings, rockets); + } + @Override + public void DrawTransport(Graphics2D g) + { + if (!(AirFighter instanceof EntityModernAircraft)) + { + return; + } + + EntityModernAircraft modernAircraft = (EntityModernAircraft)AirFighter; + + g.setPaint(modernAircraft.DopColor); + + if (modernAircraft.DopWings) + { + Polygon topDopWing = new Polygon(); + topDopWing.addPoint((int)_startPosX + 78, (int)_startPosY + 56); + topDopWing.addPoint((int)_startPosX + 75, (int)_startPosY + 70); + topDopWing.addPoint((int)_startPosX + 55, (int)_startPosY + 50); + topDopWing.addPoint((int)_startPosX + 60, (int)_startPosY + 45); + + Polygon bottomDopWing = new Polygon(); + bottomDopWing.addPoint((int)_startPosX + 78, (int)_startPosY + 110); + bottomDopWing.addPoint((int)_startPosX + 75, (int)_startPosY + 96); + bottomDopWing.addPoint((int)_startPosX + 55, (int)_startPosY + 116); + bottomDopWing.addPoint((int)_startPosX + 60, (int)_startPosY + 121); + + g.fillPolygon(topDopWing); + g.fillPolygon(bottomDopWing); + } + + if (modernAircraft.Rockets) + { + Polygon topRocket1 = new Polygon(); + topRocket1.addPoint((int)_startPosX + 100, (int)_startPosY + 20); + topRocket1.addPoint((int)_startPosX + 100, (int)_startPosY + 30); + topRocket1.addPoint((int)_startPosX + 112, (int)_startPosY + 30); + topRocket1.addPoint((int)_startPosX + 120, (int)_startPosY + 25); + topRocket1.addPoint((int)_startPosX + 112, (int)_startPosY + 20); + + Polygon topRocket2 = new Polygon(); + topRocket2.addPoint((int)_startPosX + 100, (int)_startPosY + 35); + topRocket2.addPoint((int)_startPosX + 100, (int)_startPosY + 45); + topRocket2.addPoint((int)_startPosX + 112, (int)_startPosY + 45); + topRocket2.addPoint((int)_startPosX + 120, (int)_startPosY + 40); + topRocket2.addPoint((int)_startPosX + 112, (int)_startPosY + 35); + + Polygon bottomRocket1 = new Polygon(); + bottomRocket1.addPoint((int)_startPosX + 100, (int)_startPosY + 146); + bottomRocket1.addPoint((int)_startPosX + 100, (int)_startPosY + 136); + bottomRocket1.addPoint((int)_startPosX + 112, (int)_startPosY + 136); + bottomRocket1.addPoint((int)_startPosX + 120, (int)_startPosY + 141); + bottomRocket1.addPoint((int)_startPosX + 112, (int)_startPosY + 146); + + Polygon bottomRocket2 = new Polygon(); + bottomRocket2.addPoint((int)_startPosX + 100, (int)_startPosY + 131); + bottomRocket2.addPoint((int)_startPosX + 100, (int)_startPosY + 121); + bottomRocket2.addPoint((int)_startPosX + 112, (int)_startPosY + 121); + bottomRocket2.addPoint((int)_startPosX + 120, (int)_startPosY + 126); + bottomRocket2.addPoint((int)_startPosX + 112, (int)_startPosY + 131); + + g.fillPolygon(topRocket1); + g.fillPolygon(topRocket2); + g.fillPolygon(bottomRocket1); + g.fillPolygon(bottomRocket2); + } + + super.DrawTransport(g); + } +} diff --git a/DrawingObjectAircraft.java b/DrawingObjectAircraft.java new file mode 100644 index 0000000..668cffd --- /dev/null +++ b/DrawingObjectAircraft.java @@ -0,0 +1,42 @@ +import java.awt.*; + +public class DrawingObjectAircraft implements IDrawingObject +{ + private DrawingAircraft _aircraft = null; + public DrawingObjectAircraft(DrawingAircraft aircraft){ + _aircraft = aircraft; + } + + public void MoveObject(Direction direction) { + if(_aircraft == null) return; + _aircraft.MoveTransport(direction); + } + + @Override + public float getStep() { + if(_aircraft == null) return 0; + return _aircraft.AirFighter.Step; + } + + public void SetObject(int x, int y, int width, int height) + { + _aircraft.SetPosition(x, y, width, height); + } + public void DrawningObject(Graphics2D g) + { + // TODO + _aircraft.DrawTransport(g); + } + + @Override + public Point GetLeftTop() { + if(_aircraft == null) return new Point(0,0); + return _aircraft.getLeftTop(); + } + + @Override + public Point GetRightBottom() { + if(_aircraft == null) return new Point(0,0); + return _aircraft.getRightBottom(); + } +} diff --git a/DrawingTruncatedEngines.java b/DrawingTruncatedEngines.java new file mode 100644 index 0000000..b63c02f --- /dev/null +++ b/DrawingTruncatedEngines.java @@ -0,0 +1,34 @@ +import java.awt.*; + +public class DrawingTruncatedEngines implements IDrawingEngines { + private EnginesCount enginesCount; + private Color color; + + public DrawingTruncatedEngines(int count, Color bodyColor) { + setCount(count); + color = bodyColor; + } + + @Override + public void setCount(int count) { + if(count <= 2) enginesCount = EnginesCount.Two; + else if(count >= 6) enginesCount = EnginesCount.Six; + else enginesCount = EnginesCount.Four; + } + + public void draw(Graphics2D g, int startPosX, int startPosY) { + g.setPaint(color); + g.fillArc(startPosX + 90, startPosY + 10, 30, 15, 90, 180); + g.fillArc(startPosX + 90, startPosY + 141, 30, 15, 90, 180); + + if(enginesCount == EnginesCount.Two) return; + + g.fillArc(startPosX + 90, startPosY + 30, 30, 15, 90, 180); + g.fillArc(startPosX + 90, startPosY + 121, 30, 15, 90, 180); + + if(enginesCount == EnginesCount.Four) return; + + g.fillArc(startPosX + 90, startPosY + 50, 30, 15, 90, 180); + g.fillArc(startPosX + 90, startPosY + 101, 30, 15, 90, 180); + } +} diff --git a/DrawingWavyEngines.java b/DrawingWavyEngines.java new file mode 100644 index 0000000..cae2701 --- /dev/null +++ b/DrawingWavyEngines.java @@ -0,0 +1,46 @@ +import java.awt.*; + +public class DrawingWavyEngines implements IDrawingEngines { + private EnginesCount enginesCount; + private Color color; + + public DrawingWavyEngines(int count, Color bodyColor) { + setCount(count); + color = bodyColor; + } + + @Override + public void setCount(int count) { + if(count <= 2) enginesCount = EnginesCount.Two; + else if(count >= 6) enginesCount = EnginesCount.Six; + else enginesCount = EnginesCount.Four; + } + + private void drawEngine(Graphics2D g, int x, int y) { + g.setColor(color); + g.fillRect(x, y, 21, 10); + + g.fillArc(x, y - 4, 7, 8, 0, 180); + g.fillArc(x + 7, y - 4, 7, 8, 0, 180); + g.fillArc(x + 14, y - 4, 7, 8, 0, 180); + + g.fillArc(x, y + 6, 7, 8, 180, 180); + g.fillArc(x + 7, y + 6, 7, 8, 180, 180); + g.fillArc(x + 14, y + 6, 7, 8, 180, 180); + } + + public void draw(Graphics2D g, int startPosX, int startPosY) { + drawEngine(g, startPosX + 84, startPosY + 10); + drawEngine(g, startPosX + 84, startPosY + 146); + + if(enginesCount == EnginesCount.Two) return; + + drawEngine(g, startPosX + 84, startPosY + 30); + drawEngine(g, startPosX + 84, startPosY + 125); + + if(enginesCount == EnginesCount.Four) return; + + drawEngine(g, startPosX + 84, startPosY + 50); + drawEngine(g, startPosX + 84, startPosY + 106); + } +} diff --git a/EntityAircraft.java b/EntityAircraft.java index 2988375..811c81c 100644 --- a/EntityAircraft.java +++ b/EntityAircraft.java @@ -9,7 +9,7 @@ class EntityAircraft public float Step; - public void Init(int speed, float weight, Color bodyColor) + public EntityAircraft(int speed, float weight, Color bodyColor) { Random rnd = new Random(); diff --git a/EntityModernAircraft.java b/EntityModernAircraft.java new file mode 100644 index 0000000..96e9c24 --- /dev/null +++ b/EntityModernAircraft.java @@ -0,0 +1,18 @@ +import java.awt.*; + +public class EntityModernAircraft extends EntityAircraft +{ + public Color DopColor; + public boolean DopWings; + public boolean Rockets; + + + public EntityModernAircraft(int speed, float weight, Color bodyColor, Color + dopColor, boolean dopWings, boolean rockets) + { + super(speed, weight, bodyColor); + DopColor = dopColor; + DopWings = dopWings; + Rockets = rockets; + } +} diff --git a/FormAircraft.java b/FormAircraft.java index e53b6af..451ccc7 100644 --- a/FormAircraft.java +++ b/FormAircraft.java @@ -40,9 +40,7 @@ public class FormAircraft implements Form { Dimension canvSize = canv.getSize(); Random rnd = new Random(); - _airFighter = new DrawingAircraft(); - - _airFighter.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + _airFighter = new DrawingAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))); _airFighter.SetPosition((int)rnd.nextInt(10, 100), (int)rnd.nextInt(10, 100), canvSize.width, canvSize.height); diff --git a/IDrawingEngines.java b/IDrawingEngines.java new file mode 100644 index 0000000..04c08e6 --- /dev/null +++ b/IDrawingEngines.java @@ -0,0 +1,6 @@ +import java.awt.*; + +public interface IDrawingEngines { + void setCount(int count); + void draw(Graphics2D g, int x, int y); +} diff --git a/IDrawingObject.java b/IDrawingObject.java new file mode 100644 index 0000000..9665c81 --- /dev/null +++ b/IDrawingObject.java @@ -0,0 +1,11 @@ +import java.awt.*; + +public interface IDrawingObject +{ + float getStep(); + void SetObject(int x, int y, int width, int height); + void MoveObject(Direction direction); + void DrawningObject(Graphics2D g); + Point GetLeftTop(); + Point GetRightBottom(); +} diff --git a/Main.java b/Main.java index 420ccca..195c2fb 100644 --- a/Main.java +++ b/Main.java @@ -1,5 +1,5 @@ public class Main { public static void main(String[] args) { - new FormAircraft().run(); + new formMap().run(); } } diff --git a/MyMap.java b/MyMap.java new file mode 100644 index 0000000..3c57091 --- /dev/null +++ b/MyMap.java @@ -0,0 +1,72 @@ +import java.awt.*; + +public class MyMap extends AbstractMap +{ + + private Color barrierColor = Color.BLACK; + private Color roadColor = Color.GRAY; + + @Override + protected void DrawBarrierPart(Graphics2D g, int i, int j) + { + g.setPaint(barrierColor); + g.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } + + @Override + protected void DrawRoadPart(Graphics2D g, int i, int j) + { + g.setPaint(roadColor); + g.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } + + @Override + protected void GenerateMap() +{ + _map = new int[100][100]; + _size_x = (float)_width / _map.length; + _size_y = (float)_height / _map[0].length; + + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + + + for(int i = 0; i < 20; ++i) + { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + + GenerateMap(x, y, _random.nextInt(13, 23)); + } +} + + private void GenerateMap(int x, int y, int depth) + { + if (depth <= 0) return; + boolean check = false; + + while (!check) + { + int deltaX = _random.nextInt(-1, 2); + int deltaY = _random.nextInt(-1, 2); + + if (x + deltaX < 0 || x + deltaX >= 100) continue; + if (y + deltaY < 0 || y + deltaY >= 100) continue; + + if (_map[y + deltaY][x + deltaX] == _barrier) depth--; + x += deltaX; + y += deltaY; + + _map[y][x] = _barrier; + + check = true; + } + + GenerateMap(x, y, depth - 1); + } +} diff --git a/SimpleMap.java b/SimpleMap.java new file mode 100644 index 0000000..1f618b8 --- /dev/null +++ b/SimpleMap.java @@ -0,0 +1,47 @@ +import java.awt.*; + +public class SimpleMap extends AbstractMap +{ + private Color barrierColor = Color.BLACK; + private Color roadColor = Color.GRAY; + + @Override + protected void DrawBarrierPart(Graphics2D g, int i, int j) + { + g.setPaint(barrierColor); + g.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } + + @Override + protected void DrawRoadPart(Graphics2D g, int i, int j) + { + g.setPaint(roadColor); + g.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } + + @Override + protected void GenerateMap() + { + _map = new int[100][100]; + _size_x = (float)_width / _map.length; + _size_y = (float)_height / _map[0].length; + int counter = 0; + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + while (counter < 50) + { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + counter++; + } + } + } +} diff --git a/formMap.form b/formMap.form new file mode 100644 index 0000000..06b5494 --- /dev/null +++ b/formMap.form @@ -0,0 +1,173 @@ + +
diff --git a/formMap.java b/formMap.java new file mode 100644 index 0000000..62617c7 --- /dev/null +++ b/formMap.java @@ -0,0 +1,119 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +public class formMap implements Form { + private JPanel DrawPlace; + private JLabel weightLabel; + private JLabel speedLabel; + private JLabel colorLabel; + private JButton createButton; + private JButton downButton; + private JButton leftButton; + private JButton rightButton; + private JButton upButton; + private JComboBox comboBoxSelectorMap; + private JPanel mainPanel; + private JButton modifiedButton; + + private Canvas canv = new Canvas(this); + private DrawingAircraft _aircraft; + private AbstractMap _abstractMap = new SimpleMap(); + private JFrame jframe = getFrame(); + + public formMap() { + } + + private JFrame getFrame() { + JFrame frame = new JFrame(); + frame.setVisible(true); + frame.setBounds(300, 100, 800, 600); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + return frame; + } + + private void SetData(DrawingAircraft aircraft) + { + Color bodyColor = _aircraft.AirFighter.BodyColor; + String colorString = "(" + bodyColor.getRed() + ", " + bodyColor.getGreen() + ", " + bodyColor.getBlue() + ")"; + + speedLabel.setText("Скорость: " + _aircraft.AirFighter.Speed + " "); + weightLabel.setText("Вес: " + _aircraft.AirFighter.Weight + " "); + colorLabel.setText("Цвет: " + colorString); + + Dimension canvSize = canv.getSize(); + + _abstractMap.CreateMap(canvSize.width, canvSize.height, new DrawingObjectAircraft(aircraft)); + } + + public void run() { + jframe.add(mainPanel); + DrawPlace.add(canv); + + comboBoxSelectorMap.addActionListener(e -> { + String selectedItem = comboBoxSelectorMap.getSelectedItem().toString(); + switch(selectedItem) { + case "Простая карта": + _abstractMap = new SimpleMap(); + break; + case "Моя карта": + _abstractMap = new MyMap(); + break; + } + }); + + createButton.addActionListener(e -> { + Random rnd = new Random(); + + _aircraft = new DrawingAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))); + + SetData(_aircraft); + canv.repaint(); + }); + + modifiedButton.addActionListener(e -> { + Random rnd = new Random(); + + _aircraft = new DrawingModernAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + rnd.nextInt(0, 2) == 1, rnd.nextInt(0, 2) == 1); + + + SetData(_aircraft); + canv.repaint(); + }); + + downButton.addActionListener(e -> { + if(_abstractMap == null) return; + _abstractMap.MoveObject(Direction.Down); + canv.repaint(); + }); + + upButton.addActionListener(e -> { + if(_abstractMap == null) return; + _abstractMap.MoveObject(Direction.Up); + canv.repaint(); + }); + + leftButton.addActionListener(e -> { + if(_abstractMap == null) return; + _abstractMap.MoveObject(Direction.Left); + canv.repaint(); + }); + + rightButton.addActionListener(e -> { + if(_abstractMap == null) return; + _abstractMap.MoveObject(Direction.Right); + canv.repaint(); + }); + } + + @Override + public void Draw(Graphics2D g) { + _abstractMap.DrawMapWithObject(g); + } +} -- 2.25.1