This commit is contained in:
Ilya Ryabov 2024-04-28 13:45:08 +04:00
parent b345db3a00
commit 648d42871e
8 changed files with 137 additions and 89 deletions

View File

@ -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()

View File

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

View File

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

View File

@ -0,0 +1,41 @@
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,15 +1,19 @@
package CollectionGenericObjects;
import Drawnings.DrawingBaseStormtrooper;
import java.lang.reflect.Array;
public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{
private T[] _collection;
private T[] _collection = null;
private int Count;
public void SetMaxCount(int size, Class<T> 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<T> implements ICollectionGenericObjects<T>{
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;

View File

@ -0,0 +1,39 @@
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,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

View File

@ -1,18 +1,16 @@
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<DrawingBaseStormtrooper> _canvasStormtrooper = new CanvasFormStormtrooperCollection<DrawingBaseStormtrooper>();
@ -36,15 +34,18 @@ public class FormStormtrooperCollection extends JFrame{
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) {
if (_company == null) return;
int speed = (int) (Math.random() * 300 + 100);
@ -104,49 +105,45 @@ public class FormStormtrooperCollection extends JFrame{
}
});
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) {
@ -162,8 +159,7 @@ public class FormStormtrooperCollection extends JFrame{
RefreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null)
{
if (_company == null) {
return;
}
canvasShow();
@ -172,22 +168,14 @@ 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);
buttonAddCollection.addActionListener(new ActionListener() {
@ -285,29 +273,33 @@ public class FormStormtrooperCollection extends JFrame{
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(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<String> list = new DefaultListModel<String>();
for (String name : _storageCollection.Keys()) {
if (name != "") {
list.addElement(name);
}
});
}
listBoxCollection.setModel(list);
}
}