From 1095fca9e86af573e4d3f2e00d87035ad10b4308 Mon Sep 17 00:00:00 2001 From: Safgerd Date: Mon, 7 Nov 2022 00:26:51 +0400 Subject: [PATCH] =?UTF-8?q?LabWork02:=20=D0=91=D0=B0=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=87=D0=B0=D1=81=D1=82=D1=8C=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AbstractMap.java | 117 ++++++++++++++++++++++++++++++ Direction.java | 3 +- DrawningObjectExcavator.java | 38 ++++++++++ DrawningRollers.java | 4 +- DrawningTrackedVehicle.java | 57 +++++++++++++++ DrawningTracktor.java | 34 ++++++--- DumpMap.java | 58 +++++++++++++++ EntityTrackedVehicle.java | 29 ++++++++ EntityTracktor.java | 2 +- FormMap.form | 137 +++++++++++++++++++++++++++++++++++ FormMap.java | 118 ++++++++++++++++++++++++++++++ FormTracktor.form | 10 ++- FormTracktor.java | 41 +++++++---- IDrawningObject.java | 9 +++ Program.java | 6 +- SimpleMap.java | 40 ++++++++++ 16 files changed, 670 insertions(+), 33 deletions(-) create mode 100644 AbstractMap.java create mode 100644 DrawningObjectExcavator.java create mode 100644 DrawningTrackedVehicle.java create mode 100644 DumpMap.java create mode 100644 EntityTrackedVehicle.java create mode 100644 FormMap.form create mode 100644 FormMap.java create mode 100644 IDrawningObject.java create mode 100644 SimpleMap.java diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..fd5bf05 --- /dev/null +++ b/AbstractMap.java @@ -0,0 +1,117 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; + +public abstract class AbstractMap { + private IDrawningObject _drawningObject = null; + protected int[][] _map = null; + protected int _width; + protected int _height; + protected float _size_x; + protected float _size_y; + protected final Random _random = new Random(); + protected final int _freeRoad = 0; + protected final int _barrier = 1; + + public Image CreateMap(int width, int height, IDrawningObject drawningObject) + { + _width = width; + _height = height; + _drawningObject = drawningObject; + do { + GenerateMap(); + } while (!SetObjectOnMap()); + return DrawMapWithObject(); + } + + public Image MoveObject(Direction direction) + { + _drawningObject.moveObject(direction); + if (objectIntersects()) { + switch (direction) { + case Left -> _drawningObject.moveObject(Direction.Right); + case Right -> _drawningObject.moveObject(Direction.Left); + case Up -> _drawningObject.moveObject(Direction.Down); + case Down -> _drawningObject.moveObject(Direction.Up); + } + } + return DrawMapWithObject(); + } + + private boolean SetObjectOnMap() + { + if (_drawningObject == null || _map == null) + { + return false; + } + + int x = _random.nextInt(0, 10); + int y = _random.nextInt(0, 10); + _drawningObject.setObject(x, y, _width, _height); + + var Location = _drawningObject.getCurrentPosition(); + + if (objectIntersects()){ + int startLocX = (int)(Location[0] / _size_x); + int startLocY = (int)(Location[2] / _size_y); + int endLocX = (int)(Location[1] / _size_x); + int endLocY = (int)(Location[3] / _size_y); + for (int j = startLocX; j <= endLocX; j++) + { + for (int i = startLocY; i <= endLocY; i++) + { + if (_map[i][j] == _barrier) _map[i][j] = _freeRoad; + } + } + } + + return true; + } + + private boolean objectIntersects() { + float[] location = _drawningObject.getCurrentPosition(); + Rectangle self = new Rectangle((int) location[0], (int) location[2], (int) location[1] - (int) location[0], (int) location[3] - (int) location[2]); + for (int i = 0; i < _map.length; i++) + { + for (int j = 0; j < _map[i].length; j++) + { + if (_map[i][j] == _barrier) + { + if (self.intersects(new Rectangle(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y))) + { + return true; + } + } + } + } + + return false; + } + + private Image DrawMapWithObject() { + Image img = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_ARGB); + if (_drawningObject == null || _map == null) { + return img; + } + Graphics2D g = (Graphics2D) img.getGraphics(); + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[i].length; ++j) + { + if (_map[i][j] == _freeRoad) + { + DrawRoadPart(g, i, j); + } else if (_map[i][j] == _barrier) + { + DrawBarrierPart(g, i, j); + } + } + } + _drawningObject.drawningObject(g); + return img; + } + + 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/Direction.java b/Direction.java index a641b80..3b7253a 100644 --- a/Direction.java +++ b/Direction.java @@ -2,5 +2,6 @@ public enum Direction { Up, Down, Left, - Right + Right, + None } diff --git a/DrawningObjectExcavator.java b/DrawningObjectExcavator.java new file mode 100644 index 0000000..db2e26a --- /dev/null +++ b/DrawningObjectExcavator.java @@ -0,0 +1,38 @@ +import javax.print.DocFlavor; +import java.awt.*; + +public class DrawningObjectExcavator implements IDrawningObject{ + private DrawningTracktor _tracktor = null; + + public DrawningObjectExcavator(DrawningTracktor tracktor) { + this._tracktor = tracktor; + } + + public float getStep() { + if (_tracktor != null && _tracktor.Tracktor != null) { + return _tracktor.Tracktor.getStep(); + } + return 0; + } + + public float[] getCurrentPosition() { + if (_tracktor != null) { + return _tracktor.getCurrentPosition(); + } + return new float[] { 0, 0, 0, 0 }; + } + + public void moveObject(Direction direction) { + if (_tracktor != null) { + _tracktor.MoveTransport(direction); + } + } + + public void setObject(int x, int y, int width, int height) { + _tracktor.SetPosition(x, y, width, height); + } + + public void drawningObject(Graphics2D g) { + _tracktor.DrawTransport(g); + } +} diff --git a/DrawningRollers.java b/DrawningRollers.java index 10d8a2e..f3b8a41 100644 --- a/DrawningRollers.java +++ b/DrawningRollers.java @@ -14,12 +14,12 @@ public class DrawningRollers { } } - public void Init(int count, Color colorRollers){ + public DrawningRollers(int count, Color colorRollers){ setRollersCount(count); this.colorRollers = colorRollers; } - public void DrawRollers(Graphics g, float _startPosX, float _startPosY){ + public void DrawRollers(Graphics2D g, float _startPosX, float _startPosY){ Color penColor = Color.BLACK; Color mainColor = colorRollers==null ? Color.LIGHT_GRAY : colorRollers; diff --git a/DrawningTrackedVehicle.java b/DrawningTrackedVehicle.java new file mode 100644 index 0000000..29d692b --- /dev/null +++ b/DrawningTrackedVehicle.java @@ -0,0 +1,57 @@ +import java.awt.*; + +public class DrawningTrackedVehicle extends DrawningTracktor { + public DrawningTrackedVehicle(int speed, float weight, Color bodyColor, int countRollers, Color dopColor, boolean bucket, boolean supports){ + super(speed, weight, bodyColor, countRollers, 130, 87); + Tracktor = new EntityTrackedVehicle(speed, weight, bodyColor, dopColor, bucket, supports); + } + + @Override + public void DrawTransport(Graphics2D g){ + if (!(Tracktor instanceof EntityTrackedVehicle trackedVehicle)) + { + return; + } + Color pen; + Color dopBrush = trackedVehicle.getDopColor(); + if (trackedVehicle.getBucket()) + { + pen = trackedVehicle.getDopColor(); + g.setStroke(new BasicStroke(5)); + g.setColor(pen); + g.drawLine((int)_startPosX + 1, (int)_startPosY + 90, (int)_startPosX + 15, (int)_startPosY + 70); + g.drawLine((int)_startPosX + 15, (int)_startPosY + 72, (int)_startPosX + 15, (int)_startPosY + 50); + g.drawLine((int)_startPosX + 15, (int)_startPosY + 52, (int)_startPosX + 10, (int)_startPosY + 45); + g.drawLine((int)_startPosX + 15, (int)_startPosY + 60, (int)_startPosX + 40, (int)_startPosY + 50); + g.setStroke(new BasicStroke(1)); + } + _startPosX += 20; + _startPosY += 5; + super.DrawTransport(g); + _startPosX -= 20; + _startPosY -= 5; + if (trackedVehicle.getSupports()) + { + pen = Color.BLACK; + g.setColor(dopBrush); + g.fillRect((int)_startPosX + 100, (int)_startPosY + 50, 10, 42); + g.setColor(pen); + g.drawRect((int)_startPosX + 100, (int)_startPosY + 50, 10, 42); + + g.setColor(dopBrush); + g.fillRect((int)_startPosX + 90, (int)_startPosY + 82, 30, 10); + g.setColor(pen); + g.drawRect((int)_startPosX + 90, (int)_startPosY + 82, 30, 10); + + g.setColor(dopBrush); + g.fillRect((int)_startPosX + 45, (int)_startPosY + 50, 10, 42); + g.setColor(pen); + g.drawRect((int)_startPosX + 45, (int)_startPosY + 50, 10, 42); + + g.setColor(dopBrush); + g.fillRect((int)_startPosX + 35, (int)_startPosY + 82, 30, 10); + g.setColor(pen); + g.drawRect((int)_startPosX + 35, (int)_startPosY + 82, 30, 10); + } + } +} diff --git a/DrawningTracktor.java b/DrawningTracktor.java index 81d5ab1..7deb577 100644 --- a/DrawningTracktor.java +++ b/DrawningTracktor.java @@ -2,26 +2,30 @@ import java.awt.*; // Класс, отвечающий за прорисовку и перемещение объекта-сущности public class DrawningTracktor { - private EntityTracktor Tracktor; // Класс-сущность + protected EntityTracktor Tracktor; // Класс-сущность public EntityTracktor getTracktor(){ return Tracktor; } - private float _startPosX; // Левая координата отрисовки трактора - private float _startPosY; // Верхняя кооридната отрисовки трактора + protected float _startPosX; // Левая координата отрисовки трактора + protected float _startPosY; // Верхняя кооридната отрисовки трактора private Integer _pictureWidth = null; // Ширина окна отрисовки private Integer _pictureHeight = null; // Высота окна отрисовки - private final int _tracktorWidth = 110; // Ширина отрисовки трактора - private final int _tracktorHeight = 87; // Высота отрисовки трактора + protected int _tracktorWidth = 110; // Ширина отрисовки трактора + protected int _tracktorHeight = 87; // Высота отрисовки трактора private DrawningRollers drawningRollers; // Инициализация свойств - public void Init(int speed, float weight, Color bodyColor, int countRollers) + public DrawningTracktor(int speed, float weight, Color bodyColor, int countRollers) { - Tracktor = new EntityTracktor(); - Tracktor.Init(speed, weight, bodyColor); - drawningRollers = new DrawningRollers(); - drawningRollers.Init(countRollers, bodyColor); + Tracktor = new EntityTracktor(speed, weight, bodyColor); + drawningRollers = new DrawningRollers(countRollers, bodyColor); + } + + protected DrawningTracktor(int speed, float weight, Color bodyColor, int countRollers, int tracktorWidth, int tracktorHeight){ + this(speed, weight, bodyColor, countRollers); + _tracktorWidth = tracktorWidth; + _tracktorHeight = tracktorHeight; } // Установка позиции Трактора @@ -81,7 +85,7 @@ public class DrawningTracktor { } // Отрисовка Трактора - public void DrawTransport(Graphics g) + public void DrawTransport(Graphics2D g) { if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) { @@ -157,4 +161,12 @@ public class DrawningTracktor { _startPosY = _pictureHeight - _tracktorHeight; } } + + public float[] getCurrentPosition() { + return new float[] { _startPosX, _startPosX + _tracktorWidth - 1, _startPosY, _startPosY + _tracktorHeight - 1 }; + // Left - 0 + // Right - 1 + // Top - 2 + // Bottom - 3 + } } diff --git a/DumpMap.java b/DumpMap.java new file mode 100644 index 0000000..b8c2255 --- /dev/null +++ b/DumpMap.java @@ -0,0 +1,58 @@ +import java.awt.*; + +public class DumpMap extends AbstractMap{ + private final Color barrierColor = new Color(165,42,42); + private final Color roadColor = Color.LIGHT_GRAY; + + @Override + protected void DrawBarrierPart(Graphics2D g, int j, int i) + { + g.setColor(barrierColor); + g.fillRect(i * (int)_size_x, j * (int)_size_y, (int)_size_x, (int)_size_y); + } + + @Override + protected void DrawRoadPart(Graphics2D g, int j, int i) + { + g.setColor(roadColor); + g.fillRect(i * (int)_size_x, j * (int)_size_y, (int)_size_x, (int)_size_y); + } + + @Override + protected void GenerateMap() + { + _map = new int[100][100]; + _size_x = (float)_width / _map[0].length; + _size_y = (float)_height / _map.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 < 20) + { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[y][x] == _freeRoad && x > 1 && x < 98 && y > 1 && y < 98) + { + _map[y][x] = _barrier; + _map[y][x + 1] = _barrier; + _map[y][x + 2] = _barrier; + _map[y][x - 1] = _barrier; + _map[y][x - 2] = _barrier; + _map[y + 1][x] = _barrier; + _map[y + 2][x] = _barrier; + _map[y - 1][x] = _barrier; + _map[y - 2][x] = _barrier; + _map[y + 1][x + 1] = _barrier; + _map[y - 1][x + 1] = _barrier; + _map[y + 1][x - 1] = _barrier; + _map[y - 1][x - 1] = _barrier; + counter++; + } + } + } +} diff --git a/EntityTrackedVehicle.java b/EntityTrackedVehicle.java new file mode 100644 index 0000000..1f63853 --- /dev/null +++ b/EntityTrackedVehicle.java @@ -0,0 +1,29 @@ +import java.awt.*; + +public class EntityTrackedVehicle extends EntityTracktor { + // Дополнительный цвет + private Color dopColor; + // Признак наличия ковша + private boolean bucket; + // Признак наличия опор + private boolean supports; + + public EntityTrackedVehicle(int speed, float weight, Color bodyColor, Color dopColor, boolean bucket, boolean supports){ + super(speed, weight, bodyColor); + this.dopColor = dopColor; + this.bucket = bucket; + this.supports = supports; + } + + public Color getDopColor(){ + return dopColor; + } + + public boolean getBucket(){ + return bucket; + } + + public boolean getSupports(){ + return supports; + } +} diff --git a/EntityTracktor.java b/EntityTracktor.java index f6c85e9..37314f9 100644 --- a/EntityTracktor.java +++ b/EntityTracktor.java @@ -21,7 +21,7 @@ public class EntityTracktor { } // Инициализация полей объекта-класса Трактора - public void Init(int speed, float weight, Color bodyColor) + public EntityTracktor(int speed, float weight, Color bodyColor) { Random rnd = new Random(); Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed; diff --git a/FormMap.form b/FormMap.form new file mode 100644 index 0000000..29f645b --- /dev/null +++ b/FormMap.form @@ -0,0 +1,137 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormMap.java b/FormMap.java new file mode 100644 index 0000000..52c9dde --- /dev/null +++ b/FormMap.java @@ -0,0 +1,118 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.awt.event.ItemEvent; +import java.util.Random; + +public class FormMap extends JFrame { + private JPanel ContentPanel; + private JButton buttonCreate; + private JLabel speedLabel; + private JLabel weightLabel; + private JLabel colorLabel; + private JButton buttonLeft; + private JButton buttonDown; + private JButton buttonRight; + private JButton buttonUp; + private JPanel pictureBox; + private JButton buttonCreateModif; + private JComboBox comboBoxMapSelector; + + private AbstractMap _abstractMap; + private Image imageBuffer; + + public FormMap(){ + setTitle("Трактор"); + setContentPane(ContentPanel); + setSize(800, 500); + _abstractMap = new SimpleMap(); + + // Обработка нажатия кнопки "Создать" + buttonCreate.addActionListener(e->{ + Random rnd = new Random(); + var _tracktor = new DrawningTracktor( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + rnd.nextInt(3,8) + ); + setData(_tracktor); + }); + + // Обработка нажатия кнопки "Модификация" + buttonCreateModif.addActionListener(e->{ + Random rnd = new Random(); + var _tracktor = new DrawningTrackedVehicle( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + rnd.nextInt(3,8), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + rnd.nextBoolean(), + rnd.nextBoolean() + ); + setData(_tracktor); + }); + + buttonUp.addActionListener(e->{ + if (_abstractMap != null){ + imageBuffer = _abstractMap.MoveObject(Direction.Up); + repaint(); + } + }); + + buttonLeft.addActionListener(e->{ + if (_abstractMap != null){ + imageBuffer = _abstractMap.MoveObject(Direction.Left); + repaint(); + } + }); + + buttonDown.addActionListener(e->{ + if (_abstractMap != null){ + imageBuffer = _abstractMap.MoveObject(Direction.Down); + repaint(); + } + }); + + buttonRight.addActionListener(e->{ + if (_abstractMap != null){ + imageBuffer = _abstractMap.MoveObject(Direction.Right); + repaint(); + } + }); + + comboBoxMapSelector.addItemListener(e->{ + if (e.getStateChange()== ItemEvent.SELECTED){ + switch (e.getItem().toString()) { + case "Простая карта" -> _abstractMap = new SimpleMap(); + case "Свалка карта" -> _abstractMap = new DumpMap(); + } + + repaint(); + } + }); + } + + private void setData(DrawningTracktor _tracktor) { + Random rnd = new Random(); + _tracktor.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight()); + speedLabel.setText("Скорость: " + _tracktor.getTracktor().getSpeed()); + weightLabel.setText("Вес: " + _tracktor.getTracktor().getWeight()); + colorLabel.setText("Цвет: " + String.format("%h",_tracktor.getTracktor().getBodyColor())); + imageBuffer = _abstractMap.CreateMap(pictureBox.getWidth(), pictureBox.getHeight(), new DrawningObjectExcavator(_tracktor)); + + repaint(); + } + + @Override + public void paint(Graphics g){ + super.paint(g); + Graphics2D g2d = (Graphics2D)pictureBox.getGraphics(); + if (imageBuffer != null){ + pictureBox.paintComponents(imageBuffer.getGraphics()); + g2d.drawImage(imageBuffer,0,0,null); + } + } +} diff --git a/FormTracktor.form b/FormTracktor.form index d6e43fa..8a2e7dd 100644 --- a/FormTracktor.form +++ b/FormTracktor.form @@ -18,7 +18,7 @@ - + @@ -102,6 +102,14 @@ + + + + + + + + diff --git a/FormTracktor.java b/FormTracktor.java index 74b9158..126ac7d 100644 --- a/FormTracktor.java +++ b/FormTracktor.java @@ -15,6 +15,7 @@ public class FormTracktor extends JFrame { private JButton buttonRight; private JButton buttonUp; private JPanel pictureBox; + private JButton buttonCreateModif; private DrawningTracktor _tracktor; @@ -26,25 +27,28 @@ public class FormTracktor extends JFrame { // Обработка нажатия кнопки "Создать" buttonCreate.addActionListener(e->{ Random rnd = new Random(); - _tracktor = new DrawningTracktor(); - - _tracktor.Init( + _tracktor = new DrawningTracktor( rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), rnd.nextInt(3,8) ); + setData(); + }); - _tracktor.SetPosition( - rnd.nextInt(10, 100), - rnd.nextInt(10, 100), - pictureBox.getWidth(), pictureBox.getHeight() + // Обработка нажатия кнопки "Модификация" + buttonCreateModif.addActionListener(e->{ + Random rnd = new Random(); + _tracktor = new DrawningTrackedVehicle( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + rnd.nextInt(3,8), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + rnd.nextBoolean(), + rnd.nextBoolean() ); - - speedLabel.setText("Скорость: " + _tracktor.getTracktor().getSpeed()); - weightLabel.setText("Вес: " + _tracktor.getTracktor().getWeight()); - colorLabel.setText("Цвет: " + String.format("%h",_tracktor.getTracktor().getBodyColor())); - repaint(); + setData(); }); buttonUp.addActionListener(e->{ @@ -87,12 +91,21 @@ public class FormTracktor extends JFrame { }); } + private void setData() { + Random rnd = new Random(); + _tracktor.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight()); + speedLabel.setText("Скорость: " + _tracktor.getTracktor().getSpeed()); + weightLabel.setText("Вес: " + _tracktor.getTracktor().getWeight()); + colorLabel.setText("Цвет: " + String.format("%h",_tracktor.getTracktor().getBodyColor())); + repaint(); + } + @Override public void paint(Graphics g){ super.paint(g); - g = pictureBox.getGraphics(); + Graphics2D g2d = (Graphics2D)pictureBox.getGraphics(); if (_tracktor != null){ - _tracktor.DrawTransport(g); + _tracktor.DrawTransport(g2d); } } } diff --git a/IDrawningObject.java b/IDrawningObject.java new file mode 100644 index 0000000..d1648a1 --- /dev/null +++ b/IDrawningObject.java @@ -0,0 +1,9 @@ +import java.awt.*; + +public interface IDrawningObject { + float getStep(); + void setObject(int x, int y, int width, int height); + void moveObject(Direction direction); + void drawningObject(Graphics2D g); + float[] getCurrentPosition(); +} diff --git a/Program.java b/Program.java index 3f5ddf6..339ae6b 100644 --- a/Program.java +++ b/Program.java @@ -2,8 +2,8 @@ import javax.swing.*; public class Program { public static void main(String[] args){ - FormTracktor formTracktor = new FormTracktor(); - formTracktor.setVisible(true); - formTracktor.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + FormMap formMap = new FormMap(); + formMap.setVisible(true); + formMap.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } } diff --git a/SimpleMap.java b/SimpleMap.java new file mode 100644 index 0000000..346f038 --- /dev/null +++ b/SimpleMap.java @@ -0,0 +1,40 @@ +import java.awt.*; +import java.util.Arrays; + +public class SimpleMap extends AbstractMap{ + private final Color barrierColor = Color.BLACK; + private final Color roadColor = Color.GRAY; + + @Override + protected void DrawBarrierPart(Graphics2D g, int i, int j) { + g.setColor(barrierColor); + g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y); + } + + @Override + protected void DrawRoadPart(Graphics2D g, int i, int j) { + g.setColor(roadColor); + g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y); + } + + @Override + protected void GenerateMap() { + _map = new int[100][100]; + _size_x = (float)_width / _map[0].length; + _size_y = (float)_height / _map.length; + int counter = 0; + for (int[] row : _map) { + Arrays.fill(row, _freeRoad); + } + while (counter < 50) + { + int x = _random.nextInt(0, (int)_map[0].length); + int y = _random.nextInt(0, (int)_map.length); + if (_map[y][x] == _freeRoad) + { + _map[y][x] = _barrier; + counter++; + } + } + } +}