From 029dcb4e425467dafcc6a4c7c278f2778728f945 Mon Sep 17 00:00:00 2001 From: ilyaryabovv Date: Sat, 27 Apr 2024 17:00:36 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=BB=D0=B0=D0=B13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/CanvasFormStormtrooperCollection.java | 29 +++ .../AbstractCompany.java | 35 +++ .../AdditionalCollection.java | 60 +++++ .../ICollectionGenericObjects.java | 12 + .../MassiveGenericObjects.java | 76 ++++++ .../StormtrooperSharingService.java | 52 +++++ .../Drawnings/DrawingBaseStormtrooper.java | 6 + .../src/Drawnings/DrawingStormtrooper.java | 6 +- .../src/FormAdditionalCollection.java | 154 ++++++++++++ ProjectStormtrooper/src/FormStormtrooper.java | 106 +++------ .../src/FormStormtrooperCollection.java | 221 ++++++++++++++++++ ProjectStormtrooper/src/Main.java | 2 +- 12 files changed, 678 insertions(+), 81 deletions(-) create mode 100644 ProjectStormtrooper/src/CanvasFormStormtrooperCollection.java create mode 100644 ProjectStormtrooper/src/CollectionGenericObjects/AbstractCompany.java create mode 100644 ProjectStormtrooper/src/CollectionGenericObjects/AdditionalCollection.java create mode 100644 ProjectStormtrooper/src/CollectionGenericObjects/ICollectionGenericObjects.java create mode 100644 ProjectStormtrooper/src/CollectionGenericObjects/MassiveGenericObjects.java create mode 100644 ProjectStormtrooper/src/CollectionGenericObjects/StormtrooperSharingService.java create mode 100644 ProjectStormtrooper/src/FormAdditionalCollection.java create mode 100644 ProjectStormtrooper/src/FormStormtrooperCollection.java diff --git a/ProjectStormtrooper/src/CanvasFormStormtrooperCollection.java b/ProjectStormtrooper/src/CanvasFormStormtrooperCollection.java new file mode 100644 index 0000000..9c93a70 --- /dev/null +++ b/ProjectStormtrooper/src/CanvasFormStormtrooperCollection.java @@ -0,0 +1,29 @@ +import javax.swing.*; +import CollectionGenericObjects.AbstractCompany; +import Drawnings.DrawingBaseStormtrooper; + +import java.awt.*; + +public class CanvasFormStormtrooperCollection extends JComponent { + public AbstractCompany company = null; + public void SetCollectionToCanvas(AbstractCompany company) { + this.company = company; + } + public CanvasFormStormtrooperCollection() {}; + public void paintComponent(Graphics g) { + super.paintComponents(g); + if (company == null || company._collection == null) { + return; + } + company.DrawBackgound(g); + for (int i = 0; i < company._collection.getCount(); i++) { + + Graphics2D g2d = (Graphics2D) g; + T obj = (T) company._collection.Get(i); + if (obj instanceof DrawingBaseStormtrooper) { + ((DrawingBaseStormtrooper) obj).DrawTransport(g2d); + } + } + super.repaint(); + } +} diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/AbstractCompany.java b/ProjectStormtrooper/src/CollectionGenericObjects/AbstractCompany.java new file mode 100644 index 0000000..f04184c --- /dev/null +++ b/ProjectStormtrooper/src/CollectionGenericObjects/AbstractCompany.java @@ -0,0 +1,35 @@ +package CollectionGenericObjects; +import Drawnings.DrawingBaseStormtrooper; + +import java.awt.*; + +public abstract class AbstractCompany { + protected int _placeSizeWidth = 220; + protected int _placeSizeHeight = 155; + protected int _pictureWidth; + protected int _pictureHeight; + public ICollectionGenericObjects _collection = null; + private int GetMaxCount() { + return _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight); + + } + public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects collection) + { + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = collection; + System.out.println(_pictureHeight+" "+_pictureWidth+" "+_placeSizeHeight+" "+_placeSizeWidth); + _collection.SetMaxCount(GetMaxCount(), (Class) DrawingBaseStormtrooper.class); + } + //Перегрузок нет + public DrawingBaseStormtrooper GetRandomObject() + { + return _collection.Get((int)(Math.random()*GetMaxCount() + 0)); + } + public void SetPosition() + { + SetObjectsPosition(); + } + public abstract void DrawBackgound(Graphics graphics); + protected abstract void SetObjectsPosition(); +} \ No newline at end of file diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/AdditionalCollection.java b/ProjectStormtrooper/src/CollectionGenericObjects/AdditionalCollection.java new file mode 100644 index 0000000..35891a5 --- /dev/null +++ b/ProjectStormtrooper/src/CollectionGenericObjects/AdditionalCollection.java @@ -0,0 +1,60 @@ +package CollectionGenericObjects; + +import Drawnings.DrawingBaseStormtrooper; +import Drawnings.DrawingStormtrooper; +import Drawnings.Engines.IDrawingEngines; +import Entities.EntityBaseStormtrooper; +import Entities.EntityStormtrooper; +import java.lang.reflect.Array; +import java.util.Random; + +public class AdditionalCollection { + public T[] _collectionEntity; + public U[] _collectionEngines; + public AdditionalCollection(int size, Class type1, Class type2) { + _collectionEntity = (T[]) Array.newInstance(type1, size); + _collectionEngines = (U[]) Array.newInstance(type2, size); + CountEntities = size; + CountEngines = size; + } + public int CountEntities; + public int CountEngines; + public int Insert(T entity) { + int index = 0; + while (index < CountEntities) { + if (_collectionEntity[index] == null) + { + _collectionEntity[index] = entity; + return index; + } + ++index; + } + return -1; + } + public int Insert(U decks) { + int index = 0; + while (index < CountEngines) { + if (_collectionEngines[index] == null) + { + _collectionEngines[index] = decks; + return index; + } + ++index; + } + return -1; + } + public DrawingBaseStormtrooper CreateAdditionalCollectionStormtrooper() { + Random random = new Random(); + if (_collectionEntity == null || _collectionEngines == null) return null; + T entity = _collectionEntity[random.nextInt(CountEntities)]; + U engines = _collectionEngines[random.nextInt(CountEngines)]; + DrawingBaseStormtrooper drawingBaseStormtrooper = null; + if (entity instanceof EntityStormtrooper) { + drawingBaseStormtrooper = new DrawingStormtrooper((EntityStormtrooper) entity, engines); + } + else { + drawingBaseStormtrooper = new DrawingBaseStormtrooper(entity, engines); + } + return drawingBaseStormtrooper; + } +} \ No newline at end of file diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/ICollectionGenericObjects.java b/ProjectStormtrooper/src/CollectionGenericObjects/ICollectionGenericObjects.java new file mode 100644 index 0000000..d9e4279 --- /dev/null +++ b/ProjectStormtrooper/src/CollectionGenericObjects/ICollectionGenericObjects.java @@ -0,0 +1,12 @@ + +package CollectionGenericObjects; + +public interface ICollectionGenericObjects +{ + int getCount(); + void SetMaxCount(int count, Class type); + int Insert(T obj); + int Insert(T obj, int position); + T Remove(int position); + T Get(int position); +} diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/MassiveGenericObjects.java b/ProjectStormtrooper/src/CollectionGenericObjects/MassiveGenericObjects.java new file mode 100644 index 0000000..d3f6753 --- /dev/null +++ b/ProjectStormtrooper/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -0,0 +1,76 @@ +package CollectionGenericObjects; + +import java.lang.reflect.Array; + + +public class MassiveGenericObjects implements ICollectionGenericObjects{ + private T[] _collection; + private int Count; + public void SetMaxCount(int size, Class type) { + if (size > 0) { + _collection = (T[]) Array.newInstance(type, size); + Count = size; + } + } + @Override + public int getCount() { + return Count; + } + @Override + public int Insert(T obj) { + int index = 0; + while (index < getCount()) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + ++index; + } + 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; + T obj = (T) _collection[position]; + _collection[position] = null; + return obj; + } + @Override + public T Get(int position) { + if (position >= getCount() || position < 0) return null; + return (T) _collection[position]; + } +} diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/StormtrooperSharingService.java b/ProjectStormtrooper/src/CollectionGenericObjects/StormtrooperSharingService.java new file mode 100644 index 0000000..de43749 --- /dev/null +++ b/ProjectStormtrooper/src/CollectionGenericObjects/StormtrooperSharingService.java @@ -0,0 +1,52 @@ +package CollectionGenericObjects; + +import Drawnings.DrawingBaseStormtrooper; + +import java.awt.*; + +public class StormtrooperSharingService extends AbstractCompany{ + public StormtrooperSharingService (int picWidth, int picHeight, ICollectionGenericObjects collection) { + super(picWidth, picHeight, collection); + } + @Override + public void DrawBackgound(Graphics g) { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + g.setColor(Color.BLACK); + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height + 1; ++j) + { + g.drawLine(i * _placeSizeWidth+15, j * _placeSizeHeight, i * _placeSizeWidth+15 + _placeSizeWidth-55, j * _placeSizeHeight); + g.drawLine(i * _placeSizeWidth+15, j * _placeSizeHeight, i * _placeSizeWidth+15, j * _placeSizeHeight -_placeSizeHeight); + } + } + } + @Override + 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 + { + curWidth = 0; + curHeight++; + } + if (curHeight > height) + { + return; + } + } + } +} \ No newline at end of file diff --git a/ProjectStormtrooper/src/Drawnings/DrawingBaseStormtrooper.java b/ProjectStormtrooper/src/Drawnings/DrawingBaseStormtrooper.java index 5460079..e40398d 100644 --- a/ProjectStormtrooper/src/Drawnings/DrawingBaseStormtrooper.java +++ b/ProjectStormtrooper/src/Drawnings/DrawingBaseStormtrooper.java @@ -1,9 +1,11 @@ package Drawnings; +import Drawnings.Engines.IDrawingEngines; import Entities.*; import java.awt.*; public class DrawingBaseStormtrooper { public EntityBaseStormtrooper EntityBaseStormtrooper; + public IDrawingEngines drawingEngines =null; protected Integer _pictureWidth; protected Integer _pictureHeight; protected Integer _startPosX; @@ -25,6 +27,10 @@ public class DrawingBaseStormtrooper { super(); EntityBaseStormtrooper = new EntityBaseStormtrooper(speed, weight, bodyColor); } + public DrawingBaseStormtrooper(EntityBaseStormtrooper entityBaseStormtrooper, IDrawingEngines drawingEngines){ + this.EntityBaseStormtrooper = entityBaseStormtrooper; + this.drawingEngines = drawingEngines; + } public boolean SetPictureSize(int width, int height) { // TODO проверка, что объект "влезает" в размеры поля diff --git a/ProjectStormtrooper/src/Drawnings/DrawingStormtrooper.java b/ProjectStormtrooper/src/Drawnings/DrawingStormtrooper.java index 4ea2061..6901e46 100644 --- a/ProjectStormtrooper/src/Drawnings/DrawingStormtrooper.java +++ b/ProjectStormtrooper/src/Drawnings/DrawingStormtrooper.java @@ -7,7 +7,7 @@ import Entities.*; import java.awt.*; public class DrawingStormtrooper extends DrawingBaseStormtrooper{ - public IDrawingEngines drawingEngines =null; + public DrawingStormtrooper(int speed, float weight, Color bodyColor, Color additionalColor,boolean rockets, boolean bombs, boolean engines, int typeOfEngines) { EntityBaseStormtrooper = new EntityStormtrooper(speed, weight, bodyColor, additionalColor, rockets, bombs, engines); if(engines){ @@ -32,6 +32,10 @@ public class DrawingStormtrooper extends DrawingBaseStormtrooper{ _pictureWidth = null; _pictureHeight = null; } + public DrawingStormtrooper(EntityStormtrooper entityStormtrooper, IDrawingEngines drawingEngines){ + this.EntityBaseStormtrooper=entityStormtrooper; + this.drawingEngines = drawingEngines; + } @Override public void DrawTransport(Graphics g) diff --git a/ProjectStormtrooper/src/FormAdditionalCollection.java b/ProjectStormtrooper/src/FormAdditionalCollection.java new file mode 100644 index 0000000..b04373c --- /dev/null +++ b/ProjectStormtrooper/src/FormAdditionalCollection.java @@ -0,0 +1,154 @@ +import CollectionGenericObjects.AdditionalCollection; +import CollectionGenericObjects.AbstractCompany; +import Drawnings.DrawingBaseStormtrooper; +import Drawnings.DrawingStormtrooper; +import Drawnings.Engines.DrawingEngines; +import Drawnings.Engines.DrawingOvalEngines; +import Drawnings.Engines.DrawingTriangleEngines; +import Drawnings.Engines.IDrawingEngines; +import Entities.EntityBaseStormtrooper; +import Entities.EntityStormtrooper; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +public class FormAdditionalCollection extends JFrame { + public DrawingBaseStormtrooper _drawingBaseStormtrooper = null; + private AbstractCompany _company = null; + private CanvasStormtrooper _canvasStormtrooper = new CanvasStormtrooper(); + private AdditionalCollection _additionalCollection = null; + private Random random = new Random(); + private JButton buttonGenerate = new JButton("Создать"); + private JList listEntity = new JList(); + private JList listEngines = new JList(); + public FormAdditionalCollection() { + setTitle("Random stormtrooper"); + setMinimumSize(new Dimension(650,310)); + _additionalCollection = new AdditionalCollection(3,(Class)EntityBaseStormtrooper.class,(Class)IDrawingEngines.class); + AddEntities(); + AddEngines(); + + buttonGenerate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + _drawingBaseStormtrooper = _additionalCollection.CreateAdditionalCollectionStormtrooper(); + _drawingBaseStormtrooper.SetPictureSize(getWidth(), getHeight()); + _drawingBaseStormtrooper.SetPosition(50,50); + _canvasStormtrooper._drawingBaseStormtrooper = _drawingBaseStormtrooper; + _canvasStormtrooper.repaint(); + DrawingBaseStormtrooper copyStormtrooper; + if (_drawingBaseStormtrooper instanceof DrawingStormtrooper) + copyStormtrooper = new DrawingStormtrooper((EntityStormtrooper) _drawingBaseStormtrooper.EntityBaseStormtrooper, _drawingBaseStormtrooper.drawingEngines); + else + copyStormtrooper = new DrawingBaseStormtrooper(_drawingBaseStormtrooper.EntityBaseStormtrooper, _drawingBaseStormtrooper.drawingEngines); + _company._collection.Insert(copyStormtrooper); + FormStormtrooperCollection.canvasShow(); + + String[] data1 = new String[_additionalCollection.CountEntities]; + for (int i = 0; i < _additionalCollection.CountEntities; i++) { + EntityBaseStormtrooper entity = _additionalCollection._collectionEntity[i]; + data1[i] = ToString(entity); + } + String[] data2 = new String[_additionalCollection.CountEngines]; + for (int i = 0; i < _additionalCollection.CountEngines; i++) { + IDrawingEngines engines = _additionalCollection._collectionEngines[i]; + data2[i] = ToString(engines); + } + listEntity.setListData(data1); + listEngines.setListData(data2); + } + }); + buttonGenerate.setBounds(450, 10, 100, 50); + add(buttonGenerate); + listEntity.setBounds(10,200,300,60); + listEngines.setBounds(320,200,300,60); + add(listEntity); + add(listEngines); + add(_canvasStormtrooper); + setVisible(true); + } + private String ToString(EntityBaseStormtrooper entity) { + String str = ""; + if (entity instanceof EntityStormtrooper) str += "EntityWarmlyShip "; + else str += "EntityShip "; + str += entity.getBodyColor().toString(); + return str; + } + private String ToString(IDrawingEngines engines) { + if (engines == null || engines.getNumberOfEngines() == null) + return "Не имеет двигателей"; + String str = ""+engines.getNumberOfEngines(); + str+=" двигателя"; + + return str; + } + public void AddEntities() { + for (int i = 0; i < _additionalCollection.CountEntities; i++) { + random = new Random(); + int speed = random.nextInt(100, 300); + float weight = random.nextInt(1000, 3000); + Color bodycolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)); + EntityBaseStormtrooper entityBaseStormtrooper; + if (random.nextBoolean()) { + entityBaseStormtrooper = new EntityBaseStormtrooper(speed, weight, bodycolor); + } + else { + Color additionalcolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)); + boolean rockets = random.nextBoolean(); + boolean bombs = random.nextBoolean(); + boolean engines = random.nextBoolean(); + entityBaseStormtrooper = new EntityStormtrooper(speed, weight, bodycolor, additionalcolor, rockets, bombs,engines); + } + _additionalCollection.Insert(entityBaseStormtrooper); + } + } + public void AddEngines() { + for (int i = 0; i < _additionalCollection.CountEngines; i++) { + random = new Random(); + EntityBaseStormtrooper entity = _additionalCollection._collectionEntity[i]; + IDrawingEngines drawingEngines = null; + int numberOfEngines = ((int) ((Math.random() * 3) + 1) * 2); + int typeOfEngines = (int) ((Math.random() * 3) + 1); + if (entity instanceof EntityStormtrooper) { + if (((EntityStormtrooper) entity).getEngines()) { + switch (typeOfEngines) { + case 1: + drawingEngines = new DrawingEngines(); + drawingEngines.setAmountOfEngines((int) ((Math.random() * 3) + 1) * 2); + break; + case 2: + drawingEngines = new DrawingTriangleEngines(); + drawingEngines.setAmountOfEngines((int) ((Math.random() * 3) + 1) * 2); + break; + case 3: + drawingEngines = new DrawingOvalEngines(); + drawingEngines.setAmountOfEngines((int) ((Math.random() * 3) + 1) * 2); + break; + } + } + } + if(drawingEngines!=null){ + _additionalCollection.Insert(drawingEngines); + } + } + } + void setCompany(AbstractCompany company) { + this._company = company; + for (int i = 0; i < _additionalCollection.CountEntities; i++){ + EntityBaseStormtrooper entity = _additionalCollection._collectionEntity[i]; + DrawingBaseStormtrooper _listStormtrooper; + if(entity instanceof EntityStormtrooper){ + _listStormtrooper = new DrawingStormtrooper((EntityStormtrooper) entity,_additionalCollection._collectionEngines[i]); + }else{ + _listStormtrooper = new DrawingBaseStormtrooper(entity,_additionalCollection._collectionEngines[i]); + } + _company._collection.Insert(_listStormtrooper); + + + } + } +} \ No newline at end of file diff --git a/ProjectStormtrooper/src/FormStormtrooper.java b/ProjectStormtrooper/src/FormStormtrooper.java index 9eacf7d..547df06 100644 --- a/ProjectStormtrooper/src/FormStormtrooper.java +++ b/ProjectStormtrooper/src/FormStormtrooper.java @@ -1,11 +1,9 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; +import java.awt.event.*; import java.util.Random; + import Drawnings.DirectionType; import Drawnings.DrawingBaseStormtrooper; import Drawnings.DrawingStormtrooper; @@ -20,55 +18,25 @@ public class FormStormtrooper extends JFrame { private final CanvasStormtrooper canvasStormtrooper = new CanvasStormtrooper(); private JComboBox ComboBoxStrategy = new JComboBox(new String []{"К центру", "К краю"}); private JButton ButtonStrategy = new JButton("Шаг"); - private final JButton CreateBaseStormtrooper = new JButton("Создать базовый бомбардировщик"); - private final JButton CreateStormtrooper = new JButton("Создать бомбардировщик"); + private final JButton UpButton = new JButton(); private final JButton DownButton = new JButton(); private final JButton LeftButton = new JButton(); private final JButton RightButton = new JButton(); + public FormStormtrooper(String title, Dimension dimension) { this.title = title; this.dimension = dimension; } - private void CreateObject(String typeOfClass){ - int StartPositionX = (int)(Math.random() * 90 + 10); - int StartPositionY = (int)(Math.random() * 90 + 10); - int speed = (int)(Math.random() * 300 + 100); - float weight = (float) (Math.random() * 3000 + 1000); - Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); - switch (typeOfClass){ - case "Drawnings.DrawingBaseStormtrooper": - canvasStormtrooper._drawingBaseStormtrooper = new DrawingBaseStormtrooper(speed, weight, bodyColor); - canvasStormtrooper._drawingBaseStormtrooper.SetPictureSize(Width, Height); - canvasStormtrooper._drawingBaseStormtrooper.SetPosition( StartPositionX, StartPositionY); - canvasStormtrooper.repaint(); - break; - case "Drawnings.DrawingStormtrooper": - Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); - boolean rockets = new Random().nextBoolean(); - boolean bombs = new Random().nextBoolean(); - boolean engines = new Random().nextBoolean(); - int typeOfEngine = ((int)((Math.random()*3)+1)); - canvasStormtrooper._drawingBaseStormtrooper = new DrawingStormtrooper(speed, weight, bodyColor, additionalColor,rockets, bombs, engines, typeOfEngine); - canvasStormtrooper._drawingBaseStormtrooper.SetPictureSize(Width, Height); - canvasStormtrooper._drawingBaseStormtrooper.SetPosition( StartPositionX, StartPositionY); - canvasStormtrooper.repaint(); - break; - default: - return; - } - _strategy=null; - ComboBoxStrategy.setEnabled(true); - } - public void Init() { + + public void Init(DrawingBaseStormtrooper stormtrooper) { setTitle(title); setMinimumSize(dimension); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - Width = getWidth()-10; + canvasStormtrooper._drawingBaseStormtrooper = stormtrooper; + canvasStormtrooper._drawingBaseStormtrooper.SetPosition((int) (Math.random() * 300 + 100), (int) (Math.random() * 300 + 100)); + Width = getWidth() - 10; Height = getHeight() - 34; - _strategy=null; - CreateStormtrooper.setName("createStormtrooperButton"); - CreateBaseStormtrooper.setName("createBaseStormtrooperButton"); + _strategy = null; Icon iconUp = new ImageIcon("Resources\\arrowUp.jpg"); UpButton.setIcon(iconUp); UpButton.setName("UP"); @@ -81,16 +49,14 @@ public class FormStormtrooper extends JFrame { RightButton.setName("RIGHT"); Icon iconRight = new ImageIcon("Resources\\arrowRight.jpg"); RightButton.setIcon(iconRight); - ButtonStrategy.addActionListener(new ActionListener() { + ButtonStrategy.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (canvasStormtrooper._drawingBaseStormtrooper == null) return; - if (ComboBoxStrategy.isEnabled()) - { + if (ComboBoxStrategy.isEnabled()) { int index = ComboBoxStrategy.getSelectedIndex(); - switch(index) - { + switch (index) { case 1: _strategy = new MoveToBorder(); break; @@ -100,40 +66,24 @@ public class FormStormtrooper extends JFrame { default: _strategy = null; break; - }; - if (_strategy == null) - { + } + ; + if (_strategy == null) { return; } _strategy.SetData(new MoveableStormtrooper(canvasStormtrooper._drawingBaseStormtrooper), Width, Height); } - if (_strategy == null) - { + if (_strategy == null) { return; } ComboBoxStrategy.setEnabled(false); _strategy.MakeStep(); - if (_strategy.GetStatus() == StrategyStatus.Finish) - { + if (_strategy.GetStatus() == StrategyStatus.Finish) { ComboBoxStrategy.setEnabled(true); _strategy = null; } } }); - CreateBaseStormtrooper.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - CreateObject("Drawnings.DrawingBaseStormtrooper"); - } - }); - CreateStormtrooper.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - CreateObject("Drawnings.DrawingStormtrooper"); - } - }); - - ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { @@ -154,19 +104,15 @@ public class FormStormtrooper extends JFrame { DownButton.addActionListener(actionListener); LeftButton.addActionListener(actionListener); RightButton.addActionListener(actionListener); - setSize(dimension.width,dimension.height); + setSize(dimension.width, dimension.height); setLayout(null); - canvasStormtrooper.setBounds(0,0, getWidth(), getHeight()); - CreateBaseStormtrooper.setBounds(10, getHeight() - 90, 130, 40); - CreateStormtrooper.setBounds(160,getHeight()-90,130,40); + canvasStormtrooper.setBounds(0, 0, getWidth() - 180, getHeight()); UpButton.setBounds(getWidth() - 180, getHeight() - 210, 70, 70); DownButton.setBounds(getWidth() - 180, getHeight() - 140, 70, 70); RightButton.setBounds(getWidth() - 110, getHeight() - 140, 70, 70); LeftButton.setBounds(getWidth() - 250, getHeight() - 140, 70, 70); ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35); ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25); - add(CreateBaseStormtrooper); - add(CreateStormtrooper); add(ComboBoxStrategy); add(ButtonStrategy); add(UpButton); @@ -177,12 +123,11 @@ public class FormStormtrooper extends JFrame { setVisible(true); addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { - Width = getWidth() -10; + Width = getWidth() - 10; Height = getHeight() - 34; - if (canvasStormtrooper._drawingBaseStormtrooper != null)canvasStormtrooper._drawingBaseStormtrooper.SetPictureSize(Width, Height); - canvasStormtrooper.setBounds(0,0, getWidth(), getHeight()); - CreateBaseStormtrooper.setBounds(10, getHeight() - 90, 130, 40); - CreateStormtrooper.setBounds(160,getHeight()-90,130,40); + if (canvasStormtrooper._drawingBaseStormtrooper != null) + canvasStormtrooper._drawingBaseStormtrooper.SetPictureSize(Width, Height); + canvasStormtrooper.setBounds(0, 0, getWidth(), getHeight()); UpButton.setBounds(getWidth() - 180, getHeight() - 210, 70, 70); DownButton.setBounds(getWidth() - 180, getHeight() - 140, 70, 70); RightButton.setBounds(getWidth() - 110, getHeight() - 140, 70, 70); @@ -192,4 +137,7 @@ public class FormStormtrooper extends JFrame { } }); } + + } + diff --git a/ProjectStormtrooper/src/FormStormtrooperCollection.java b/ProjectStormtrooper/src/FormStormtrooperCollection.java new file mode 100644 index 0000000..c53cc13 --- /dev/null +++ b/ProjectStormtrooper/src/FormStormtrooperCollection.java @@ -0,0 +1,221 @@ +import CollectionGenericObjects.AbstractCompany; +import CollectionGenericObjects.MassiveGenericObjects; +import CollectionGenericObjects.StormtrooperSharingService; +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.Random; + +import static java.lang.Integer.parseInt; + +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 JButton GoToCheckButton = new JButton("Check"); + private JButton RandomButton = new JButton("RandomShip"); + private JButton RefreshButton = new JButton("Refresh"); + private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"}); + private JFormattedTextField MaskedTextField; + 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); + float weight = (float)(Math.random() * 3000 + 1000); + Color bodyColor = getColor(); + DrawingBaseStormtrooper drawingBaseStormtrooper; + switch (typeOfClass) { + case "DrawingBaseStormtrooper": + drawingBaseStormtrooper = new DrawingBaseStormtrooper(speed, weight, bodyColor); + break; + case "DrawingStormtrooper": + Color additionalColor = getColor(); + 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); + break; + default: return; + } + if (_company._collection.Insert(drawingBaseStormtrooper, 0) != -1) { + JOptionPane.showMessageDialog(null, "Объект добавлен"); + canvasShow(); + } + 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 color = JColorChooser.showDialog(this, "Цвет", initializator); + return color; + } + public void Init() { + setTitle(title); + setMinimumSize(dimension); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + MaskFormatter mask = null; + try { + mask = new MaskFormatter("##"); + mask.setPlaceholder("00"); + } catch (ParseException e) { + throw new RuntimeException(e); + } + + MaskedTextField = new JFormattedTextField(mask); + + 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() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawingBaseStormtrooper"); + } + }); + CreateButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawingStormtrooper"); + } + }); + + RemoveButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_company == null || MaskedTextField.getText() == null) { + return; + } + int pos = parseInt(MaskedTextField.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); + JOptionPane.showMessageDialog(null, "Объект удален"); + canvasShow(); + } + else { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект"); + } + } + }); + + GoToCheckButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_company == null) + { + return; + } + DrawingBaseStormtrooper stormtrooper = null; + int counter = 100; + while (stormtrooper == null) + { + stormtrooper = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + if (stormtrooper == null) + { + return; + } + FormStormtrooper form = new FormStormtrooper("Бомбардировщик", new Dimension(1000,750)); + form.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + canvasShow(); + super.windowClosing(e); + + } + }); + form.Init(stormtrooper); + } + }); + + RefreshButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_company == null) + { + return; + } + canvasShow(); + } + }); + RandomButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + 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); + MaskedTextField.setBounds(getWidth()-190,200,150,30); + RemoveButton.setBounds(getWidth()-190, 240, 150, 30); + GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30); + RandomButton.setBounds(getWidth()-190, 320, 150, 30); + RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30); + + setSize(dimension.width,dimension.height); + setLayout(null); + add(_canvasStormtrooper); + add(ComboBoxCollections); + add(CreateShipButton); + add(CreateButton); + add(MaskedTextField); + 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); + MaskedTextField.setBounds(getWidth()-190,200,150,30); + RemoveButton.setBounds(getWidth()-190, 240, 150, 30); + GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30); + RandomButton.setBounds(getWidth()-190, 320, 150, 30); + RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30); + } + }); + } +} \ No newline at end of file diff --git a/ProjectStormtrooper/src/Main.java b/ProjectStormtrooper/src/Main.java index d12a463..662a188 100644 --- a/ProjectStormtrooper/src/Main.java +++ b/ProjectStormtrooper/src/Main.java @@ -2,7 +2,7 @@ import java.awt.*; public class Main { public static void main(String[] args) { - FormStormtrooper form = new FormStormtrooper("Бoмбардировщик", new Dimension(800,800)); + FormStormtrooperCollection form = new FormStormtrooperCollection("Бoмбардировщик", new Dimension(1100,750)); form.Init(); } } \ No newline at end of file -- 2.25.1 From 5f3ecf6c51ce6513678b1e22b8aac2e7d1207967 Mon Sep 17 00:00:00 2001 From: ilyaryabovv Date: Sat, 27 Apr 2024 17:38:24 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=BB=D0=B0=D0=B13.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/FormAdditionalCollection.java | 97 +++++++++---------- ProjectStormtrooper/src/FormStormtrooper.java | 8 +- .../src/FormStormtrooperCollection.java | 31 +++--- 3 files changed, 64 insertions(+), 72 deletions(-) diff --git a/ProjectStormtrooper/src/FormAdditionalCollection.java b/ProjectStormtrooper/src/FormAdditionalCollection.java index b04373c..4719570 100644 --- a/ProjectStormtrooper/src/FormAdditionalCollection.java +++ b/ProjectStormtrooper/src/FormAdditionalCollection.java @@ -17,55 +17,54 @@ import java.util.Random; public class FormAdditionalCollection extends JFrame { public DrawingBaseStormtrooper _drawingBaseStormtrooper = null; + private DrawingBaseStormtrooper copyStormtrooper = null; private AbstractCompany _company = null; private CanvasStormtrooper _canvasStormtrooper = new CanvasStormtrooper(); private AdditionalCollection _additionalCollection = null; private Random random = new Random(); private JButton buttonGenerate = new JButton("Создать"); + private JButton buttonGoToCollection = new JButton("В коллекцию"); private JList listEntity = new JList(); private JList listEngines = new JList(); public FormAdditionalCollection() { - setTitle("Random stormtrooper"); - setMinimumSize(new Dimension(650,310)); + setTitle("Случайные бомбардировщики"); + setMinimumSize(new Dimension(970,310)); _additionalCollection = new AdditionalCollection(3,(Class)EntityBaseStormtrooper.class,(Class)IDrawingEngines.class); AddEntities(); AddEngines(); - - buttonGenerate.addActionListener(new ActionListener() { + buttonGoToCollection.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + if(_drawingBaseStormtrooper!=null){ + _company._collection.Insert(copyStormtrooper); + FormStormtrooperCollection.canvasShow(); + } + } + }); + buttonGenerate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { _drawingBaseStormtrooper = _additionalCollection.CreateAdditionalCollectionStormtrooper(); _drawingBaseStormtrooper.SetPictureSize(getWidth(), getHeight()); - _drawingBaseStormtrooper.SetPosition(50,50); + _drawingBaseStormtrooper.SetPosition(360,30); _canvasStormtrooper._drawingBaseStormtrooper = _drawingBaseStormtrooper; _canvasStormtrooper.repaint(); - DrawingBaseStormtrooper copyStormtrooper; if (_drawingBaseStormtrooper instanceof DrawingStormtrooper) copyStormtrooper = new DrawingStormtrooper((EntityStormtrooper) _drawingBaseStormtrooper.EntityBaseStormtrooper, _drawingBaseStormtrooper.drawingEngines); else copyStormtrooper = new DrawingBaseStormtrooper(_drawingBaseStormtrooper.EntityBaseStormtrooper, _drawingBaseStormtrooper.drawingEngines); - _company._collection.Insert(copyStormtrooper); - FormStormtrooperCollection.canvasShow(); - String[] data1 = new String[_additionalCollection.CountEntities]; - for (int i = 0; i < _additionalCollection.CountEntities; i++) { - EntityBaseStormtrooper entity = _additionalCollection._collectionEntity[i]; - data1[i] = ToString(entity); - } - String[] data2 = new String[_additionalCollection.CountEngines]; - for (int i = 0; i < _additionalCollection.CountEngines; i++) { - IDrawingEngines engines = _additionalCollection._collectionEngines[i]; - data2[i] = ToString(engines); - } - listEntity.setListData(data1); - listEngines.setListData(data2); + + } }); - buttonGenerate.setBounds(450, 10, 100, 50); + buttonGoToCollection.setBounds(830,200,120,60); + buttonGenerate.setBounds(830, 130, 120, 60); + listEntity.setBounds(10,200,400,60); + listEngines.setBounds(420,200,400,60); add(buttonGenerate); - listEntity.setBounds(10,200,300,60); - listEngines.setBounds(320,200,300,60); + add(buttonGoToCollection); add(listEntity); add(listEngines); add(_canvasStormtrooper); @@ -73,8 +72,8 @@ public class FormAdditionalCollection extends JFrame { } private String ToString(EntityBaseStormtrooper entity) { String str = ""; - if (entity instanceof EntityStormtrooper) str += "EntityWarmlyShip "; - else str += "EntityShip "; + if (entity instanceof EntityStormtrooper) str += "EntityStormtrooper "; + else str += "EntityBaseStormtrooper "; str += entity.getBodyColor().toString(); return str; } @@ -115,20 +114,20 @@ public class FormAdditionalCollection extends JFrame { int typeOfEngines = (int) ((Math.random() * 3) + 1); if (entity instanceof EntityStormtrooper) { if (((EntityStormtrooper) entity).getEngines()) { - switch (typeOfEngines) { - case 1: - drawingEngines = new DrawingEngines(); - drawingEngines.setAmountOfEngines((int) ((Math.random() * 3) + 1) * 2); - break; - case 2: - drawingEngines = new DrawingTriangleEngines(); - drawingEngines.setAmountOfEngines((int) ((Math.random() * 3) + 1) * 2); - break; - case 3: - drawingEngines = new DrawingOvalEngines(); - drawingEngines.setAmountOfEngines((int) ((Math.random() * 3) + 1) * 2); - break; - } + switch (typeOfEngines) { + case 1: + drawingEngines = new DrawingEngines(); + drawingEngines.setAmountOfEngines((int) ((Math.random() * 3) + 1) * 2); + break; + case 2: + drawingEngines = new DrawingTriangleEngines(); + drawingEngines.setAmountOfEngines((int) ((Math.random() * 3) + 1) * 2); + break; + case 3: + drawingEngines = new DrawingOvalEngines(); + drawingEngines.setAmountOfEngines((int) ((Math.random() * 3) + 1) * 2); + break; + } } } if(drawingEngines!=null){ @@ -138,17 +137,17 @@ public class FormAdditionalCollection extends JFrame { } void setCompany(AbstractCompany company) { this._company = company; - for (int i = 0; i < _additionalCollection.CountEntities; i++){ + String[] data1 = new String[_additionalCollection.CountEntities]; + for (int i = 0; i < _additionalCollection.CountEntities; i++) { EntityBaseStormtrooper entity = _additionalCollection._collectionEntity[i]; - DrawingBaseStormtrooper _listStormtrooper; - if(entity instanceof EntityStormtrooper){ - _listStormtrooper = new DrawingStormtrooper((EntityStormtrooper) entity,_additionalCollection._collectionEngines[i]); - }else{ - _listStormtrooper = new DrawingBaseStormtrooper(entity,_additionalCollection._collectionEngines[i]); - } - _company._collection.Insert(_listStormtrooper); - - + data1[i] = ToString(entity); } + String[] data2 = new String[_additionalCollection.CountEngines]; + for (int i = 0; i < _additionalCollection.CountEngines; i++) { + IDrawingEngines engines = _additionalCollection._collectionEngines[i]; + data2[i] = ToString(engines); + } + listEntity.setListData(data1); + listEngines.setListData(data2); } } \ No newline at end of file diff --git a/ProjectStormtrooper/src/FormStormtrooper.java b/ProjectStormtrooper/src/FormStormtrooper.java index 547df06..0da67a7 100644 --- a/ProjectStormtrooper/src/FormStormtrooper.java +++ b/ProjectStormtrooper/src/FormStormtrooper.java @@ -37,17 +37,17 @@ public class FormStormtrooper extends JFrame { Width = getWidth() - 10; Height = getHeight() - 34; _strategy = null; - Icon iconUp = new ImageIcon("Resources\\arrowUp.jpg"); + Icon iconUp = new ImageIcon("E:\\ООП\\Java\\Lab3\\ProjectStormtrooper\\Resources\\arrowUp.jpg"); UpButton.setIcon(iconUp); UpButton.setName("UP"); DownButton.setName("DOWN"); - Icon iconDown = new ImageIcon("Resources\\arrowDown.jpg"); + Icon iconDown = new ImageIcon("E:\\ООП\\Java\\Lab3\\ProjectStormtrooper\\Resources\\arrowDown.jpg"); DownButton.setIcon(iconDown); LeftButton.setName("LEFT"); - Icon iconLeft = new ImageIcon("Resources\\arrowLeft.jpg"); + Icon iconLeft = new ImageIcon("E:\\ООП\\Java\\Lab3\\ProjectStormtrooper\\Resources\\arrowLeft.jpg"); LeftButton.setIcon(iconLeft); RightButton.setName("RIGHT"); - Icon iconRight = new ImageIcon("Resources\\arrowRight.jpg"); + Icon iconRight = new ImageIcon("E:\\ООП\\Java\\Lab3\\ProjectStormtrooper\\Resources\\arrowRight.jpg"); RightButton.setIcon(iconRight); ButtonStrategy.addActionListener(new ActionListener() { diff --git a/ProjectStormtrooper/src/FormStormtrooperCollection.java b/ProjectStormtrooper/src/FormStormtrooperCollection.java index c53cc13..801662a 100644 --- a/ProjectStormtrooper/src/FormStormtrooperCollection.java +++ b/ProjectStormtrooper/src/FormStormtrooperCollection.java @@ -20,11 +20,11 @@ public class FormStormtrooperCollection extends JFrame{ private JButton CreateButton = new JButton("Создать бомбардировщик");; private JButton CreateShipButton = new JButton("Создать базовый бомбардировщик"); private JButton RemoveButton = new JButton("Удалить"); - private JButton GoToCheckButton = new JButton("Check"); - private JButton RandomButton = new JButton("RandomShip"); - private JButton RefreshButton = new JButton("Refresh"); + private JButton GoToCheckButton = new JButton("На проверку"); + private JButton RandomButton = new JButton("Случайные"); + private JButton RefreshButton = new JButton("Обновить"); private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"}); - private JFormattedTextField MaskedTextField; + private JFormattedTextField TextField; public FormStormtrooperCollection(String title, Dimension dimension) { this.title = title; this.dimension = dimension; @@ -72,15 +72,8 @@ public class FormStormtrooperCollection extends JFrame{ setMinimumSize(dimension); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - MaskFormatter mask = null; - try { - mask = new MaskFormatter("##"); - mask.setPlaceholder("00"); - } catch (ParseException e) { - throw new RuntimeException(e); - } - MaskedTextField = new JFormattedTextField(mask); + TextField = new JFormattedTextField(); ComboBoxCollections.addActionListener(new ActionListener() { @Override @@ -109,10 +102,10 @@ public class FormStormtrooperCollection extends JFrame{ RemoveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_company == null || MaskedTextField.getText() == null) { + if (_company == null || TextField.getText() == null) { return; } - int pos = parseInt(MaskedTextField.getText()); + 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) { @@ -185,10 +178,10 @@ public class FormStormtrooperCollection extends JFrame{ ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20); CreateShipButton.setBounds(getWidth()-190, 60, 150, 30); CreateButton.setBounds(getWidth()-190, 100, 150, 30); - MaskedTextField.setBounds(getWidth()-190,200,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); - RandomButton.setBounds(getWidth()-190, 320, 150, 30); RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30); setSize(dimension.width,dimension.height); @@ -197,7 +190,7 @@ public class FormStormtrooperCollection extends JFrame{ add(ComboBoxCollections); add(CreateShipButton); add(CreateButton); - add(MaskedTextField); + add(TextField); add(RemoveButton); add(GoToCheckButton); add(RandomButton); @@ -210,10 +203,10 @@ public class FormStormtrooperCollection extends JFrame{ ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20); CreateShipButton.setBounds(getWidth()-190, 60, 150, 30); CreateButton.setBounds(getWidth()-190, 100, 150, 30); - MaskedTextField.setBounds(getWidth()-190,200,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, 320, 150, 30); + RandomButton.setBounds(getWidth()-190, 140, 150, 30); RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30); } }); -- 2.25.1 From 4ea097cdb5a65aa4264ef32b89a9f0456d7a80e4 Mon Sep 17 00:00:00 2001 From: ilyaryabovv Date: Sun, 28 Apr 2024 13:45:08 +0400 Subject: [PATCH 3/4] =?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 -- 2.25.1 From 8571cb9ea6a076af2b851743ac782f5a41a8eb8d Mon Sep 17 00:00:00 2001 From: ilyaryabovv Date: Sun, 12 May 2024 13:48:17 +0400 Subject: [PATCH 4/4] =?UTF-8?q?Revert=20"=D0=BB=D0=B0=D0=B14"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 4ea097cdb5a65aa4264ef32b89a9f0456d7a80e4. --- .../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, 118 insertions(+), 259 deletions(-) delete mode 100644 ProjectStormtrooper/src/CollectionGenericObjects/CollectionType.java delete mode 100644 ProjectStormtrooper/src/CollectionGenericObjects/ListGenericObjects.java delete mode 100644 ProjectStormtrooper/src/CollectionGenericObjects/StorageCollection.java diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/AbstractCompany.java b/ProjectStormtrooper/src/CollectionGenericObjects/AbstractCompany.java index b1a22d8..f04184c 100644 --- a/ProjectStormtrooper/src/CollectionGenericObjects/AbstractCompany.java +++ b/ProjectStormtrooper/src/CollectionGenericObjects/AbstractCompany.java @@ -18,7 +18,8 @@ public abstract class AbstractCompany { _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = collection; - _collection.SetMaxCount(GetMaxCount()); + System.out.println(_pictureHeight+" "+_pictureWidth+" "+_placeSizeHeight+" "+_placeSizeWidth); + _collection.SetMaxCount(GetMaxCount(), (Class) DrawingBaseStormtrooper.class); } //Перегрузок нет public DrawingBaseStormtrooper GetRandomObject() diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/CollectionType.java b/ProjectStormtrooper/src/CollectionGenericObjects/CollectionType.java deleted file mode 100644 index bba944c..0000000 --- a/ProjectStormtrooper/src/CollectionGenericObjects/CollectionType.java +++ /dev/null @@ -1,7 +0,0 @@ -package CollectionGenericObjects; - -public enum CollectionType { - None, - Massive, - List -} diff --git a/ProjectStormtrooper/src/CollectionGenericObjects/ICollectionGenericObjects.java b/ProjectStormtrooper/src/CollectionGenericObjects/ICollectionGenericObjects.java index b0f2825..d9e4279 100644 --- a/ProjectStormtrooper/src/CollectionGenericObjects/ICollectionGenericObjects.java +++ b/ProjectStormtrooper/src/CollectionGenericObjects/ICollectionGenericObjects.java @@ -4,8 +4,9 @@ package CollectionGenericObjects; public interface ICollectionGenericObjects { int getCount(); - void SetMaxCount(int count); + void SetMaxCount(int count, Class type); 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 deleted file mode 100644 index c7dbf28..0000000 --- a/ProjectStormtrooper/src/CollectionGenericObjects/ListGenericObjects.java +++ /dev/null @@ -1,41 +0,0 @@ -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 cdc06ab..d3f6753 100644 --- a/ProjectStormtrooper/src/CollectionGenericObjects/MassiveGenericObjects.java +++ b/ProjectStormtrooper/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -1,19 +1,15 @@ package CollectionGenericObjects; -import Drawnings.DrawingBaseStormtrooper; - import java.lang.reflect.Array; public class MassiveGenericObjects implements ICollectionGenericObjects{ - private T[] _collection = null; + private T[] _collection; private int Count; - public void SetMaxCount(int size) { + public void SetMaxCount(int size, Class type) { if (size > 0) { - if (_collection == null) { - _collection = (T[]) Array.newInstance((Class) DrawingBaseStormtrooper.class, size); - Count = size; - } + _collection = (T[]) Array.newInstance(type, size); + Count = size; } } @Override @@ -35,6 +31,36 @@ 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 deleted file mode 100644 index bf56bdc..0000000 --- a/ProjectStormtrooper/src/CollectionGenericObjects/StorageCollection.java +++ /dev/null @@ -1,39 +0,0 @@ -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 2a9311f..de43749 100644 --- a/ProjectStormtrooper/src/CollectionGenericObjects/StormtrooperSharingService.java +++ b/ProjectStormtrooper/src/CollectionGenericObjects/StormtrooperSharingService.java @@ -26,13 +26,16 @@ 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 fb17cac..801662a 100644 --- a/ProjectStormtrooper/src/FormStormtrooperCollection.java +++ b/ProjectStormtrooper/src/FormStormtrooperCollection.java @@ -1,54 +1,43 @@ -import CollectionGenericObjects.*; +import CollectionGenericObjects.AbstractCompany; +import CollectionGenericObjects.MassiveGenericObjects; +import CollectionGenericObjects.StormtrooperSharingService; import Drawnings.DrawingBaseStormtrooper; import Drawnings.DrawingStormtrooper; import javax.swing.*; +import javax.swing.text.MaskFormatter; import java.awt.*; import java.awt.event.*; -import java.util.LinkedList; -import java.util.Queue; +import java.text.ParseException; 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 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 CreateButton = new JButton("Создать бомбардировщик");; + private JButton CreateShipButton = new JButton("Создать базовый бомбардировщик"); + private JButton RemoveButton = 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) { @@ -60,26 +49,24 @@ 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) != -1) { + if (_company._collection.Insert(drawingBaseStormtrooper, 0) != -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); @@ -88,58 +75,73 @@ public class FormStormtrooperCollection extends JFrame { 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()); + break; + } + } + }); + + 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; - DrawingBaseStormtrooper obj = _storageCollection.remove( - listBoxCollection.getSelectedValue().toString(), pos); - if (obj != null) { + if (_company._collection.Remove(pos) != null) { + System.out.println(pos); 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) { @@ -155,7 +157,8 @@ public class FormStormtrooperCollection extends JFrame { RefreshButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_company == null) { + if (_company == null) + { return; } canvasShow(); @@ -164,136 +167,48 @@ 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() { - @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); + 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); - } - private void RerfreshListBoxItems() { - DefaultListModel list = new DefaultListModel(); - for (String name : _storageCollection.Keys()) { - if (name != "") { - list.addElement(name); + + 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); } - } - listBoxCollection.setModel(list); + }); } } \ No newline at end of file -- 2.25.1