From ec302cae53fac3d175e9e9edcba061d1fa909610 Mon Sep 17 00:00:00 2001 From: sonyazubkova Date: Sat, 8 Jun 2024 09:21:16 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=963?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/src.iml | 11 - .../AbstractCompany.java | 54 +++++ .../AdditionalCollection.java | 61 +++++ .../ICollectionGenericObjects.java | 10 + .../MassiveGenericObjects.java | 80 +++++++ .../TruckSharingService.java | 49 ++++ .../Drawings/CanvasFormTruckCollection.java | 31 +++ src/src/Drawings/DrawingGasolineTanker.java | 17 +- src/src/Drawings/DrawingTruck.java | 38 ++- src/src/Entities/EntityGasolineTanker.java | 2 +- src/src/FormAdditionalCollection.form | 13 ++ src/src/FormAdditionalCollection.java | 177 ++++++++++++++ src/src/FormGasolineTanker.form | 16 -- src/src/FormGasolineTanker.java | 61 +---- src/src/FormTruckCollection.form | 26 +++ src/src/FormTruckCollection.java | 219 ++++++++++++++++++ src/src/Main.java | 6 +- .../MovementStrategy/AbstractStrategy.java | 1 - .../MovementStrategy/IMoveableObjects.java | 2 +- src/src/Wheels/DrawingWheels.java | 1 - src/src/Wheels/IDrawingWheels.java | 2 +- 21 files changed, 768 insertions(+), 109 deletions(-) delete mode 100644 src/src.iml create mode 100644 src/src/CollectionGenericObjects/AbstractCompany.java create mode 100644 src/src/CollectionGenericObjects/AdditionalCollection.java create mode 100644 src/src/CollectionGenericObjects/ICollectionGenericObjects.java create mode 100644 src/src/CollectionGenericObjects/MassiveGenericObjects.java create mode 100644 src/src/CollectionGenericObjects/TruckSharingService.java create mode 100644 src/src/Drawings/CanvasFormTruckCollection.java create mode 100644 src/src/FormAdditionalCollection.form create mode 100644 src/src/FormAdditionalCollection.java create mode 100644 src/src/FormTruckCollection.form create mode 100644 src/src/FormTruckCollection.java diff --git a/src/src.iml b/src/src.iml deleted file mode 100644 index c90834f..0000000 --- a/src/src.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/src/src/CollectionGenericObjects/AbstractCompany.java b/src/src/CollectionGenericObjects/AbstractCompany.java new file mode 100644 index 0000000..3958399 --- /dev/null +++ b/src/src/CollectionGenericObjects/AbstractCompany.java @@ -0,0 +1,54 @@ +package CollectionGenericObjects; +import Drawings.DrawingTruck; +import java.awt.*; + +public abstract class AbstractCompany { + + /// Размер места (ширина) + protected int _placeSizeWidth = 190; + + /// Размер места (высота) + protected int _placeSizeHeight = 100; + + /// Ширина окна + 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; + _collection.SetMaxCount(GetMaxCount(), (Class)DrawingTruck.class); + } + + /// Получение случайного объекта из коллекции + public DrawingTruck GetRandomObject() + { + return _collection.Get((int)(Math.random()*GetMaxCount() + 0)); + } + + /// Вывод заднего фона + public abstract void DrawBackgound(Graphics g); + + /// Расстановка объектов + protected abstract void SetObjectsPosition(); + + public void SetPosition() + { + SetObjectsPosition(); + } + +} diff --git a/src/src/CollectionGenericObjects/AdditionalCollection.java b/src/src/CollectionGenericObjects/AdditionalCollection.java new file mode 100644 index 0000000..6b269c0 --- /dev/null +++ b/src/src/CollectionGenericObjects/AdditionalCollection.java @@ -0,0 +1,61 @@ +package CollectionGenericObjects; +import java.lang.reflect.Array; +import java.util.Random; +import Drawings.DrawingGasolineTanker; +import Drawings.DrawingTruck; +import Entities.EntityGasolineTanker; +import Entities.EntityTruck; +import Wheels.IDrawingWheels; + +public class AdditionalCollection { + public T[] _collectionEntity; + public U[] _collectionWheels; + + public AdditionalCollection(int size, Class type1, Class type2) { + _collectionEntity = (T[]) Array.newInstance(type1, size); + _collectionWheels = (U[]) Array.newInstance(type2, size); + CountEntities = size; + CountWheels = size; + } + + public int CountEntities; + public int CountWheels; + + public int Insert(T entity) { + for (int i = 0; i < CountEntities; ++i) { + if (_collectionEntity[i] == null) { + _collectionEntity[i] = entity; + return i; + } + } + return -1; + } + + public int Insert(U wheels) { + for (int i = 0; i < CountWheels; ++i) { + if (_collectionWheels[i] == null) { + _collectionWheels[i] = wheels; + return i; + } + } + return -1; + } + + public DrawingTruck CreateAdditionalCollectionTruck() { + Random random = new Random(); + if (_collectionEntity == null || _collectionWheels == null) { + return null; + } + T entity = _collectionEntity[random.nextInt(CountEntities)]; + U wheels = _collectionWheels[random.nextInt(CountWheels)]; + DrawingTruck drawingTruck = null; + if (entity instanceof EntityGasolineTanker) { + drawingTruck = new DrawingGasolineTanker((EntityGasolineTanker) entity, wheels); + + } else { + drawingTruck = new DrawingTruck(entity, wheels); + } + return drawingTruck; + + } +} diff --git a/src/src/CollectionGenericObjects/ICollectionGenericObjects.java b/src/src/CollectionGenericObjects/ICollectionGenericObjects.java new file mode 100644 index 0000000..3d3772b --- /dev/null +++ b/src/src/CollectionGenericObjects/ICollectionGenericObjects.java @@ -0,0 +1,10 @@ +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/src/src/CollectionGenericObjects/MassiveGenericObjects.java b/src/src/CollectionGenericObjects/MassiveGenericObjects.java new file mode 100644 index 0000000..edeb0ea --- /dev/null +++ b/src/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -0,0 +1,80 @@ +package CollectionGenericObjects; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.List; + +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 T Get(int position) { + if (position >= getCount() || position < 0) return null; + return (T) _collection[position]; + } + @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; + } +} diff --git a/src/src/CollectionGenericObjects/TruckSharingService.java b/src/src/CollectionGenericObjects/TruckSharingService.java new file mode 100644 index 0000000..06c474e --- /dev/null +++ b/src/src/CollectionGenericObjects/TruckSharingService.java @@ -0,0 +1,49 @@ +package CollectionGenericObjects; +import Drawings.DrawingTruck; +import java.awt.*; +public class TruckSharingService extends AbstractCompany { + public TruckSharingService(int picWidth, int picHeight, ICollectionGenericObjects collection) { + super(picWidth, picHeight, collection); + } + + @Override + public void DrawBackgound(Graphics g) { + int count_width = _pictureWidth / _placeSizeWidth; // кол-во мест в ширину + int count_height = _pictureHeight / _placeSizeHeight; + g.setColor(Color.BLACK); + for (int i = 0; i < count_width; i++) { + for (int j = 0; j < count_height+1; ++j) { + g.drawLine(i * _placeSizeWidth + 10, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth - 50, j * _placeSizeHeight); // вертикаль + g.drawLine(i * _placeSizeWidth + 10, j * _placeSizeHeight, i * _placeSizeWidth + 10, j * _placeSizeHeight + _placeSizeHeight); + + } + } + } + + @Override + protected void SetObjectsPosition() { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + int positionWidth = 0; + int positionHeight = height - 1; + for (int i = 0; i < (_collection.getCount()); i++) { + if (_collection.Get(i) != null) { + _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight); + _collection.Get(i).SetPosition(_placeSizeWidth * positionWidth + 20, positionHeight * _placeSizeHeight + 5); + + } + + if (positionWidth < width - 1) { + positionWidth++; + } + else { + positionWidth = 0; + positionHeight--; + } + if (positionHeight < 0) { + return; + } + } + } +} + diff --git a/src/src/Drawings/CanvasFormTruckCollection.java b/src/src/Drawings/CanvasFormTruckCollection.java new file mode 100644 index 0000000..6be1814 --- /dev/null +++ b/src/src/Drawings/CanvasFormTruckCollection.java @@ -0,0 +1,31 @@ +package Drawings; +import javax.swing.*; +import CollectionGenericObjects.AbstractCompany; +import CollectionGenericObjects.ICollectionGenericObjects; +import CollectionGenericObjects.MassiveGenericObjects; +import Drawings.DrawingTruck; +import java.awt.*; + +public class CanvasFormTruckCollection extends JComponent { + public AbstractCompany company = null; + public void SetCollectionToCanvas(AbstractCompany company) { + this.company = company; + } + public CanvasFormTruckCollection () {}; + 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 DrawingTruck) { + ((DrawingTruck) obj).DrawTransport(g2d); + } + } + super.repaint(); + } +} diff --git a/src/src/Drawings/DrawingGasolineTanker.java b/src/src/Drawings/DrawingGasolineTanker.java index 7e0d75d..df6838a 100644 --- a/src/src/Drawings/DrawingGasolineTanker.java +++ b/src/src/Drawings/DrawingGasolineTanker.java @@ -1,18 +1,23 @@ package Drawings; import Entities.EntityGasolineTanker; +import Entities.EntityTruck; import Wheels.IDrawingWheels; import java.awt.*; public class DrawingGasolineTanker extends DrawingTruck{ - private IDrawingWheels drawingWheels; - - public DrawingGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean gasTank, boolean signalBeacon, int number) { - super(105,70); - EntityTruck = new EntityGasolineTanker(speed, weight, bodyColor, additionalColor, gasTank, signalBeacon, number); + public DrawingGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean gasTank, boolean signalBeacon) { + //super(105,70); + EntityTruck = new EntityGasolineTanker(speed, weight, bodyColor, additionalColor, gasTank, signalBeacon); DrawWheels(); } - @Override + + public DrawingGasolineTanker(EntityGasolineTanker entity, IDrawingWheels wheels) { + EntityTruck = entity; + drawingWheels = wheels; + } + + @Override public void DrawTransport(Graphics2D g) { if (EntityTruck == null || !(EntityTruck instanceof EntityGasolineTanker gasolineTanker) || _startPosX == null || _startPosY == null) { return; diff --git a/src/src/Drawings/DrawingTruck.java b/src/src/Drawings/DrawingTruck.java index 4db358e..8848733 100644 --- a/src/src/Drawings/DrawingTruck.java +++ b/src/src/Drawings/DrawingTruck.java @@ -4,12 +4,13 @@ import Wheels.DrawingOrnamentSquare; import Wheels.DrawingOrnamentHeart; import Wheels.DrawingWheels; import Wheels.IDrawingWheels; - import javax.swing.*; import java.awt.*; public class DrawingTruck extends JPanel{ - private IDrawingWheels drawingWheels; + + public IDrawingWheels drawingWheels; + public Entities.EntityTruck EntityTruck; // Ширина окна private Integer _pictureWidth; @@ -34,9 +35,10 @@ public class DrawingTruck extends JPanel{ public Integer GetWidth() {return _drawingTruckWidth;} public Integer GetHeight() {return _drawingTruckHeight;} - protected void DrawWheels() { - int number = (int)(Math.random() * 4 + 0); + public void DrawWheels() { + int numWheels = (int)(Math.random() * 4 + 2); switch ((int)(Math.random() * 3 + 1)) { + case 1: drawingWheels = new DrawingWheels(); break; @@ -47,10 +49,10 @@ public class DrawingTruck extends JPanel{ drawingWheels = new DrawingOrnamentHeart(); break; default: - number = 0; + drawingWheels = new DrawingOrnamentSquare(); break; } - drawingWheels.setNumWheels(number); + drawingWheels.setNumWheels(numWheels); } protected DrawingTruck(){ _pictureWidth = null; @@ -59,17 +61,24 @@ public class DrawingTruck extends JPanel{ _startPosY = null; } - public DrawingTruck(int speed, double weight, Color bodyColor, int number){ + public DrawingTruck(int speed, double weight, Color bodyColor){ super(); EntityTruck = new EntityTruck(speed, weight, bodyColor); DrawWheels(); } + public DrawingTruck(EntityTruck entity, IDrawingWheels wheels) { + + EntityTruck = entity; + drawingWheels = wheels; + } + protected DrawingTruck(int drawingTruckWidth, int drawingTruckHeight) { - _drawingTruckWidth = drawingTruckWidth; - _drawingTruckHeight = drawingTruckHeight; + super(); + this._drawingTruckWidth = drawingTruckWidth; + this._drawingTruckHeight = drawingTruckHeight; } @@ -188,8 +197,15 @@ public class DrawingTruck extends JPanel{ { return; } - // колеса - drawingWheels.drawWheels(g, Color.BLACK, _startPosX, _startPosY); + + if (drawingWheels == null) { + g.setColor(Color.BLACK); + g.fillOval(_startPosX + 5, _startPosY + 50, 20, 20); + g.fillOval(_startPosX + 25, _startPosY + 50, 20, 20); + g.fillOval(_startPosX + 85, _startPosY + 50, 20, 20); + } + + else drawingWheels.drawWheels(g, Color.BLACK, _startPosX, _startPosY); // нижняя платформа g.setColor(EntityTruck.getBodyColor()); diff --git a/src/src/Entities/EntityGasolineTanker.java b/src/src/Entities/EntityGasolineTanker.java index 2bfc94f..bd37249 100644 --- a/src/src/Entities/EntityGasolineTanker.java +++ b/src/src/Entities/EntityGasolineTanker.java @@ -15,7 +15,7 @@ public class EntityGasolineTanker extends EntityTruck{ public boolean SignalBeacon; public boolean getSignalBeacon() {return SignalBeacon;} - public EntityGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean signalBeacon, boolean gasTank, int numWheels){ + public EntityGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean signalBeacon, boolean gasTank){ super(speed, weight, bodyColor); AdditionalColor = additionalColor; GasTank = gasTank; diff --git a/src/src/FormAdditionalCollection.form b/src/src/FormAdditionalCollection.form new file mode 100644 index 0000000..105145f --- /dev/null +++ b/src/src/FormAdditionalCollection.form @@ -0,0 +1,13 @@ + +
+ + + + + + + + + + +
diff --git a/src/src/FormAdditionalCollection.java b/src/src/FormAdditionalCollection.java new file mode 100644 index 0000000..3343517 --- /dev/null +++ b/src/src/FormAdditionalCollection.java @@ -0,0 +1,177 @@ +import CollectionGenericObjects.AbstractCompany; +import Drawings.DrawingGasolineTanker; +import Drawings.DrawingTruck; +import Drawings.CanvasGasolineTanker; +import Entities.EntityGasolineTanker; +import Entities.EntityTruck; +import Wheels.DrawingOrnamentHeart; +import Wheels.DrawingOrnamentSquare; +import Wheels.DrawingWheels; +import Wheels.IDrawingWheels; +import CollectionGenericObjects.AdditionalCollection; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; +import java.awt.*; +import javax.swing.*; + +public class FormAdditionalCollection extends JFrame{ + private AbstractCompany company = null; + + public DrawingTruck drawingTruck = null; + private DrawingTruck copyTruck = null; + + private CanvasGasolineTanker canvasTruck = new CanvasGasolineTanker(); + 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 listWheels = new JList(); + public FormAdditionalCollection() { + setTitle("Случайные грузовики"); + setSize(650, 400); + additionalCollection = new AdditionalCollection(3, (Class) EntityTruck.class, (Class) IDrawingWheels.class); + AddEntities(); + AddWheels(); + buttonGoToCollection.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(drawingTruck !=null){ + company._collection.Insert(copyTruck); + FormTruckCollection.canvasShow(); + + } + } + }); + buttonGenerate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + drawingTruck = additionalCollection.CreateAdditionalCollectionTruck(); + drawingTruck.SetPictureSize(getWidth(), getHeight()); + drawingTruck.SetPosition(50, 50); + canvasTruck._drawingTruck = drawingTruck; + canvasTruck.repaint(); + if (drawingTruck instanceof DrawingGasolineTanker) + copyTruck = new DrawingGasolineTanker((EntityGasolineTanker) drawingTruck.EntityTruck, drawingTruck.drawingWheels); + else + copyTruck = new DrawingTruck(drawingTruck.EntityTruck, drawingTruck.drawingWheels); + + + String[] data1 = new String[additionalCollection.CountEntities]; + for (int i = 0; i < additionalCollection.CountEntities; i++) { + EntityTruck entity = additionalCollection._collectionEntity[i]; + data1[i] = ToString(entity); + } + String[] data2 = new String[additionalCollection.CountWheels]; + for (int i = 0; i < additionalCollection.CountWheels; i++) { + IDrawingWheels wheels = additionalCollection._collectionWheels[i]; + data2[i] = ToString(wheels); + } + listEntity.setListData(data1); + listWheels.setListData(data2); + + } + }); + buttonGoToCollection.setBounds(300,300,150,40); + buttonGenerate.setBounds(130, 300, 150, 40); + add(buttonGenerate); + add(buttonGoToCollection); + listEntity.setBounds(10, 200, 300, 60); + listWheels.setBounds(320, 200, 300, 60); + + add(listEntity); + add(listWheels); + add(canvasTruck); + setVisible(true); + } + + + private String ToString(EntityTruck entity) { + String str = ""; + if (entity instanceof EntityGasolineTanker) str += "EntityGasolineTanker "; + else str += "EntityTruck "; + str += entity.getBodyColor().toString(); + return str; + } + + private String ToString(IDrawingWheels wheels) { + if (wheels == null || wheels.getNumWheels() == null) + return "Simple Wheels"; + + String str = "Wheels "; + if (wheels instanceof DrawingWheels) str += "Simple Wheels "; + else if (wheels instanceof DrawingOrnamentHeart) str += "OrnamentHeart "; + else if (wheels instanceof DrawingOrnamentSquare) str += "OrnamentSquare ";; + str += wheels.getNumWheels().toString(); + return str; + + } + + public void AddEntities() { + for (int i = 0; i < additionalCollection.CountEntities; i++) { + random = new Random(); + int speed = random.nextInt(100, 300); + double weight = random.nextInt(1000, 3000); + //int number = random.nextInt(0,4); + Color bodyColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)); + EntityTruck entityTruck; + if (random.nextBoolean()) { + entityTruck = new EntityTruck(speed, weight, bodyColor); + + } + else { + Color additionalColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)); + boolean gasTank = random.nextBoolean(); + boolean signalBeacon = random.nextBoolean(); + entityTruck = new EntityGasolineTanker(speed, weight, bodyColor, additionalColor, gasTank, signalBeacon); + } + additionalCollection.Insert(entityTruck); + } + } + public void AddWheels() { + for (int i = 0; i < additionalCollection.CountWheels; i++) { + random = new Random(); + EntityTruck entity = additionalCollection._collectionEntity[i]; + Integer number = random.nextInt(0, 4); + IDrawingWheels drawingWheels = null; + switch (random.nextInt(0,4)) { + case 1: + drawingWheels = new DrawingWheels(); + break; + case 2: + drawingWheels = new DrawingOrnamentSquare(); + break; + case 3: + drawingWheels = new DrawingOrnamentHeart(); + break; + default: + number = null; + break; + } + if (drawingWheels != null) drawingWheels.setNumWheels(number); + additionalCollection.Insert(drawingWheels); + } + } + + void setCompany(AbstractCompany company){ + this.company = company; + String[] data1 = new String[additionalCollection.CountEntities]; + for (int i = 0; i < additionalCollection.CountEntities; i++) { + EntityTruck entity = additionalCollection._collectionEntity[i]; + data1[i] = ToString(entity); + } + String[] data2 = new String[additionalCollection.CountWheels]; + for (int i = 0; i < additionalCollection.CountWheels; i++) { + IDrawingWheels wheels = additionalCollection._collectionWheels[i]; + data2[i] = ToString(wheels); + } + listEntity.setListData(data1); + listWheels.setListData(data2); + } +} + + + diff --git a/src/src/FormGasolineTanker.form b/src/src/FormGasolineTanker.form index 0311adc..0c4be64 100644 --- a/src/src/FormGasolineTanker.form +++ b/src/src/FormGasolineTanker.form @@ -8,14 +8,6 @@ - - - - - - - - @@ -57,14 +49,6 @@ - - - - - - - - diff --git a/src/src/FormGasolineTanker.java b/src/src/FormGasolineTanker.java index fc78a60..7dcba09 100644 --- a/src/src/FormGasolineTanker.java +++ b/src/src/FormGasolineTanker.java @@ -14,46 +14,16 @@ public class FormGasolineTanker extends JFrame{ private int Width; private int Height; private CanvasGasolineTanker canvasGasolineTanker = new CanvasGasolineTanker(); - private JButton buttonCreateTruck = new JButton("Создать грузовик"); private JButton buttonRight = new JButton(); private JButton buttonLeft = new JButton(); private JButton buttonDown = new JButton(); private JButton buttonUp = new JButton(); private AbstractStrategy _strategy; - private JButton buttonCreateGasolineTanker = new JButton("Создать бензовоз"); + private JComboBox comboBoxStrategy = new JComboBox(new String[] {"К центру", "К краю"}); private JButton buttonStrategy = new JButton("Шаг"); - private void CreateObject(String typeOfClass){ - int StartPositionX = 10 + (int)(Math.random() *((100-10) +1)); - int StartPositionY = 10 + (int)(Math.random() *((100-10) +1)); - int speed = 10 + (int)(Math.random() *((100-10) +1)); - double weight = 1000 + (double)(Math.random() *((5000-1000) +1)); - Color bodyColor = new Color((int) (Math.random() * ((256) + 1)), (int) (Math.random() * ((256) + 1)), (int) (Math.random() * ((256) + 1))); - int number = new Random().nextInt(3)+1; - switch (typeOfClass){ - case "DrawingTruck": - canvasGasolineTanker._drawingTruck = new DrawingTruck(speed, weight, bodyColor, number); - canvasGasolineTanker._drawingTruck.SetPictureSize(Width,Height); - canvasGasolineTanker._drawingTruck.SetPosition(StartPositionX, StartPositionY); - canvasGasolineTanker.repaint(); - break; - case "DrawingGasolineTanker": - Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); - boolean gasTank = new Random().nextBoolean(); - boolean signalBeacon = new Random().nextBoolean(); - canvasGasolineTanker._drawingTruck = new DrawingGasolineTanker(speed, weight, bodyColor, additionalColor, signalBeacon, gasTank, number); - canvasGasolineTanker._drawingTruck.SetPictureSize(Width,Height); - canvasGasolineTanker._drawingTruck.SetPosition(StartPositionX, StartPositionY); - canvasGasolineTanker.repaint(); - break; - default: return; - } - _strategy = null; - comboBoxStrategy.setEnabled(true); - } - - public void Init(){ + public void Init(DrawingTruck truck){ setTitle("Бензовоз"); setSize(900,600); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); @@ -62,8 +32,7 @@ public class FormGasolineTanker extends JFrame{ Height = getHeight(); _strategy = null; - buttonCreateTruck.setName("CREATETRUCK"); - buttonCreateGasolineTanker.setName("CREATEGASOLINETANKER"); + canvasGasolineTanker._drawingTruck = truck; buttonUp.setName("buttonUp"); Icon iconUp = new ImageIcon("src\\src\\resources\\Up.jpg"); @@ -81,19 +50,6 @@ public class FormGasolineTanker extends JFrame{ Icon iconRight = new ImageIcon("src\\src\\resources\\Right.jpg"); buttonRight.setIcon(iconRight); - buttonCreateTruck.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - CreateObject("DrawingTruck"); - } - }); - buttonCreateGasolineTanker.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - CreateObject("DrawingGasolineTanker"); - } - }); - buttonStrategy.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -161,9 +117,6 @@ public class FormGasolineTanker extends JFrame{ setLayout(null); canvasGasolineTanker.setBounds(0,0, getWidth(), getHeight()); - buttonCreateTruck.setBounds(10, getHeight() - 90, 150, 40); - buttonCreateGasolineTanker.setBounds(170, getHeight() - 90, 150, 40); - buttonUp.setBounds(getWidth() - 200, getHeight() - 160, 45, 45); buttonDown.setBounds(getWidth() - 140, getHeight() - 100, 45, 45); buttonRight.setBounds(getWidth() - 80, getHeight() - 100, 45, 45); @@ -171,8 +124,7 @@ public class FormGasolineTanker extends JFrame{ comboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35); buttonStrategy.setBounds(getWidth() - 130, 55, 100, 25); - add(buttonCreateTruck); - add(buttonCreateGasolineTanker); + add(buttonUp); add(buttonDown); add(buttonRight); @@ -189,19 +141,14 @@ public class FormGasolineTanker extends JFrame{ if (canvasGasolineTanker._drawingTruck != null) canvasGasolineTanker._drawingTruck.SetPictureSize(Width, Height); canvasGasolineTanker.setBounds(0,0, getWidth(), getHeight()); - buttonCreateTruck.setBounds(10, getHeight() - 90, 150, 40); - buttonCreateGasolineTanker.setBounds(170, getHeight() - 90, 150, 40); buttonUp.setBounds(getWidth() - 140, getHeight() - 160, 45, 45); buttonDown.setBounds(getWidth() - 140, getHeight() - 100, 45, 45); buttonRight.setBounds(getWidth() - 80, getHeight() - 100, 45, 45); buttonLeft.setBounds(getWidth() - 200, getHeight() - 100, 45, 45); comboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35); buttonStrategy.setBounds(getWidth() - 130, 55, 100, 25); - } }); } - - } diff --git a/src/src/FormTruckCollection.form b/src/src/FormTruckCollection.form new file mode 100644 index 0000000..b56dcaa --- /dev/null +++ b/src/src/FormTruckCollection.form @@ -0,0 +1,26 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/src/FormTruckCollection.java b/src/src/FormTruckCollection.java new file mode 100644 index 0000000..939b1f9 --- /dev/null +++ b/src/src/FormTruckCollection.java @@ -0,0 +1,219 @@ +import CollectionGenericObjects.AbstractCompany; +import CollectionGenericObjects.MassiveGenericObjects; +import CollectionGenericObjects.TruckSharingService; +import Drawings.CanvasFormTruckCollection; +import Drawings.DrawingTruck; +import Drawings.DrawingGasolineTanker; + +import javax.swing.*; +import javax.swing.text.MaskFormatter; +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.text.ParseException; +import java.util.Random; + +import static java.lang.Integer.parseInt; + +public class FormTruckCollection extends JFrame{ + + public static CanvasFormTruckCollection _canvasGasolineTanker = new CanvasFormTruckCollection(); + private static AbstractCompany _company = null; + private JButton ButtonCreateGasolineTanker = new JButton("Создать бензовоз");; + private JButton ButtonCreateTruck = 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 static void canvasShow() { + _company.SetPosition(); + _canvasGasolineTanker.SetCollectionToCanvas(_company); + _canvasGasolineTanker.repaint(); + } + private void CreateObject(String typeOfClass) { + if (_company == null) return; + int speed = (int)(Math.random() * 300 + 100); + double weight = (double)(Math.random() * 3000 + 1000); + Color bodyColor = getColor(); + DrawingTruck drawingTruck; + switch (typeOfClass) { + case "DrawingTruck": + drawingTruck = new DrawingTruck(speed, weight, bodyColor); + break; + case "DrawingGasolineTanker": + Color additionalColor = getColor(); + boolean gasTank = new Random().nextBoolean(); + boolean signalBeacon = new Random().nextBoolean();; + drawingTruck = new DrawingGasolineTanker(speed, weight, bodyColor, additionalColor, gasTank, signalBeacon); + break; + default: return; + } + if (_company._collection.Insert(drawingTruck, 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("Коллекция грузовиков"); + setSize(920,572); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + MaskFormatter mask = null; + try { + mask = new MaskFormatter("##"); + mask.setPlaceholder("00"); + } catch (ParseException e) { + throw new RuntimeException(e); + } + + TextField = new JFormattedTextField(mask); + + ComboBoxCollections.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + switch (ComboBoxCollections.getSelectedItem().toString()) { + case "Хранилище": + _company = new TruckSharingService(getWidth(), getHeight(), new MassiveGenericObjects()); + break; + } + } + }); + + ButtonCreateTruck.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawingTruck"); + } + }); + ButtonCreateGasolineTanker.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawingGasolineTanker"); + } + }); + + RemoveButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + 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) { + JOptionPane.showMessageDialog(null, "Объект удален"); + canvasShow(); + } + else { + JOptionPane.showMessageDialog(null, "Объект не удалось удалить"); + } + } + }); + + GoToCheckButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_company == null) + { + return; + } + DrawingTruck truck = null; + int counter = 100; + while (truck == null) + { + + truck = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + if (truck == null) + { + return; + } + FormGasolineTanker formGasolineTanker = new FormGasolineTanker(); + formGasolineTanker.Init(truck); + } + }); + + RandomButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_company == null) + { + return; + } + FormAdditionalCollection form = new FormAdditionalCollection(); + form.setCompany(_company); + } + }); + + RefreshButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_company == null) + { + return; + } + canvasShow(); + } + }); + + _canvasGasolineTanker.setBounds(0, 0, getWidth()-200, getHeight()); + ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20); + ButtonCreateTruck.setBounds(getWidth()-190, 60, 150, 30); + ButtonCreateGasolineTanker.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, 320, 150, 30); + RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30); + + + setLayout(null); + add(_canvasGasolineTanker); + add(ComboBoxCollections); + add(ButtonCreateTruck); + add(ButtonCreateGasolineTanker); + add(TextField); + add(RemoveButton); + add(GoToCheckButton); + add(RandomButton); + add(RefreshButton); + setVisible(true); + + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent e) { + _canvasGasolineTanker.setBounds(0, 0, getWidth()-200, getHeight()-70); + ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20); + ButtonCreateTruck.setBounds(getWidth()-190, 60, 150, 30); + ButtonCreateGasolineTanker.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, 320, 150, 30); + RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30); + } + }); + } +} diff --git a/src/src/Main.java b/src/src/Main.java index 0091087..dfb676b 100644 --- a/src/src/Main.java +++ b/src/src/Main.java @@ -1,7 +1,7 @@ import java.awt.*; -public class Main{ +public class Main{ public static void main(String[] args){ - FormGasolineTanker formGasolineTanker = new FormGasolineTanker(); - formGasolineTanker.Init(); + FormTruckCollection formTruckCollection = new FormTruckCollection(); + formTruckCollection.Init(); } } \ No newline at end of file diff --git a/src/src/MovementStrategy/AbstractStrategy.java b/src/src/MovementStrategy/AbstractStrategy.java index 28013b5..cdcb2b2 100644 --- a/src/src/MovementStrategy/AbstractStrategy.java +++ b/src/src/MovementStrategy/AbstractStrategy.java @@ -59,5 +59,4 @@ public abstract class AbstractStrategy { } return false; } - } diff --git a/src/src/MovementStrategy/IMoveableObjects.java b/src/src/MovementStrategy/IMoveableObjects.java index 15e2a32..4ecb751 100644 --- a/src/src/MovementStrategy/IMoveableObjects.java +++ b/src/src/MovementStrategy/IMoveableObjects.java @@ -1,5 +1,5 @@ package MovementStrategy; -// только объявляются методы + public interface IMoveableObjects { ObjectParameters GetObjectPosition(); int GetStep(); diff --git a/src/src/Wheels/DrawingWheels.java b/src/src/Wheels/DrawingWheels.java index 82154b6..a1a2eac 100644 --- a/src/src/Wheels/DrawingWheels.java +++ b/src/src/Wheels/DrawingWheels.java @@ -3,7 +3,6 @@ import java.awt.*; public class DrawingWheels implements IDrawingWheels{ private NumWheels numWheels; - @Override public NumWheels getNumWheels() { return numWheels; diff --git a/src/src/Wheels/IDrawingWheels.java b/src/src/Wheels/IDrawingWheels.java index 7588155..74f9018 100644 --- a/src/src/Wheels/IDrawingWheels.java +++ b/src/src/Wheels/IDrawingWheels.java @@ -3,5 +3,5 @@ import java.awt.*; public interface IDrawingWheels { void setNumWheels(int number); NumWheels getNumWheels(); - void drawWheels(Graphics2D g2d, Color color, int _startPosX, int _startPosY); // + void drawWheels(Graphics2D g2d, Color color, int _startPosX, int _startPosY); } -- 2.25.1