From 4ea097cdb5a65aa4264ef32b89a9f0456d7a80e4 Mon Sep 17 00:00:00 2001 From: ilyaryabovv Date: Sun, 28 Apr 2024 13:45:08 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=B0=D0=B14?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.java | 3 +- .../CollectionType.java | 7 + .../ICollectionGenericObjects.java | 3 +- .../ListGenericObjects.java | 41 +++ .../MassiveGenericObjects.java | 42 +-- .../StorageCollection.java | 39 +++ .../StormtrooperSharingService.java | 3 - .../src/FormStormtrooperCollection.java | 239 ++++++++++++------ 8 files changed, 259 insertions(+), 118 deletions(-) create mode 100644 ProjectStormtrooper/src/CollectionGenericObjects/CollectionType.java create mode 100644 ProjectStormtrooper/src/CollectionGenericObjects/ListGenericObjects.java create mode 100644 ProjectStormtrooper/src/CollectionGenericObjects/StorageCollection.java diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/AbstractCompany.java b/ProjectStormtrooper/src/CollectionGenericObjects/AbstractCompany.java index f04184c..b1a22d8 100644 --- a/ProjectStormtrooper/src/CollectionGenericObjects/AbstractCompany.java +++ b/ProjectStormtrooper/src/CollectionGenericObjects/AbstractCompany.java @@ -18,8 +18,7 @@ public abstract class AbstractCompany { _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = collection; - System.out.println(_pictureHeight+" "+_pictureWidth+" "+_placeSizeHeight+" "+_placeSizeWidth); - _collection.SetMaxCount(GetMaxCount(), (Class) DrawingBaseStormtrooper.class); + _collection.SetMaxCount(GetMaxCount()); } //Перегрузок нет public DrawingBaseStormtrooper GetRandomObject() diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/CollectionType.java b/ProjectStormtrooper/src/CollectionGenericObjects/CollectionType.java new file mode 100644 index 0000000..bba944c --- /dev/null +++ b/ProjectStormtrooper/src/CollectionGenericObjects/CollectionType.java @@ -0,0 +1,7 @@ +package CollectionGenericObjects; + +public enum CollectionType { + None, + Massive, + List +} diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/ICollectionGenericObjects.java b/ProjectStormtrooper/src/CollectionGenericObjects/ICollectionGenericObjects.java index d9e4279..b0f2825 100644 --- a/ProjectStormtrooper/src/CollectionGenericObjects/ICollectionGenericObjects.java +++ b/ProjectStormtrooper/src/CollectionGenericObjects/ICollectionGenericObjects.java @@ -4,9 +4,8 @@ package CollectionGenericObjects; public interface ICollectionGenericObjects { int getCount(); - void SetMaxCount(int count, Class type); + void SetMaxCount(int count); int Insert(T obj); - int Insert(T obj, int position); T Remove(int position); T Get(int position); } diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/ListGenericObjects.java b/ProjectStormtrooper/src/CollectionGenericObjects/ListGenericObjects.java new file mode 100644 index 0000000..c7dbf28 --- /dev/null +++ b/ProjectStormtrooper/src/CollectionGenericObjects/ListGenericObjects.java @@ -0,0 +1,41 @@ +package CollectionGenericObjects; + +import java.util.ArrayList; +import java.util.List; +public class ListGenericObjects implements ICollectionGenericObjects { + private List _collection; + private int _maxCount; + public int getCount() { + return _collection.size(); + } + @Override + public void SetMaxCount(int size) { + if (size > 0) { + _maxCount = size; + } + } + public ListGenericObjects() { + _collection = new ArrayList(); + } + @Override + public T Get(int position) + { + if (position >= getCount() || position < 0) return null; + return _collection.get(position); + } + @Override + public int Insert(T obj) + { + if (getCount() == _maxCount) return -1; + _collection.add(obj); + return getCount(); + } + @Override + public T Remove(int position) + { + if (position >= getCount() || position < 0) return null; + T obj = _collection.get(position); + _collection.remove(position); + return obj; + } +} \ No newline at end of file diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/MassiveGenericObjects.java b/ProjectStormtrooper/src/CollectionGenericObjects/MassiveGenericObjects.java index d3f6753..cdc06ab 100644 --- a/ProjectStormtrooper/src/CollectionGenericObjects/MassiveGenericObjects.java +++ b/ProjectStormtrooper/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -1,15 +1,19 @@ package CollectionGenericObjects; +import Drawnings.DrawingBaseStormtrooper; + import java.lang.reflect.Array; public class MassiveGenericObjects implements ICollectionGenericObjects{ - private T[] _collection; + private T[] _collection = null; private int Count; - public void SetMaxCount(int size, Class type) { + public void SetMaxCount(int size) { if (size > 0) { - _collection = (T[]) Array.newInstance(type, size); - Count = size; + if (_collection == null) { + _collection = (T[]) Array.newInstance((Class) DrawingBaseStormtrooper.class, size); + Count = size; + } } } @Override @@ -31,36 +35,6 @@ public class MassiveGenericObjects implements ICollectionGenericObjects{ return -1; } @Override - public int Insert(T obj, int position) { - if (position >= getCount() || position < 0) - return -1; - if (_collection[position] == null) { - _collection[position] = obj; - return position; - } - int index = position + 1; - while (index < getCount()) - { - if (_collection[index] == null) - { - _collection[index] = obj; - return index; - } - ++index; - } - index = position - 1; - while (index >= 0) - { - if (_collection[index] == null) - { - _collection[index] = obj; - return index; - } - --index; - } - return -1; - } - @Override public T Remove(int position) { if (position >= getCount() || position < 0) return null; diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/StorageCollection.java b/ProjectStormtrooper/src/CollectionGenericObjects/StorageCollection.java new file mode 100644 index 0000000..bf56bdc --- /dev/null +++ b/ProjectStormtrooper/src/CollectionGenericObjects/StorageCollection.java @@ -0,0 +1,39 @@ +package CollectionGenericObjects; +import java.util.*; + +public class StorageCollection { + private Map> _storages; + public StorageCollection() + { + _storages = new HashMap>(); + } + public Set Keys() { + Set keys = _storages.keySet(); + return keys; + } + public void AddCollection(String name, CollectionType collectionType) + { + if (_storages.containsKey(name)) return; + if (collectionType == CollectionType.None) return; + else if (collectionType == CollectionType.Massive) + _storages.put(name, new MassiveGenericObjects()); + else if (collectionType == CollectionType.List) + _storages.put(name, new ListGenericObjects()); + } + public void DelCollection(String name) + { + if (_storages.containsKey(name)) + _storages.remove(name); + } + public ICollectionGenericObjects getCollectionObject(String name) { + if (_storages.containsKey(name)) + return _storages.get(name); + return null; + } + public T remove(String name, int position){ + if(_storages.containsKey(name)) + return _storages.get(name).Remove(position); + return null; + } + +} \ No newline at end of file diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/StormtrooperSharingService.java b/ProjectStormtrooper/src/CollectionGenericObjects/StormtrooperSharingService.java index de43749..2a9311f 100644 --- a/ProjectStormtrooper/src/CollectionGenericObjects/StormtrooperSharingService.java +++ b/ProjectStormtrooper/src/CollectionGenericObjects/StormtrooperSharingService.java @@ -26,16 +26,13 @@ public class StormtrooperSharingService extends AbstractCompany{ protected void SetObjectsPosition() { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; - int curWidth = width - 4; int curHeight = 0; - for (int i = 0; i < (_collection.getCount()); i++) { if (_collection.Get(i) != null) { _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight); _collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 16, curHeight * _placeSizeHeight + 3); } - if (curWidth < width-1) curWidth++; else diff --git a/ProjectStormtrooper/src/FormStormtrooperCollection.java b/ProjectStormtrooper/src/FormStormtrooperCollection.java index 801662a..fb17cac 100644 --- a/ProjectStormtrooper/src/FormStormtrooperCollection.java +++ b/ProjectStormtrooper/src/FormStormtrooperCollection.java @@ -1,43 +1,54 @@ -import CollectionGenericObjects.AbstractCompany; -import CollectionGenericObjects.MassiveGenericObjects; -import CollectionGenericObjects.StormtrooperSharingService; +import CollectionGenericObjects.*; import Drawnings.DrawingBaseStormtrooper; import Drawnings.DrawingStormtrooper; import javax.swing.*; -import javax.swing.text.MaskFormatter; import java.awt.*; import java.awt.event.*; -import java.text.ParseException; +import java.util.LinkedList; +import java.util.Queue; import java.util.Random; import static java.lang.Integer.parseInt; -public class FormStormtrooperCollection extends JFrame{ +public class FormStormtrooperCollection extends JFrame { private String title; private Dimension dimension; public static CanvasFormStormtrooperCollection _canvasStormtrooper = new CanvasFormStormtrooperCollection(); private static AbstractCompany _company = null; - private JButton CreateButton = new JButton("Создать бомбардировщик");; - private JButton CreateShipButton = new JButton("Создать базовый бомбардировщик"); - private JButton RemoveButton = new JButton("Удалить"); + private Queue _collectionRemovedObjects = new LinkedList<>(); + private StorageCollection _storageCollection = new StorageCollection(); + private JTextField textBoxCollection = new JTextField(); + private JRadioButton radioButtonMassive = new JRadioButton("Массив"); + private JRadioButton radioButtonList = new JRadioButton("Список"); + private JButton buttonAddCollection = new JButton("Добавить"); + private JList listBoxCollection = new JList(); + private JButton buttonRemoveCollection = new JButton("Удалить"); + private JButton buttonCreateCompany = new JButton("Создать компанию"); + private JButton createButton = new JButton("Создать бомбардировщик"); + private JButton createShipButton = new JButton("Создать базовый бомбардировщик"); + private JButton removeButton = new JButton("Удалить"); + private JButton removeObjectsButton = new JButton("Удаленные объекты"); private JButton GoToCheckButton = new JButton("На проверку"); private JButton RandomButton = new JButton("Случайные"); private JButton RefreshButton = new JButton("Обновить"); private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"}); private JFormattedTextField TextField; + public FormStormtrooperCollection(String title, Dimension dimension) { this.title = title; this.dimension = dimension; } + public static void canvasShow() { _company.SetPosition(); _canvasStormtrooper.SetCollectionToCanvas(_company); _canvasStormtrooper.repaint(); } - private void CreateObject(String typeOfClass){ + + private void CreateObject(String typeOfClass) { if (_company == null) return; - int speed = (int)(Math.random() * 300 + 100); - float weight = (float)(Math.random() * 3000 + 1000); + int speed = (int) (Math.random() * 300 + 100); + float weight = (float) (Math.random() * 3000 + 1000); Color bodyColor = getColor(); DrawingBaseStormtrooper drawingBaseStormtrooper; switch (typeOfClass) { @@ -49,24 +60,26 @@ public class FormStormtrooperCollection extends JFrame{ boolean rockets = new Random().nextBoolean(); boolean bombs = new Random().nextBoolean(); boolean engines = new Random().nextBoolean(); - int typeOfEngine = ((int)((Math.random()*3)+1)); - drawingBaseStormtrooper = new DrawingStormtrooper(speed, weight, bodyColor, additionalColor, rockets, bombs,engines,typeOfEngine); + int typeOfEngine = ((int) ((Math.random() * 3) + 1)); + drawingBaseStormtrooper = new DrawingStormtrooper(speed, weight, bodyColor, additionalColor, rockets, bombs, engines, typeOfEngine); break; - default: return; + default: + return; } - if (_company._collection.Insert(drawingBaseStormtrooper, 0) != -1) { + if (_company._collection.Insert(drawingBaseStormtrooper) != -1) { JOptionPane.showMessageDialog(null, "Объект добавлен"); canvasShow(); - } - else { + } else { JOptionPane.showMessageDialog(null, "Объект не удалось добавить"); } } + public Color getColor() { - Color initializator = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); + Color initializator = new Color((int) (Math.random() * 255 + 0), (int) (Math.random() * 255 + 0), (int) (Math.random() * 255 + 0)); Color color = JColorChooser.showDialog(this, "Цвет", initializator); return color; } + public void Init() { setTitle(title); setMinimumSize(dimension); @@ -75,73 +88,58 @@ public class FormStormtrooperCollection extends JFrame{ TextField = new JFormattedTextField(); - ComboBoxCollections.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - switch (ComboBoxCollections.getSelectedItem().toString()) { - case "Хранилище": - _company = new StormtrooperSharingService(getWidth()-200, getHeight()-110, new MassiveGenericObjects()); - break; - } - } - }); - - CreateShipButton.addActionListener(new ActionListener() { + createShipButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { CreateObject("DrawingBaseStormtrooper"); } }); - CreateButton.addActionListener(new ActionListener() { + createButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { CreateObject("DrawingStormtrooper"); } }); - RemoveButton.addActionListener(new ActionListener() { + removeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_company == null || TextField.getText() == null) { + if (_company == null || TextField.getText() == null ) { return; } int pos = parseInt(TextField.getText()); int resultConfirmDialog = JOptionPane.showConfirmDialog(null, "Удалить", "Удаление", JOptionPane.YES_NO_OPTION); if (resultConfirmDialog == JOptionPane.NO_OPTION) return; - if (_company._collection.Remove(pos) != null) { - System.out.println(pos); + DrawingBaseStormtrooper obj = _storageCollection.remove( + listBoxCollection.getSelectedValue().toString(), pos); + if (obj != null) { JOptionPane.showMessageDialog(null, "Объект удален"); + _collectionRemovedObjects.add(obj); canvasShow(); - } - else { + } else { JOptionPane.showMessageDialog(null, "Не удалось удалить объект"); } } }); - GoToCheckButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_company == null) - { + if (_company == null) { return; } DrawingBaseStormtrooper stormtrooper = null; int counter = 100; - while (stormtrooper == null) - { + while (stormtrooper == null) { stormtrooper = _company.GetRandomObject(); counter--; - if (counter <= 0) - { + if (counter <= 0) { break; } } - if (stormtrooper == null) - { + if (stormtrooper == null) { return; } - FormStormtrooper form = new FormStormtrooper("Бомбардировщик", new Dimension(1000,750)); + FormStormtrooper form = new FormStormtrooper("Бомбардировщик", new Dimension(1000, 750)); form.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { @@ -157,8 +155,7 @@ public class FormStormtrooperCollection extends JFrame{ RefreshButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_company == null) - { + if (_company == null) { return; } canvasShow(); @@ -167,48 +164,136 @@ public class FormStormtrooperCollection extends JFrame{ RandomButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_company==null){ + if (_company == null) { return; } FormAdditionalCollection form = new FormAdditionalCollection(); form.setCompany(_company); } }); - _canvasStormtrooper.setBounds(0, 0, getWidth()-200, getHeight()); - ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20); - CreateShipButton.setBounds(getWidth()-190, 60, 150, 30); - CreateButton.setBounds(getWidth()-190, 100, 150, 30); - RandomButton.setBounds(getWidth()-190, 140, 150, 30); - TextField.setBounds(getWidth()-190,200,150,30); - RemoveButton.setBounds(getWidth()-190, 240, 150, 30); - GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30); - RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30); - setSize(dimension.width,dimension.height); + buttonAddCollection.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (textBoxCollection.getText().isEmpty() || (!radioButtonMassive.isSelected() + && !radioButtonList.isSelected())) { + JOptionPane.showMessageDialog(null, "Не все данные заполнены"); + return; + } + CollectionType collectionType = CollectionType.None; + if (radioButtonMassive.isSelected()) { + collectionType = CollectionType.Massive; + } else if (radioButtonList.isSelected()) { + collectionType = CollectionType.List; + } + _storageCollection.AddCollection(textBoxCollection.getText(), collectionType); + RerfreshListBoxItems(); + } + }); + buttonRemoveCollection.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (listBoxCollection.getSelectedIndex() < 0 || listBoxCollection.getSelectedValue() == null) { + JOptionPane.showMessageDialog(null, "Коллекция не выбрана"); + return; + } + int resultConfirmDialog = JOptionPane.showConfirmDialog(null, + "Удалить", "Удаление", + JOptionPane.YES_NO_OPTION); + if (resultConfirmDialog == JOptionPane.NO_OPTION) return; + _storageCollection.DelCollection(listBoxCollection.getSelectedValue().toString()); + RerfreshListBoxItems(); + } + }); + buttonCreateCompany.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (listBoxCollection.getSelectedIndex() < 0 || listBoxCollection.getSelectedValue() == null) { + JOptionPane.showMessageDialog(null, "Коллекция не выбрана"); + return; + } + ICollectionGenericObjects collection = + _storageCollection.getCollectionObject(listBoxCollection.getSelectedValue().toString()); + if (collection == null) { + JOptionPane.showMessageDialog(null, "Коллекция не проинициализирована"); + return; + } + switch (ComboBoxCollections.getSelectedItem().toString()) { + case "Хранилище": + _company = new StormtrooperSharingService(getWidth() - 200, getHeight() - 110, + collection); + break; + } + RerfreshListBoxItems(); + } + }); + removeObjectsButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_collectionRemovedObjects.isEmpty()) { + JOptionPane.showMessageDialog(null, "Коллекция пуста"); + return; + } + DrawingBaseStormtrooper stormtrooper = null; + stormtrooper = _collectionRemovedObjects.remove(); + if (stormtrooper == null) { + return; + } + FormStormtrooper form = new FormStormtrooper("Бомбардировщик", new Dimension(1000, 750)); + form.Init(stormtrooper); + } + }); + ButtonGroup radioButtonsGroup = new ButtonGroup(); + JLabel labelCollectionName = new JLabel("Название коллекции"); + radioButtonsGroup.add(radioButtonMassive); + radioButtonsGroup.add(radioButtonList); + _canvasStormtrooper.setBounds(0, 0, getWidth() - 200, getHeight()); + labelCollectionName.setBounds(getWidth()-190, 10, 150, 20); + textBoxCollection.setBounds(getWidth()-190,35,150,25); + radioButtonMassive.setBounds(getWidth()-190, 60, 75, 20); + radioButtonList.setBounds(getWidth()-105, 60, 75, 20); + buttonAddCollection.setBounds(getWidth()-190, 85, 150, 20); + listBoxCollection.setBounds(getWidth()-190, 115, 150, 70); + buttonRemoveCollection.setBounds(getWidth()-190, 195, 150, 20); + ComboBoxCollections.setBounds(getWidth() - 190, 225, 150, 20); + buttonCreateCompany.setBounds(getWidth()-190, 255, 150, 20); + createShipButton.setBounds(getWidth() - 190, 285, 150, 30); + createButton.setBounds(getWidth() - 190, 325, 150, 30); + RandomButton.setBounds(getWidth() - 190, 365, 150, 30); + removeObjectsButton.setBounds(getWidth()-190, 505, 150, 30); + TextField.setBounds(getWidth() - 190, 545, 150, 30); + removeButton.setBounds(getWidth() - 190, 585, 150, 30); + GoToCheckButton.setBounds(getWidth() - 190, 625, 150, 30); + RefreshButton.setBounds(getWidth() - 190, 665, 150, 30); + setSize(dimension.width, dimension.height); setLayout(null); + add(textBoxCollection); + add(radioButtonMassive); + add(radioButtonList); + add(buttonAddCollection); + add(listBoxCollection); + add(buttonRemoveCollection); + add(buttonCreateCompany); + add(labelCollectionName); + add(removeObjectsButton); add(_canvasStormtrooper); add(ComboBoxCollections); - add(CreateShipButton); - add(CreateButton); + add(createShipButton); + add(createButton); add(TextField); - add(RemoveButton); + add(removeButton); add(GoToCheckButton); add(RandomButton); add(RefreshButton); setVisible(true); - - addComponentListener(new ComponentAdapter() { - public void componentResized(ComponentEvent e) { - _canvasStormtrooper.setBounds(0, 0, getWidth()-200, getHeight()-70); - ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20); - CreateShipButton.setBounds(getWidth()-190, 60, 150, 30); - CreateButton.setBounds(getWidth()-190, 100, 150, 30); - TextField.setBounds(getWidth()-190,200,150,30); - RemoveButton.setBounds(getWidth()-190, 240, 150, 30); - GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30); - RandomButton.setBounds(getWidth()-190, 140, 150, 30); - RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30); + } + private void RerfreshListBoxItems() { + DefaultListModel list = new DefaultListModel(); + for (String name : _storageCollection.Keys()) { + if (name != "") { + list.addElement(name); } - }); + } + listBoxCollection.setModel(list); } } \ No newline at end of file