From 95edf9e7d23684bbf836899c5fce4e0cb8867d14 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 21 Oct 2022 16:24:45 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20MapWithSetLocom?= =?UTF-8?q?otivesGeneric?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MapWithSetLocomotivesGeneric.java | 142 ++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 MapWithSetLocomotivesGeneric.java diff --git a/MapWithSetLocomotivesGeneric.java b/MapWithSetLocomotivesGeneric.java new file mode 100644 index 0000000..3559e35 --- /dev/null +++ b/MapWithSetLocomotivesGeneric.java @@ -0,0 +1,142 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetLocomotivesGeneric + +{ + /// Ширина окна отрисовки + private final int _pictureWidth; + /// Высота окна отрисовки + private final int _pictureHeight; + /// Размер занимаемого объектом места (ширина) + private final int _placeSizeWidth = 210; + /// Размер занимаемого объектом места (высота) + private final int _placeSizeHeight = 90; + + /// Набор объектов + private final SetLocomotivesGeneric _setLocomotives; + /// Карта + private final U _map; + /// Конструктор + public MapWithSetLocomotivesGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setLocomotives = new SetLocomotivesGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + /// Добавление + public boolean Plus(MapWithSetLocomotivesGeneric map, T locomotive) + { + return map._setLocomotives.Insert(locomotive); + } + /// Удаление + public boolean Minus(MapWithSetLocomotivesGeneric map, int position) + { + return map._setLocomotives.Remove(position); + } + + /// Вывод всего набора объектов + public BufferedImage ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + DrawBackground(gr); + DrawLocomotives(gr); + return bmp; + } + + /// Просмотр объекта на карте + public BufferedImage ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setLocomotives.Count(); i++) + { + var locomotive = _setLocomotives.Get(i); + if (locomotive != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive); + } + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + } + + /// Перемещение объекта по крате + public BufferedImage MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + } + + /// "Взбалтываем" набор, чтобы все элементы оказались в начале + private void Shaking() + { + int j = _setLocomotives.Count() - 1; + for (int i = 0; i < _setLocomotives.Count(); i++) + { + if (_setLocomotives.Get(i) == null) + { + for (; j > i; j--) + { + var locomotive = _setLocomotives.Get(j); + if (locomotive != null) + { + _setLocomotives.Insert(locomotive, i); + _setLocomotives.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + /// Метод отрисовки фона + private void DrawBackground(Graphics g) + { + g.setColor(Color.BLACK); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { //линия разметки места + g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * + _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, + (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + + /// Метод прорисовки объектов + private void DrawLocomotives(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int curWidth = 0; + int curHeight = 0; + + for (int i = 0; i < _setLocomotives.Count(); i++) + { + // установка позиции + if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).SetObject(curWidth * _placeSizeWidth, curHeight * _placeSizeHeight, _pictureWidth, _pictureHeight); + if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).DrawningObject(g); + if (curWidth < width) curWidth++; + else + { + curWidth = 0; + curHeight++; + } + } + } + +}