From 885fa94fd3562c4b840c3af4dce6bf1b56d7da44 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 17:10:11 +0400 Subject: [PATCH] IDrawningObject & AbstractMap created & fixed for Java --- AbstractMap.java | 163 +++++++++++++++++++++++++++++++++++++++++++ IDrawningObject.java | 17 +++++ 2 files changed, 180 insertions(+) create mode 100644 AbstractMap.java create mode 100644 IDrawningObject.java diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..f56d3ae --- /dev/null +++ b/AbstractMap.java @@ -0,0 +1,163 @@ +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; + + protected abstract void GenerateMap(); + protected abstract void DrawRoadPart(Graphics g, int i, int j); + protected abstract void DrawBarrierPart(Graphics g, int i, int j); + + public BufferedImage CreateMap(int width, int height, IDrawningObject drawningObject) + { + _width = width; + _height = height; + _drawningObject = drawningObject; + GenerateMap(); // abstract void + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + + public BufferedImage MoveObject(Direction direction) + { + boolean isFree = true; + + int startPosX = (int)(_drawningObject.GetCurrentPosition()[3] / _size_x); + int startPosY = (int)(_drawningObject.GetCurrentPosition()[0] / _size_y); + + int objectWidth = (int)(_drawningObject.GetCurrentPosition()[1] / _size_x); + int objectHeight = (int)(_drawningObject.GetCurrentPosition()[2] / _size_y); + + switch (direction) + { + case Right: + for (int i = objectWidth; i <= objectWidth + (int)(_drawningObject.getStep() / _size_x); i++) + { + for (int j = startPosY; j <= objectHeight; j++) + { + if (_map[i][j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + + case Left: + for (int i = startPosX; i >= (int)(_drawningObject.getStep() / _size_x); i--) + { + for (int j = startPosY; j <= objectHeight; j++) + { + if (_map[i][j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + + case Up: + for (int i = startPosX; i <= objectWidth; i++) + { + for (int j = startPosY; j >= (int)(_drawningObject.getStep() / _size_y); j--) + { + if (_map[i][j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + + case Down: + for (int i = startPosX; i <= objectWidth; i++) + { + for (int j = objectHeight; j <= objectHeight + (int)(_drawningObject.getStep() / _size_y); j++) + { + if (_map[i][j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + } + if (isFree) + { + _drawningObject.MoveObject(direction); + } + return DrawMapWithObject(); + } + + private boolean SetObjectOnMap() + { + if (_drawningObject == null || _map == null) + { + return false; + } + int x = _random.nextInt( 10); + int y = _random.nextInt( 10); + _drawningObject.SetObject(x, y, _width, _height); + + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + if (i * _size_x >= x && j * _size_y >= y && + i * _size_x <= x + _drawningObject.GetCurrentPosition()[1] && + j * _size_y <= j + _drawningObject.GetCurrentPosition()[2]) + { + if (_map[i][j] == _barrier) + { + return false; + } + } + } + } + + return true; + } + + private BufferedImage DrawMapWithObject() + { + BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB); + if (_drawningObject == null || _map == null) + { + return bmp; + } + Graphics gr = bmp.getGraphics(); + 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); + return bmp; + } + +} diff --git a/IDrawningObject.java b/IDrawningObject.java new file mode 100644 index 0000000..80b49ff --- /dev/null +++ b/IDrawningObject.java @@ -0,0 +1,17 @@ +import java.awt.*; +public interface IDrawningObject { + /// Шаг перемещения объекта + float getStep(); + /// Установка позиции объекта + void SetObject(int x, int y, int width, int height); + /// Изменение направления перемещения объекта + void MoveObject(Direction direction); + /// Отрисовка объекта + void DrawningObject(Graphics g); + /// Получение текущей позиции объекта + float[] GetCurrentPosition(); + //0 - up + //1 - right + //2 - down + //3 - left +}