Compare commits

..

2 Commits

Author SHA1 Message Date
c792adbea7 Merge branch 'LabWork03' into LabWork04 2024-05-12 13:49:01 +04:00
8571cb9ea6 Revert "лаб4"
This reverts commit 4ea097cdb5.
2024-05-12 13:48:17 +04:00
8 changed files with 118 additions and 259 deletions

View File

@ -18,7 +18,8 @@ public abstract class AbstractCompany {
_pictureWidth = picWidth; _pictureWidth = picWidth;
_pictureHeight = picHeight; _pictureHeight = picHeight;
_collection = collection; _collection = collection;
_collection.SetMaxCount(GetMaxCount()); System.out.println(_pictureHeight+" "+_pictureWidth+" "+_placeSizeHeight+" "+_placeSizeWidth);
_collection.SetMaxCount(GetMaxCount(), (Class) DrawingBaseStormtrooper.class);
} }
//Перегрузок нет //Перегрузок нет
public DrawingBaseStormtrooper GetRandomObject() public DrawingBaseStormtrooper GetRandomObject()

View File

@ -1,7 +0,0 @@
package CollectionGenericObjects;
public enum CollectionType {
None,
Massive,
List
}

View File

@ -4,8 +4,9 @@ package CollectionGenericObjects;
public interface ICollectionGenericObjects<T> public interface ICollectionGenericObjects<T>
{ {
int getCount(); int getCount();
void SetMaxCount(int count); void SetMaxCount(int count, Class<T> type);
int Insert(T obj); int Insert(T obj);
int Insert(T obj, int position);
T Remove(int position); T Remove(int position);
T Get(int position); T Get(int position);
} }

View File

@ -1,41 +0,0 @@
package CollectionGenericObjects;
import java.util.ArrayList;
import java.util.List;
public class ListGenericObjects<T> implements ICollectionGenericObjects<T> {
private List<T> _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<T>();
}
@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;
}
}

View File

