Compare commits
No commits in common. "2d621bbe701e359804d5c68ec2d31f14326f920f" and "80e51d1ecff80e8e333b752899d1a83235a72bd9" have entirely different histories.
2d621bbe70
...
80e51d1ecf
@ -2,9 +2,6 @@ import java.awt.*;
|
|||||||
|
|
||||||
public class DrawningObjectLocomotive implements IDrawningObject {
|
public class DrawningObjectLocomotive implements IDrawningObject {
|
||||||
private DrawningLocomotive _locomotive = null;
|
private DrawningLocomotive _locomotive = null;
|
||||||
public DrawningLocomotive GetDrawningLocomotive() {
|
|
||||||
return _locomotive;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DrawningObjectLocomotive(DrawningLocomotive locomotive)
|
public DrawningObjectLocomotive(DrawningLocomotive locomotive)
|
||||||
{
|
{
|
||||||
|
@ -8,11 +8,6 @@ public class FormLocomotive extends JComponent{
|
|||||||
public DrawningLocomotive getSelectedLocomotive() {
|
public DrawningLocomotive getSelectedLocomotive() {
|
||||||
return SelectedLocomotive;
|
return SelectedLocomotive;
|
||||||
}
|
}
|
||||||
public void SetLocomotive(DrawningObjectLocomotive locomotive){
|
|
||||||
// TODO добавлен геттер в drawningobjectlocomotive
|
|
||||||
_locomotive = locomotive.GetDrawningLocomotive();
|
|
||||||
repaint();
|
|
||||||
}
|
|
||||||
public FormLocomotive(JDialog caller) {
|
public FormLocomotive(JDialog caller) {
|
||||||
|
|
||||||
Panel statusPanel = new Panel();
|
Panel statusPanel = new Panel();
|
||||||
|
@ -5,62 +5,32 @@ import java.awt.event.ActionEvent;
|
|||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
public class FormMapWithSetLocomotives extends JComponent {
|
public class FormMapWithSetLocomotives extends JComponent {
|
||||||
private BufferedImage bufferImg = null;
|
private BufferedImage bufferImg = null;
|
||||||
/// Объект от класса карты с набором объектов
|
/// Объект от класса карты с набором объектов
|
||||||
//private MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap> _mapLocomotivesCollectionGeneric;
|
private MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap> _mapLocomotivesCollectionGeneric;
|
||||||
|
|
||||||
/// Словарь для выпадающего списка
|
|
||||||
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {
|
|
||||||
{
|
|
||||||
put("Simple Map", new SimpleMap());
|
|
||||||
put("Spike Map", new SpikeMap());
|
|
||||||
put("Rail Map", new RailMap());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
/// Объект от коллекции карт
|
|
||||||
private final MapsCollection _mapsCollection;
|
|
||||||
|
|
||||||
// Элементы на форме
|
|
||||||
JFrame formFrame;
|
|
||||||
Panel statusPanel;
|
|
||||||
TextField textFieldNewMapName;
|
|
||||||
JComboBox mapSelectComboBox;
|
|
||||||
DefaultListModel<String> mapsListModel = new DefaultListModel<>();
|
|
||||||
|
|
||||||
JScrollPane listScroller = new JScrollPane();
|
|
||||||
|
|
||||||
JList listBoxMaps;
|
|
||||||
//TODO
|
|
||||||
|
|
||||||
|
|
||||||
public FormMapWithSetLocomotives() {
|
public FormMapWithSetLocomotives() {
|
||||||
formFrame = new JFrame("Form Map With SetLocomotives");
|
JFrame formFrame = new JFrame("Form Map With SetLocomotives");
|
||||||
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
formFrame.setSize(750, 500);
|
formFrame.setSize(750, 500);
|
||||||
formFrame.setLocationRelativeTo(null);
|
formFrame.setLocationRelativeTo(null);
|
||||||
|
|
||||||
statusPanel = new Panel();
|
Panel statusPanel = new Panel();
|
||||||
statusPanel.setBackground(Color.WHITE);
|
statusPanel.setBackground(Color.WHITE);
|
||||||
statusPanel.setLayout(new GridLayout(0, 1, 20, 5));
|
statusPanel.setLayout(new GridLayout(0, 1, 20, 20));
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
add(statusPanel, BorderLayout.EAST);
|
add(statusPanel, BorderLayout.EAST);
|
||||||
|
|
||||||
// Текстовое поле для ввода названия карты
|
|
||||||
textFieldNewMapName = new TextField();
|
|
||||||
statusPanel.add(textFieldNewMapName);
|
|
||||||
|
|
||||||
// КомбоБокс с картами
|
// КомбоБокс с картами
|
||||||
String[] maps = {
|
String[] maps = {
|
||||||
"Simple Map",
|
"Simple Map",
|
||||||
"Spike Map",
|
"Spike Map",
|
||||||
"Rail Map"
|
"Rail Map"
|
||||||
};
|
};
|
||||||
mapSelectComboBox = new JComboBox(maps);
|
JComboBox mapSelectComboBox = new JComboBox(maps);
|
||||||
mapSelectComboBox.setEditable(true);
|
mapSelectComboBox.setEditable(true);
|
||||||
/*mapSelectComboBox.addActionListener(e -> {
|
mapSelectComboBox.addActionListener(e -> {
|
||||||
AbstractMap map = null;
|
AbstractMap map = null;
|
||||||
String item = (String)mapSelectComboBox.getSelectedItem();
|
String item = (String)mapSelectComboBox.getSelectedItem();
|
||||||
switch (item) {
|
switch (item) {
|
||||||
@ -83,74 +53,14 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
{
|
{
|
||||||
_mapLocomotivesCollectionGeneric = null;
|
_mapLocomotivesCollectionGeneric = null;
|
||||||
}
|
}
|
||||||
});*/
|
});
|
||||||
statusPanel.add(mapSelectComboBox);
|
statusPanel.add(mapSelectComboBox);
|
||||||
|
|
||||||
// Initialization
|
|
||||||
// TODO
|
|
||||||
_mapsCollection = new MapsCollection(600, 500);
|
|
||||||
mapSelectComboBox.removeAllItems();
|
|
||||||
for (var elem : _mapsDict.keySet())
|
|
||||||
{
|
|
||||||
mapSelectComboBox.addItem(elem);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Кнопка добавления карты
|
|
||||||
JButton addMapButton = new JButton("Add Map");
|
|
||||||
addMapButton.addActionListener(e -> {
|
|
||||||
// логика добавления
|
|
||||||
if (mapSelectComboBox.getSelectedIndex() == -1 || textFieldNewMapName.getText() == null)
|
|
||||||
{
|
|
||||||
//MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
||||||
JOptionPane.showMessageDialog(null, "Not all data is complete!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
//TODO фиг знает как этот комбобокс работает, может не найдет карту
|
|
||||||
if (!_mapsDict.containsKey(mapSelectComboBox.getSelectedItem().toString()))
|
|
||||||
{
|
|
||||||
JOptionPane.showMessageDialog(null, "No such map");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_mapsCollection.AddMap(textFieldNewMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString()));
|
|
||||||
ReloadMaps();
|
|
||||||
});
|
|
||||||
statusPanel.add(addMapButton);
|
|
||||||
|
|
||||||
// ListBox для созданных карт
|
|
||||||
listBoxMaps = new JList(mapsListModel);
|
|
||||||
listBoxMaps.addListSelectionListener(e -> {
|
|
||||||
//TODO фиг знает как этот лист работает, может не вернет ничего
|
|
||||||
if(listBoxMaps.getSelectedValue() == null) return;
|
|
||||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
|
||||||
repaint();
|
|
||||||
});
|
|
||||||
statusPanel.add(listBoxMaps);
|
|
||||||
|
|
||||||
listScroller.setViewportView(listBoxMaps);
|
|
||||||
listBoxMaps.setLayoutOrientation(JList.VERTICAL);
|
|
||||||
statusPanel.add(listScroller);
|
|
||||||
|
|
||||||
// Кнопка для удаления карты
|
|
||||||
JButton deleteMapButton = new JButton("Delete Map");
|
|
||||||
deleteMapButton.addActionListener(e -> {
|
|
||||||
// логика удаления
|
|
||||||
if (listBoxMaps.getSelectedIndex() == -1)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
//TODO фиг знает как этот лист работает, может не вернет ничего
|
|
||||||
if(listBoxMaps.getSelectedValue().toString() == null) return;
|
|
||||||
_mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString());
|
|
||||||
ReloadMaps();
|
|
||||||
repaint();
|
|
||||||
});
|
|
||||||
statusPanel.add(deleteMapButton);
|
|
||||||
|
|
||||||
// Кнопка добавления локомотива
|
// Кнопка добавления локомотива
|
||||||
JButton addLocomotiveButton = new JButton("Add Locomotive");
|
JButton addLocomotiveButton = new JButton("Add Locomotive");
|
||||||
addLocomotiveButton.addActionListener(e -> {
|
addLocomotiveButton.addActionListener(e -> {
|
||||||
// логика добавления
|
// логика добавления
|
||||||
if (listBoxMaps.getSelectedIndex() == -1)
|
if (_mapLocomotivesCollectionGeneric == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -161,10 +71,9 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
||||||
dialog.setVisible(true);
|
dialog.setVisible(true);
|
||||||
DrawningObjectLocomotive locomotive = new DrawningObjectLocomotive(formLocomotive.getSelectedLocomotive());
|
DrawningObjectLocomotive locomotive = new DrawningObjectLocomotive(formLocomotive.getSelectedLocomotive());
|
||||||
//TODO
|
if (_mapLocomotivesCollectionGeneric.Plus(locomotive) != -1) {
|
||||||
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(locomotive)!= -1) {
|
|
||||||
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
|
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
|
||||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
bufferImg = _mapLocomotivesCollectionGeneric.ShowSet();
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -195,10 +104,9 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
|
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
|
||||||
//TODO
|
if (_mapLocomotivesCollectionGeneric.Minus(position) != null) {
|
||||||
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Minus(position) != null) {
|
|
||||||
JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION);
|
JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION);
|
||||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
bufferImg = _mapLocomotivesCollectionGeneric.ShowSet();
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
@ -210,12 +118,11 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
JButton showStorageButton = new JButton("Show Storage");
|
JButton showStorageButton = new JButton("Show Storage");
|
||||||
showStorageButton.addActionListener(e -> {
|
showStorageButton.addActionListener(e -> {
|
||||||
// логика просмотра
|
// логика просмотра
|
||||||
//TODO
|
if (_mapLocomotivesCollectionGeneric == null)
|
||||||
if (listBoxMaps.getSelectedIndex() == -1)
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
bufferImg = _mapLocomotivesCollectionGeneric.ShowSet();
|
||||||
repaint();
|
repaint();
|
||||||
});
|
});
|
||||||
statusPanel.add(showStorageButton);
|
statusPanel.add(showStorageButton);
|
||||||
@ -223,21 +130,18 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
JButton showOnMapButton = new JButton("Show On Map");
|
JButton showOnMapButton = new JButton("Show On Map");
|
||||||
showOnMapButton.addActionListener(e -> {
|
showOnMapButton.addActionListener(e -> {
|
||||||
// логика просмотра
|
// логика просмотра
|
||||||
//TODO
|
if (_mapLocomotivesCollectionGeneric == null)
|
||||||
if (listBoxMaps.getSelectedIndex() == -1)
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowOnMap();
|
bufferImg = _mapLocomotivesCollectionGeneric.ShowOnMap();
|
||||||
repaint();
|
|
||||||
});
|
});
|
||||||
statusPanel.add(showOnMapButton);
|
statusPanel.add(showOnMapButton);
|
||||||
|
|
||||||
ActionListener moveButtonListener = new ActionListener() {
|
ActionListener moveButtonListener = new ActionListener() {
|
||||||
//TODO
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if (listBoxMaps.getSelectedIndex() == -1)
|
if (_mapLocomotivesCollectionGeneric == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -258,7 +162,7 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
dir = Direction.Right;
|
dir = Direction.Right;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).MoveObject(dir);
|
bufferImg = _mapLocomotivesCollectionGeneric.MoveObject(dir);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -286,50 +190,10 @@ public class FormMapWithSetLocomotives extends JComponent {
|
|||||||
});
|
});
|
||||||
statusPanel.add(showGalleryButton);
|
statusPanel.add(showGalleryButton);
|
||||||
|
|
||||||
// Кнопка показа удаленных объектов
|
|
||||||
JButton showDeletedButton = new JButton("Show Deleted");
|
|
||||||
showDeletedButton.addActionListener(e -> {
|
|
||||||
// По отдельной кнопке вызывать форму работы с объектом (из
|
|
||||||
//первой лабораторной), передавая туда элемент из коллекции
|
|
||||||
//удаленных, если там есть
|
|
||||||
DrawningObjectLocomotive locomotive = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).getDeleted();
|
|
||||||
if (locomotive != null) {
|
|
||||||
JDialog dialog = new JDialog(formFrame, "Deleted", true);
|
|
||||||
FormLocomotive formLocomotive = new FormLocomotive(dialog);
|
|
||||||
// TODO чтобы передать локомотив в форму добавляем метод в formlocomotive
|
|
||||||
formLocomotive.SetLocomotive(locomotive);
|
|
||||||
dialog.setSize(800, 500);
|
|
||||||
dialog.setContentPane(formLocomotive);
|
|
||||||
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
|
||||||
dialog.setVisible(true);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
statusPanel.add(showDeletedButton);
|
|
||||||
|
|
||||||
formFrame.getContentPane().add(this);
|
formFrame.getContentPane().add(this);
|
||||||
formFrame.setVisible(true);
|
formFrame.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ReloadMaps(){
|
|
||||||
int index = listBoxMaps.getSelectedIndex();
|
|
||||||
listBoxMaps.removeAll();
|
|
||||||
mapsListModel.removeAllElements();
|
|
||||||
for (int i = 0; i < _mapsCollection.keys().size(); i++){
|
|
||||||
mapsListModel.addElement(_mapsCollection.keys().get(i));
|
|
||||||
}
|
|
||||||
if (mapsListModel.size() > 0 && (index == -1 || index >= mapsListModel.size())){
|
|
||||||
listBoxMaps.setSelectedIndex(0);
|
|
||||||
}
|
|
||||||
else if (mapsListModel.size() > 0 && index > -1 && index < mapsListModel.size())
|
|
||||||
{
|
|
||||||
listBoxMaps.setSelectedIndex(index);
|
|
||||||
}
|
|
||||||
listBoxMaps.setModel(mapsListModel);
|
|
||||||
statusPanel.repaint();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(Graphics g) {
|
protected void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.LinkedList;
|
|
||||||
|
|
||||||
public class MapWithSetLocomotivesGeneric
|
public class MapWithSetLocomotivesGeneric
|
||||||
<T extends IDrawningObject, U extends AbstractMap>
|
<T extends IDrawningObject, U extends AbstractMap>
|
||||||
@ -15,16 +14,7 @@ public class MapWithSetLocomotivesGeneric
|
|||||||
private final int _placeSizeHeight = 90;
|
private final int _placeSizeHeight = 90;
|
||||||
|
|
||||||
/// Набор объектов
|
/// Набор объектов
|
||||||
//TODO переделал на public, узнать так ли вообще надо?
|
private final SetLocomotivesGeneric<T> _setLocomotives;
|
||||||
public final SetLocomotivesGeneric<T> _setLocomotives;
|
|
||||||
|
|
||||||
// Список удаленных объектов
|
|
||||||
LinkedList<DrawningObjectLocomotive> deletedLocomotives = new LinkedList<>();
|
|
||||||
|
|
||||||
public DrawningObjectLocomotive getDeleted() {
|
|
||||||
if (deletedLocomotives.isEmpty()) return null;
|
|
||||||
return deletedLocomotives.removeFirst();
|
|
||||||
}
|
|
||||||
/// Карта
|
/// Карта
|
||||||
private final U _map;
|
private final U _map;
|
||||||
/// Конструктор
|
/// Конструктор
|
||||||
@ -46,10 +36,7 @@ public class MapWithSetLocomotivesGeneric
|
|||||||
/// Удаление
|
/// Удаление
|
||||||
public T Minus(int position)
|
public T Minus(int position)
|
||||||
{
|
{
|
||||||
T temp = this._setLocomotives.Remove(position);
|
return this._setLocomotives.Remove(position);
|
||||||
if (temp == null) return null;
|
|
||||||
if (temp instanceof DrawningObjectLocomotive) deletedLocomotives.add((DrawningObjectLocomotive) temp);
|
|
||||||
return temp;
|
|
||||||
}
|
}
|
||||||
/// Вывод всего набора объектов
|
/// Вывод всего набора объектов
|
||||||
public BufferedImage ShowSet()
|
public BufferedImage ShowSet()
|
||||||
@ -65,8 +52,9 @@ public class MapWithSetLocomotivesGeneric
|
|||||||
public BufferedImage ShowOnMap()
|
public BufferedImage ShowOnMap()
|
||||||
{
|
{
|
||||||
Shaking();
|
Shaking();
|
||||||
for (var locomotive : _setLocomotives.GetLocomotives())
|
for (int i = 0; i < _setLocomotives.Count(); i++)
|
||||||
{
|
{
|
||||||
|
var locomotive = _setLocomotives.Get(i);
|
||||||
if (locomotive != null)
|
if (locomotive != null)
|
||||||
{
|
{
|
||||||
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
|
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
|
||||||
@ -163,11 +151,11 @@ public class MapWithSetLocomotivesGeneric
|
|||||||
int curWidth = 0;
|
int curWidth = 0;
|
||||||
int curHeight = 0;
|
int curHeight = 0;
|
||||||
|
|
||||||
for (var locomotive : _setLocomotives.GetLocomotives())
|
for (int i = 0; i < _setLocomotives.Count(); i++)
|
||||||
{
|
{
|
||||||
// установка позиции
|
// установка позиции
|
||||||
if (locomotive != null) locomotive.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 18, _pictureWidth, _pictureHeight);
|
if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 18, _pictureWidth, _pictureHeight);
|
||||||
if (locomotive != null) locomotive.DrawningObject(g);
|
if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).DrawningObject(g);
|
||||||
if (curWidth < width) curWidth++;
|
if (curWidth < width) curWidth++;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -1,48 +0,0 @@
|
|||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
public class MapsCollection {
|
|
||||||
/// Словарь (хранилище) с картами
|
|
||||||
final HashMap<String, MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>> _mapStorages;
|
|
||||||
|
|
||||||
/// Возвращение списка названий карт
|
|
||||||
public ArrayList<String> keys() {
|
|
||||||
return new ArrayList<String>(_mapStorages.keySet());
|
|
||||||
}
|
|
||||||
/// Ширина окна отрисовки
|
|
||||||
private final int _pictureWidth;
|
|
||||||
/// Высота окна отрисовки
|
|
||||||
private final int _pictureHeight;
|
|
||||||
/// Конструктор
|
|
||||||
public MapsCollection(int pictureWidth, int pictureHeight)
|
|
||||||
{
|
|
||||||
_mapStorages = new HashMap<String, MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>>();
|
|
||||||
_pictureWidth = pictureWidth;
|
|
||||||
_pictureHeight = pictureHeight;
|
|
||||||
}
|
|
||||||
/// Добавление карты
|
|
||||||
public void AddMap(String name, AbstractMap map)
|
|
||||||
{
|
|
||||||
// Логика для добавления
|
|
||||||
if (!_mapStorages.containsKey(name)) _mapStorages.put(name, new MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
|
||||||
}
|
|
||||||
/// Удаление карты
|
|
||||||
public void DelMap(String name)
|
|
||||||
{
|
|
||||||
// Логика для удаления
|
|
||||||
if (_mapStorages.containsKey(name)) _mapStorages.remove(name);
|
|
||||||
}
|
|
||||||
/// Доступ к парковке
|
|
||||||
public MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap> Get(String ind)
|
|
||||||
{
|
|
||||||
// Логика получения объекта
|
|
||||||
if (_mapStorages.containsKey(ind)) return _mapStorages.get(ind);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
// Доп.индексатор из задания
|
|
||||||
// TODO поле setlocomotives в mapwithsetlocomotivesgeneric было изменено на public
|
|
||||||
public DrawningObjectLocomotive Get (String name, int position) {
|
|
||||||
if (_mapStorages.containsKey(name)) return _mapStorages.get(name)._setLocomotives.Get(position);
|
|
||||||
else return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,20 +1,13 @@
|
|||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class SetLocomotivesGeneric <T>
|
public class SetLocomotivesGeneric <T>
|
||||||
{
|
{
|
||||||
/// Список хранимых объектов
|
private final T[] _places;
|
||||||
private final ArrayList<T> _places;
|
|
||||||
|
|
||||||
public int Count() {
|
public int Count() {
|
||||||
return _places.size();
|
return _places.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ограничение на количество
|
|
||||||
private final int _maxCount;
|
|
||||||
|
|
||||||
public SetLocomotivesGeneric(int count) {
|
public SetLocomotivesGeneric(int count) {
|
||||||
_maxCount = count;
|
_places = (T[]) new Object[count];
|
||||||
_places = new ArrayList<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert (T locomotive) {
|
public int Insert (T locomotive) {
|
||||||
@ -22,42 +15,47 @@ public class SetLocomotivesGeneric <T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int Insert (T locomotive, int position) {
|
public int Insert (T locomotive, int position) {
|
||||||
if (position >= _maxCount|| position < 0) return -1;
|
if (position >= _places.length || position < 0) return -1;
|
||||||
_places.add(position, locomotive);
|
if (_places[position] == null) {
|
||||||
|
_places[position] = locomotive;
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
int emptyEl = -1;
|
||||||
|
for (int i = position + 1; i < Count(); i++)
|
||||||
|
{
|
||||||
|
if (_places[i] == null)
|
||||||
|
{
|
||||||
|
emptyEl = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (emptyEl == -1)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
for (int i = emptyEl; i > position; i--)
|
||||||
|
{
|
||||||
|
_places[i] = _places[i - 1];
|
||||||
|
}
|
||||||
|
_places[position] = locomotive;
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Remove (int position) {
|
public T Remove (int position) {
|
||||||
if (position >= _maxCount || position < 0) return null;
|
if (position >= _places.length || position < 0) return null;
|
||||||
T result = _places.get(position);
|
T result = _places[position];
|
||||||
_places.remove(position);
|
_places[position] = null;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Get(int position)
|
public T Get(int position)
|
||||||
{
|
{
|
||||||
if (position >= _maxCount || position < 0)
|
if (position >= _places.length || position < 0)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return _places.get(position);
|
return _places[position];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Проход по набору до первого пустого
|
|
||||||
public Iterable<T> GetLocomotives()
|
|
||||||
{
|
|
||||||
/*for (var locomotive : _places)
|
|
||||||
{
|
|
||||||
if (locomotive != null)
|
|
||||||
{
|
|
||||||
yield return locomotive;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
yield break;
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
return _places;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user