@ -1,21 +1,17 @@
package CollectionGenericObjects; package CollectionGenericObjects;
import Drawnings.DrawingBaseStormtrooper;
import java.lang.reflect.Array; import java.lang.reflect.Array;
public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{ public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{
private T[] _collection = null; private T[] _collection;
private int Count; private int Count;
public void SetMaxCount(int size) { public void SetMaxCount(int size, Class<T> type) {
if (size > 0) { if (size > 0) {
if (_collection == null) { _collection = (T[]) Array.newInstance(type, size);
_collection = (T[]) Array.newInstance((Class) DrawingBaseStormtrooper.class, size);
Count = size; Count = size;
} }
} }
}
@Override @Override
public int getCount() { public int getCount() {
return Count; return Count;
@ -35,6 +31,36 @@ public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{
return -1; return -1;
} }
@Override @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) { public T Remove(int position) {
if (position >= getCount() || position < 0) if (position >= getCount() || position < 0)
return null; return null;

View File

@ -1,39 +0,0 @@
package CollectionGenericObjects;
import java.util.*;
public class StorageCollection<T> {
private Map<String, ICollectionGenericObjects<T>> _storages;
public StorageCollection()
{
_storages = new HashMap<String, ICollectionGenericObjects<T>>();
}
public Set<String> Keys() {
Set<String> 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<T>());
else if (collectionType == CollectionType.List)
_storages.put(name, new ListGenericObjects<T>());
}
public void DelCollection(String name)
{
if (_storages.containsKey(name))
_storages.remove(name);
}
public ICollectionGenericObjects<T> 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;
}
}

View File

@ -26,13 +26,16 @@ public class StormtrooperSharingService extends AbstractCompany{
protected void SetObjectsPosition() { protected void SetObjectsPosition() {
int width = _pictureWidth / _placeSizeWidth; int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight; int height = _pictureHeight / _placeSizeHeight;
int curWidth = width - 4; int curWidth = width - 4;
int curHeight = 0; int curHeight = 0;
for (int i = 0; i < (_collection.getCount()); i++) { for (int i = 0; i < (_collection.getCount()); i++) {
if (_collection.Get(i) != null) { if (_collection.Get(i) != null) {
_collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight); _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight);
_collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 16, curHeight * _placeSizeHeight + 3); _collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 16, curHeight * _placeSizeHeight + 3);
} }
if (curWidth < width-1) if (curWidth < width-1)
curWidth++; curWidth++;
else else

View File

@ -1,11 +1,13 @@
import CollectionGenericObjects.*; import CollectionGenericObjects.AbstractCompany;
import CollectionGenericObjects.MassiveGenericObjects;
import CollectionGenericObjects.StormtrooperSharingService;
import Drawnings.DrawingBaseStormtrooper; import Drawnings.DrawingBaseStormtrooper;
import Drawnings.DrawingStormtrooper; import Drawnings.DrawingStormtrooper;
import javax.swing.*; import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.util.LinkedList; import java.text.ParseException;
import java.util.Queue;
import java.util.Random; import java.util.Random;
import static java.lang.Integer.parseInt; import static java.lang.Integer.parseInt;
@ -15,36 +17,23 @@ public class FormStormtrooperCollection extends JFrame {
private Dimension dimension; private Dimension dimension;
public static CanvasFormStormtrooperCollection<DrawingBaseStormtrooper> _canvasStormtrooper = new CanvasFormStormtrooperCollection<DrawingBaseStormtrooper>(); public static CanvasFormStormtrooperCollection<DrawingBaseStormtrooper> _canvasStormtrooper = new CanvasFormStormtrooperCollection<DrawingBaseStormtrooper>();
private static AbstractCompany _company = null; private static AbstractCompany _company = null;
private Queue<DrawingBaseStormtrooper> _collectionRemovedObjects = new LinkedList<>(); private JButton CreateButton = new JButton("Создать бомбардировщик");;
private StorageCollection<DrawingBaseStormtrooper> _storageCollection = new StorageCollection<DrawingBaseStormtrooper>(); private JButton CreateShipButton = new JButton("Создать базовый бомбардировщик");
private JTextField textBoxCollection = new JTextField(); private JButton RemoveButton = new JButton("Удалить");
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 GoToCheckButton = new JButton("На проверку");
private JButton RandomButton = new JButton("Случайные"); private JButton RandomButton = new JButton("Случайные");
private JButton RefreshButton = new JButton("Обновить"); private JButton RefreshButton = new JButton("Обновить");
private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"}); private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"});
private JFormattedTextField TextField; private JFormattedTextField TextField;
public FormStormtrooperCollection(String title, Dimension dimension) { public FormStormtrooperCollection(String title, Dimension dimension) {
this.title = title; this.title = title;
this.dimension = dimension; this.dimension = dimension;
} }
public static void canvasShow() { public static void canvasShow() {
_company.SetPosition(); _company.SetPosition();
_canvasStormtrooper.SetCollectionToCanvas(_company); _canvasStormtrooper.SetCollectionToCanvas(_company);
_canvasStormtrooper.repaint(); _canvasStormtrooper.repaint();
} }
private void CreateObject(String typeOfClass){ private void CreateObject(String typeOfClass){
if (_company == null) return; if (_company == null) return;
int speed = (int)(Math.random() * 300 + 100); int speed = (int)(Math.random() * 300 + 100);
@ -63,23 +52,21 @@ public class FormStormtrooperCollection extends JFrame {
int typeOfEngine = ((int)((Math.random()*3)+1)); int typeOfEngine = ((int)((Math.random()*3)+1));
drawingBaseStormtrooper = new DrawingStormtrooper(speed, weight, bodyColor, additionalColor, rockets, bombs,engines,typeOfEngine); drawingBaseStormtrooper = new DrawingStormtrooper(speed, weight, bodyColor, additionalColor, rockets, bombs,engines,typeOfEngine);
break; break;
default: default: return;
return;
} }
if (_company._collection.Insert(drawingBaseStormtrooper) != -1) { if (_company._collection.Insert(drawingBaseStormtrooper, 0) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен"); JOptionPane.showMessageDialog(null, "Объект добавлен");
canvasShow(); canvasShow();
} else { }
else {
JOptionPane.showMessageDialog(null, "Объект не удалось добавить"); JOptionPane.showMessageDialog(null, "Объект не удалось добавить");
} }
} }
public Color getColor() { 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); Color color = JColorChooser.showDialog(this, "Цвет", initializator);
return color; return color;
} }
public void Init() { public void Init() {
setTitle(title); setTitle(title);
setMinimumSize(dimension); setMinimumSize(dimension);
@ -89,20 +76,31 @@ public class FormStormtrooperCollection extends JFrame {
TextField = new JFormattedTextField(); TextField = new JFormattedTextField();
createShipButton.addActionListener(new ActionListener() { ComboBoxCollections.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (ComboBoxCollections.getSelectedItem().toString()) {
case "Хранилище":
_company = new StormtrooperSharingService(getWidth()-200, getHeight()-110, new MassiveGenericObjects<DrawingBaseStormtrooper>());
break;
}
}
});
CreateShipButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
CreateObject("DrawingBaseStormtrooper"); CreateObject("DrawingBaseStormtrooper");
} }
}); });
createButton.addActionListener(new ActionListener() { CreateButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
CreateObject("DrawingStormtrooper"); CreateObject("DrawingStormtrooper");
} }
}); });
removeButton.addActionListener(new ActionListener() { RemoveButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (_company == null || TextField.getText() == null) { if (_company == null || TextField.getText() == null) {
@ -111,33 +109,37 @@ public class FormStormtrooperCollection extends JFrame {
int pos = parseInt(TextField.getText()); int pos = parseInt(TextField.getText());
int resultConfirmDialog = JOptionPane.showConfirmDialog(null, "Удалить", "Удаление", JOptionPane.YES_NO_OPTION); int resultConfirmDialog = JOptionPane.showConfirmDialog(null, "Удалить", "Удаление", JOptionPane.YES_NO_OPTION);
if (resultConfirmDialog == JOptionPane.NO_OPTION) return; if (resultConfirmDialog == JOptionPane.NO_OPTION) return;
DrawingBaseStormtrooper obj = _storageCollection.remove( if (_company._collection.Remove(pos) != null) {
listBoxCollection.getSelectedValue().toString(), pos); System.out.println(pos);
if (obj != null) {
JOptionPane.showMessageDialog(null, "Объект удален"); JOptionPane.showMessageDialog(null, "Объект удален");
_collectionRemovedObjects.add(obj);
canvasShow(); canvasShow();
} else { }
else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект"); JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
} }
} }
}); });
GoToCheckButton.addActionListener(new ActionListener() { GoToCheckButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (_company == null) { if (_company == null)
{
return; return;
} }
DrawingBaseStormtrooper stormtrooper = null; DrawingBaseStormtrooper stormtrooper = null;
int counter = 100; int counter = 100;
while (stormtrooper == null) { while (stormtrooper == null)
{
stormtrooper = _company.GetRandomObject(); stormtrooper = _company.GetRandomObject();
counter--; counter--;
if (counter <= 0) { if (counter <= 0)
{
break; break;
} }
} }
if (stormtrooper == null) { if (stormtrooper == null)
{
return; return;
} }
FormStormtrooper form = new FormStormtrooper("Бомбардировщик", new Dimension(1000,750)); FormStormtrooper form = new FormStormtrooper("Бомбардировщик", new Dimension(1000,750));
@ -156,7 +158,8 @@ public class FormStormtrooperCollection extends JFrame {
RefreshButton.addActionListener(new ActionListener() { RefreshButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (_company == null) { if (_company == null)
{
return; return;
} }
canvasShow(); canvasShow();
@ -172,129 +175,41 @@ public class FormStormtrooperCollection extends JFrame {
form.setCompany(_company); form.setCompany(_company);
} }
}); });
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<DrawingBaseStormtrooper> 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()); _canvasStormtrooper.setBounds(0, 0, getWidth()-200, getHeight());
labelCollectionName.setBounds(getWidth()-190, 10, 150, 20); ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20);
textBoxCollection.setBounds(getWidth()-190,35,150,25); CreateShipButton.setBounds(getWidth()-190, 60, 150, 30);
radioButtonMassive.setBounds(getWidth()-190, 60, 75, 20); CreateButton.setBounds(getWidth()-190, 100, 150, 30);
radioButtonList.setBounds(getWidth()-105, 60, 75, 20); RandomButton.setBounds(getWidth()-190, 140, 150, 30);
buttonAddCollection.setBounds(getWidth()-190, 85, 150, 20); TextField.setBounds(getWidth()-190,200,150,30);
listBoxCollection.setBounds(getWidth()-190, 115, 150, 70); RemoveButton.setBounds(getWidth()-190, 240, 150, 30);
buttonRemoveCollection.setBounds(getWidth()-190, 195, 150, 20); GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30);
ComboBoxCollections.setBounds(getWidth() - 190, 225, 150, 20); RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30);
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); setSize(dimension.width,dimension.height);
setLayout(null); setLayout(null);
add(textBoxCollection);
add(radioButtonMassive);
add(radioButtonList);
add(buttonAddCollection);
add(listBoxCollection);
add(buttonRemoveCollection);
add(buttonCreateCompany);
add(labelCollectionName);
add(removeObjectsButton);
add(_canvasStormtrooper); add(_canvasStormtrooper);
add(ComboBoxCollections); add(ComboBoxCollections);
add(createShipButton); add(CreateShipButton);
add(createButton); add(CreateButton);
add(TextField); add(TextField);
add(removeButton); add(RemoveButton);
add(GoToCheckButton); add(GoToCheckButton);
add(RandomButton); add(RandomButton);
add(RefreshButton); add(RefreshButton);
setVisible(true); 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<String> list = new DefaultListModel<String>();
for (String name : _storageCollection.Keys()) {
if (name != "") {
list.addElement(name);
}
}
listBoxCollection.setModel(list);
} }
} }