diff --git a/CollectionGenericObjects/AbstractCompany.java b/CollectionGenericObjects/AbstractCompany.java new file mode 100644 index 0000000..f7b2ac5 --- /dev/null +++ b/CollectionGenericObjects/AbstractCompany.java @@ -0,0 +1,37 @@ +package CollectionGenericObjects; +import Drawnings.DrawningTruck; + +import java.awt.*; +import java.util.Random; + +public class AbstractCompany { + protected int _placeSizeWidth = 120; + protected int _placeSizeHeight = 120; + 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(), DrawningTruck.class); + } + public DrawningTruck GetRandomObject() + { + Random random = new Random(); + return _collection.Get(random.nextInt(GetMaxCount())); + } + + public void DrawBackgound(Graphics g) { } + + protected void SetObjectsPosition() { } + + public void SetPosition() + { + SetObjectsPosition(); + } +} diff --git a/CollectionGenericObjects/AdditionalCollection.java b/CollectionGenericObjects/AdditionalCollection.java new file mode 100644 index 0000000..3c227a7 --- /dev/null +++ b/CollectionGenericObjects/AdditionalCollection.java @@ -0,0 +1,65 @@ +package CollectionGenericObjects; + +import DifferenceOfWheels.IOrnaments; +import Drawnings.DrawningDumpTruck; +import Drawnings.DrawningTruck; +import Entities.EntityDumpTruck; +import Entities.EntityTruck; + +import java.lang.reflect.Array; +import java.util.Random; + +public class AdditionalCollection { + public T[] _collectionEntityTruck; + public U[] _collectionIOrnaments; + public int Len_entity; + public int Len_ornament; + public AdditionalCollection(int len, Class entity, Class ornament){ + _collectionEntityTruck = (T[]) Array.newInstance(entity, len); + _collectionIOrnaments = (U[]) Array.newInstance(ornament, len); + Len_entity = len; + Len_ornament = len; + } + + public int Insert(T entity) { + int index = 0; + while (index < Len_entity) { + if (_collectionEntityTruck[index] == null) + { + _collectionEntityTruck[index] = entity; + return index; + } + ++index; + } + return -1; + } + public int Insert(U ornament) { + int index = 0; + while (index < Len_ornament) { + if (_collectionIOrnaments[index] == null) + { + _collectionIOrnaments[index] = ornament; + return index; + } + ++index; + } + return -1; + } + + public DrawningTruck CreateDrawningTruckClass(){ + if (_collectionIOrnaments == null || _collectionEntityTruck == null){ + return null; + } + DrawningTruck _drawningTruck; + Random random = new Random(); + EntityTruck _truck = _collectionEntityTruck[random.nextInt(0, Len_entity)]; + IOrnaments _ornament = _collectionIOrnaments[random.nextInt(0, Len_ornament)]; + if (_truck instanceof EntityDumpTruck){ + _drawningTruck = new DrawningDumpTruck((EntityDumpTruck) _truck, _ornament); + } + else{ + _drawningTruck = new DrawningTruck(_truck, _ornament); + } + return _drawningTruck; + } +} diff --git a/CollectionGenericObjects/ICollectionGenericObjects.java b/CollectionGenericObjects/ICollectionGenericObjects.java new file mode 100644 index 0000000..aa8be0b --- /dev/null +++ b/CollectionGenericObjects/ICollectionGenericObjects.java @@ -0,0 +1,9 @@ +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/CollectionGenericObjects/MassiveGenericObjects.java b/CollectionGenericObjects/MassiveGenericObjects.java new file mode 100644 index 0000000..e3b4dbc --- /dev/null +++ b/CollectionGenericObjects/MassiveGenericObjects.java @@ -0,0 +1,85 @@ +package CollectionGenericObjects; + +import java.lang.reflect.Array; + +public class MassiveGenericObjects implements ICollectionGenericObjects { + + private T[] _collection; + int Count; + + @Override + public int getCount() { + return _collection.length; + } + + @Override + public void SetMaxCount(int count, Class type) { + if (count > 0) { + _collection = (T[]) Array.newInstance(type, count); + Count = count; + } + } + + @Override + public T Get(int position) { + if (position >= getCount() || position < 0) + return null; + return (T) _collection[position]; + } + + @Override + public int Insert(T obj) { + for (int i = 0; i < _collection.length; ++i) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } + } + return -1; + } + + @Override + public int Insert(T obj, int position) { + if (position < 0 || position > Count) + { + return -1; + } + if (_collection[position] == null) + { + _collection[position] = obj; + return position; + } + for (int i = position + 1; i < _collection.length; ++i) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return position; + } + } + for (int i = 0; i < position; ++i) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return position; + } + } + return -1; + } + + @Override + public T Remove(int position) { + if (position >= 0 && position < Count) + { + T remove = _collection[position]; + _collection[position]= null; + return remove; + } + return null; + } + + +} diff --git a/CollectionGenericObjects/TruckShearingService.java b/CollectionGenericObjects/TruckShearingService.java new file mode 100644 index 0000000..c00ee07 --- /dev/null +++ b/CollectionGenericObjects/TruckShearingService.java @@ -0,0 +1,65 @@ +package CollectionGenericObjects; +import Drawnings.DrawningTruck; + +import java.awt.*; + +public class TruckShearingService extends AbstractCompany { + public TruckShearingService(int picWidth, int picHeight, ICollectionGenericObjects collection) + { + super(picWidth, picHeight, collection); + } + + private int start_width = 10, start_height = 20; + private int width_between = 60; + + @Override + public void DrawBackgound(Graphics g) + { + g.setColor(Color.BLACK); + int height = 5, width = 5; + + int maxWidth = _pictureWidth / (_placeSizeWidth + width_between); + int maxHeight = _pictureHeight / _placeSizeHeight; + + for (int i = 0; i < maxWidth; i++) + { + height = 10; + for (int j = 0; j < maxHeight; j++) + { + g.drawLine(width, height, width + _placeSizeWidth, height); + g.drawLine(width, height, width, height + _placeSizeHeight); + height += _placeSizeHeight; + } + g.drawLine(width, height, width + _placeSizeWidth, height); + width = width + _placeSizeWidth + width_between; + } + } + + @Override + protected void SetObjectsPosition() + { + int maxWidth = _pictureWidth / (_placeSizeWidth + width_between); + int maxHeight = _pictureHeight / _placeSizeHeight; + int i_collection = 0; + int x_pos = start_width, y_pos = start_height; + + + if (_collection != null) { + for (int j = 0; j < maxHeight; j++) + { + x_pos = start_width; + for (int i = 0; i < maxWidth; i++) + { + if (_collection.Get(i_collection) != null) + { + _collection.Get(i_collection).SetPictureSize(_pictureWidth, _pictureHeight); + _collection.Get(i_collection).SetPosition(x_pos, y_pos); + x_pos += _placeSizeWidth + width_between; + i_collection++; + } + } + y_pos = y_pos + _placeSizeHeight; + } + } + } +} diff --git a/DifferenceOfWheels/DrawningOrnamentRectangle.java b/DifferenceOfWheels/DrawningOrnamentRectangle.java new file mode 100644 index 0000000..b9ef6a6 --- /dev/null +++ b/DifferenceOfWheels/DrawningOrnamentRectangle.java @@ -0,0 +1,47 @@ +package DifferenceOfWheels; + +import java.awt.*; + +public class DrawningOrnamentRectangle implements IOrnaments{ + int CountWeel; + + @Override + public void SetCount(int n) { + CountWeel = n; + } + + @Override + public Integer get_count_weels() { + return CountWeel; + } + + @Override + public void DrawOrnament(Graphics2D g, int x, int y) { + g.setColor(Color.BLACK); + g.fillOval(x, y, 25, 25); + + g.setColor(Color.PINK); + g.fillRect(x + 5, y + 5, 15, 15); + } + + @Override + public void DrawWeels(Graphics2D g, int x, int y) { + switch (CountWeel){ + case 2: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 3: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 38, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 4: + DrawOrnament(g, x + 5, y + 67); + DrawOrnament(g, x + 32, y + 67); + DrawOrnament(g, x + 61, y + 67); + DrawOrnament(g, x + 88, y + 67); + break; + } + } +} diff --git a/DifferenceOfWheels/DrawningOrnamentStar.java b/DifferenceOfWheels/DrawningOrnamentStar.java new file mode 100644 index 0000000..e6ec3c0 --- /dev/null +++ b/DifferenceOfWheels/DrawningOrnamentStar.java @@ -0,0 +1,57 @@ +package DifferenceOfWheels; + +import java.awt.*; + +public class DrawningOrnamentStar implements IOrnaments{ + int CountWeel; + @Override + public void SetCount(int n) { + CountWeel = n; + } + + @Override + public Integer get_count_weels() { + return CountWeel; + } + + @Override + public void DrawOrnament(Graphics2D g, int x, int y) { + g.setColor(Color.BLACK); + g.fillOval(x, y, 25, 25); + + g.setColor(Color.CYAN); + Polygon elements = new Polygon(); + elements.addPoint(x + 12, y + 2); + elements.addPoint(x + 15, y + 8); + elements.addPoint(x + 21, y + 10); + elements.addPoint(x + 15, y + 13); + elements.addPoint(x + 19, y + 19); + elements.addPoint(x + 12, y + 15); + elements.addPoint(x + 5, y + 19); + elements.addPoint(x + 9, y + 13); + elements.addPoint(x + 3, y + 10); + elements.addPoint(x + 10, y + 8); + g.fillPolygon(elements); + } + + @Override + public void DrawWeels(Graphics2D g, int x, int y) { + switch (CountWeel){ + case 2: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 3: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 38, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 4: + DrawOrnament(g, x + 5, y + 67); + DrawOrnament(g, x + 32, y + 67); + DrawOrnament(g, x + 61, y + 67); + DrawOrnament(g, x + 88, y + 67); + break; + } + } +} diff --git a/DifferenceOfWheels/DrawningOrnamentTriangle.java b/DifferenceOfWheels/DrawningOrnamentTriangle.java new file mode 100644 index 0000000..68099dd --- /dev/null +++ b/DifferenceOfWheels/DrawningOrnamentTriangle.java @@ -0,0 +1,51 @@ +package DifferenceOfWheels; + +import java.awt.*; + +public class DrawningOrnamentTriangle implements IOrnaments{ + int CountWeel; + @Override + public void SetCount(int n) { + CountWeel = n; + } + + @Override + public Integer get_count_weels() { + return CountWeel; + } + + @Override + public void DrawOrnament(Graphics2D g, int x, int y) { + g.setColor(Color.BLACK); + g.fillOval(x, y, 25, 25); + + g.setColor(Color.MAGENTA); + Polygon elements = new Polygon(); + elements.addPoint(x + 7, y + 6); + elements.addPoint(x + 20, y + 6); + elements.addPoint(x + 7, y + 18); + g.fillPolygon(elements); + } + + @Override + public void DrawWeels(Graphics2D g, int x, int y) { + switch (CountWeel){ + case 2: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 3: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 38, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 4: + DrawOrnament(g, x + 5, y + 67); + DrawOrnament(g, x + 32, y + 67); + DrawOrnament(g, x + 61, y + 67); + DrawOrnament(g, x + 88, y + 67); + break; + } + } + +} diff --git a/DifferenceOfWheels/DrawningWheels.java b/DifferenceOfWheels/DrawningWheels.java new file mode 100644 index 0000000..65f3619 --- /dev/null +++ b/DifferenceOfWheels/DrawningWheels.java @@ -0,0 +1,53 @@ +package DifferenceOfWheels; + +import java.awt.*; +import java.util.Random; + +public class DrawningWheels implements IOrnaments { + int CountWeel; + Random rnd = new Random(); + int ranOrnament = rnd.nextInt(0, 3); + + @Override + public void SetCount(int n) { + for (EnumWheels count: EnumWheels.values()) { + if (count.get_number() == n) { + CountWeel = n; + return; + } + } + } + + @Override + public Integer get_count_weels () { + return CountWeel; + } + + @Override + public void DrawOrnament (Graphics2D g,int x, int y) { + g.setColor(Color.BLACK); + g.fillOval(x, y, 25, 25); + + } + + @Override + public void DrawWeels(Graphics2D g, int x, int y) { + switch (CountWeel){ + case 2: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 3: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 38, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 4: + DrawOrnament(g, x + 5, y + 67); + DrawOrnament(g, x + 32, y + 67); + DrawOrnament(g, x + 61, y + 67); + DrawOrnament(g, x + 88, y + 67); + break; + } + } +} diff --git a/DopEnum.java b/DifferenceOfWheels/EnumWheels.java similarity index 65% rename from DopEnum.java rename to DifferenceOfWheels/EnumWheels.java index 22f1a64..eeb6295 100644 --- a/DopEnum.java +++ b/DifferenceOfWheels/EnumWheels.java @@ -1,11 +1,13 @@ -public enum DopEnum { +package DifferenceOfWheels; + +public enum EnumWheels { Two (2), Three (3), Four (4); private int number; - DopEnum(int n){ + EnumWheels(int n){ this.number = n; } diff --git a/DifferenceOfWheels/IOrnaments.java b/DifferenceOfWheels/IOrnaments.java new file mode 100644 index 0000000..63f168e --- /dev/null +++ b/DifferenceOfWheels/IOrnaments.java @@ -0,0 +1,10 @@ +package DifferenceOfWheels; +import java.awt.*; + +public interface IOrnaments { + void SetCount(int n); + Integer get_count_weels(); + void DrawOrnament(Graphics2D g, int x, int y); + + void DrawWeels(Graphics2D g, int x, int y); +} diff --git a/DrawningDumpTruck.java b/DrawningDumpTruck.java deleted file mode 100644 index 5937cc0..0000000 --- a/DrawningDumpTruck.java +++ /dev/null @@ -1,191 +0,0 @@ -import java.awt.*; -import java.util.Random; -//import javax.swing.*; - -public class DrawningDumpTruck { - public EntityDumpTruck EntityDumpTruck; - - private EntityDumpTruck get_EntityDumpTruck() { return EntityDumpTruck; } - public DrawningWeels _drawingWeels = null; - - // Ширина окна - private Integer _pictureWidth; - - // Высота окна - private Integer _pictureHeight; - - // Левая координата прорисовки автомобиля - private Integer _startPosX; - - // Верхняя кооридната прорисовки автомобиля - private Integer _startPosY; - - // Ширина прорисовки самосвала - private final int _drawningDumpTruckWidth = 116; - - // Высота прорисовки самосвала - private final int _drawingDumpTruckHeight = 100; - Random random = new Random(); - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, Boolean body, Boolean tent) - { - EntityDumpTruck = new EntityDumpTruck(); - EntityDumpTruck.Init(speed, weight, bodyColor, additionalColor, body, tent); - _pictureWidth = null; - _pictureHeight = null; - _startPosX = null; - _startPosY = null; - _drawingWeels = new DrawningWeels(random.nextInt(2, 5)); - - } - - public Boolean SetPictureSize(int width, int height) - { - if (_drawningDumpTruckWidth > width || _drawingDumpTruckHeight > height) - { - return false; - } - - if (_pictureWidth != null && width != _pictureWidth || _pictureHeight != null && height != _pictureHeight) - { - if (_startPosX + _drawningDumpTruckWidth > width) - { - _startPosX -= _drawningDumpTruckWidth; - } - - if (_startPosY + _drawingDumpTruckHeight > height) - { - _startPosY -= _drawingDumpTruckHeight; - } - } - - _pictureWidth = width; - _pictureHeight = height; - return true; - } - - public void SetPosition(int x, int y) - { - if (_pictureHeight == null|| _pictureWidth == null) - { - return; - } - - if (x < 0) - { - x = 0; - } - if (x + _drawningDumpTruckWidth > _pictureWidth) - { - x = _pictureWidth - _drawningDumpTruckWidth; - } - if (y < 0) - { - y = 0; - } - if (y + _drawingDumpTruckHeight > _pictureHeight) - { - y = _pictureHeight - _drawingDumpTruckHeight; - - } - - _startPosX = x; - _startPosY = y; - - } - - public Boolean MoveTransport(DirectionType direction) - { - if (EntityDumpTruck == null || _startPosX == null || _startPosY == null) - { - return false; - } - - switch (direction) - { - //влево - case DirectionType.Left: - if (_startPosX - _drawningDumpTruckWidth > 0) - { - _startPosX -= (int)EntityDumpTruck.Step; - } - return true; - //вверх - case DirectionType.Up: - if (_startPosY - _drawingDumpTruckHeight > 0) - { - _startPosY -= (int)EntityDumpTruck.Step; - } - return true; - // вправо - case DirectionType.Right: - if (_startPosX + _drawningDumpTruckWidth + (int)EntityDumpTruck.Step < _pictureWidth) - { - _startPosX += (int)EntityDumpTruck.Step; - } - return true; - //вниз - case DirectionType.Down: - if (_startPosY + _drawingDumpTruckHeight + (int)EntityDumpTruck.Step < _pictureHeight) - { - _startPosY += (int)EntityDumpTruck.Step; - } - return true; - default: - return false; - } - } - - public void DrawTransport(Graphics2D g) - { - if (EntityDumpTruck == null || _startPosX == null || _startPosY == null) - { - return; - } - - g.setPaint(Color.BLACK); - - if (EntityDumpTruck.Body) - { - int[] x_b = {_startPosX, _startPosX + 77, _startPosX + 77, _startPosX + 20}; - int[] y_b = {_startPosY + 15, _startPosY + 15, _startPosY + 50, _startPosY + 50}; - - g.setPaint(Color.BLACK); - g.drawPolygon(x_b, y_b, 4); - g.setPaint(EntityDumpTruck.AdditionalColor); - g.fillPolygon(x_b, y_b, 4); - } - - if (EntityDumpTruck.Tent && EntityDumpTruck.Body) - { - int[] x_t = {_startPosX, _startPosX + 39, _startPosX + 77}; - int[] y_t = {_startPosY + 13, _startPosY, _startPosY + 13}; - - g.setPaint(Color.BLACK); - g.drawPolygon(x_t, y_t, 3); - g.setPaint(EntityDumpTruck.AdditionalColor); - g.fillPolygon(x_t, y_t, 3); - } - - // Колёса - g.setPaint(Color.BLACK); - if (_drawingWeels != null){ - _drawingWeels.DrawningWeel(g, _startPosX, _startPosY); - } - - //Границы самосвала - g.drawRect( _startPosX + 10, _startPosY + 53, 100, 13); - g.drawRect( _startPosX + 80, _startPosY + 15, 30, 36); - g.drawRect(_startPosX + 85, _startPosY + 20, 20, 20); - - // Кузов - g.setPaint(EntityDumpTruck.BodyColor); - g.fillRect(_startPosX + 10, _startPosY + 53, 100, 13); - g.fillRect(_startPosX + 80, _startPosY + 15, 30, 36); - - //Окно - g.setPaint(Color.CYAN); - g.fillRect(_startPosX + 85, _startPosY + 20, 20, 20); - - } -} diff --git a/DrawningWeels.java b/DrawningWeels.java index ddd4baf..d4ab789 100644 --- a/DrawningWeels.java +++ b/DrawningWeels.java @@ -1,7 +1,6 @@ import java.awt.*; public class DrawningWeels { - private int count; DrawningWeels (int n){ count = n; @@ -12,7 +11,6 @@ public class DrawningWeels { public void DrawningWeel (Graphics g, int x, int y){ g.setColor(Color.BLACK); - //g.fillOval(x, y, 25, 25); switch (get_countWheels()){ case 2: diff --git a/Drawnings/CanvasDumpTruck.java b/Drawnings/CanvasDumpTruck.java new file mode 100644 index 0000000..635a2ba --- /dev/null +++ b/Drawnings/CanvasDumpTruck.java @@ -0,0 +1,18 @@ +package Drawnings; +import javax.swing.*; +import java.awt.*; + +public class CanvasDumpTruck extends JComponent { + public DrawningTruck _drawningTruck; + public CanvasDumpTruck() {} + + public void paintComponent(Graphics g) { + if (_drawningTruck == null) { + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D) g; + _drawningTruck.DrawTransport(g2d); + super.repaint(); + } +} diff --git a/Drawnings/CanvasFormDumpTruckCollection.java b/Drawnings/CanvasFormDumpTruckCollection.java new file mode 100644 index 0000000..04dfac1 --- /dev/null +++ b/Drawnings/CanvasFormDumpTruckCollection.java @@ -0,0 +1,33 @@ +package Drawnings; + +import CollectionGenericObjects.AbstractCompany; +import CollectionGenericObjects.ICollectionGenericObjects; +import CollectionGenericObjects.MassiveGenericObjects; + +import javax.swing.*; +import java.awt.*; + +public class CanvasFormDumpTruckCollection extends JComponent { + public ICollectionGenericObjects collection = new MassiveGenericObjects(); + public AbstractCompany company = null; + public void SetCollectionToCanvas(AbstractCompany company) { + this.company = company; + } + public CanvasFormDumpTruckCollection(){}; + 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 DrawningTruck) { + ((DrawningTruck) obj).DrawTransport(g2d); + } + } + super.repaint(); + } +} diff --git a/DirectionType.java b/Drawnings/DirectionType.java similarity index 69% rename from DirectionType.java rename to Drawnings/DirectionType.java index 95f37e8..9c1c629 100644 --- a/DirectionType.java +++ b/Drawnings/DirectionType.java @@ -1,6 +1,9 @@ +package Drawnings; + public enum DirectionType { Up, Down, Left, Right, + Unknow } diff --git a/Drawnings/DrawningDumpTruck.java b/Drawnings/DrawningDumpTruck.java new file mode 100644 index 0000000..2df818d --- /dev/null +++ b/Drawnings/DrawningDumpTruck.java @@ -0,0 +1,63 @@ +package Drawnings; +import DifferenceOfWheels.IOrnaments; +import Entities.EntityDumpTruck; + +import java.awt.*; + +public class DrawningDumpTruck extends DrawningTruck{ + + public DrawningDumpTruck(int speed, double weight, Color bodyColor, + Color additionalColor, Boolean body, Boolean tent) + { + super(speed, weight, bodyColor); + EntityTruck = new EntityDumpTruck(speed, weight, bodyColor, additionalColor, body, tent); + SetOrnamentWheel(); + _ornament.SetCount(random.nextInt(2, 5)); + + } + + public DrawningDumpTruck (EntityDumpTruck _dumpTruck, IOrnaments ornament){ + super(_dumpTruck, ornament); + } + + public DrawningDumpTruck(){ + super(110, 100); + } + + public void DrawTransport(Graphics2D g) + { + if (EntityTruck == null || !(EntityTruck instanceof EntityDumpTruck entityTruck) || _startPosX == null || _startPosY == null) + { + return; + } + + g.setPaint(Color.BLACK); + + _startPosX += 5; + super.DrawTransport(g); + _startPosX -= 5; + + if (entityTruck.Body) + { + int[] x_b = {_startPosX, _startPosX + 77, _startPosX + 77, _startPosX + 20}; + int[] y_b = {_startPosY + 15, _startPosY + 15, _startPosY + 50, _startPosY + 50}; + + g.setPaint(Color.BLACK); + g.drawPolygon(x_b, y_b, 4); + g.setPaint(entityTruck.AdditionalColor); + g.fillPolygon(x_b, y_b, 4); + } + + if (entityTruck.Tent && entityTruck.Body) + { + int[] x_t = {_startPosX, _startPosX + 39, _startPosX + 77}; + int[] y_t = {_startPosY + 13, _startPosY, _startPosY + 13}; + + g.setPaint(Color.BLACK); + g.drawPolygon(x_t, y_t, 3); + g.setPaint(entityTruck.AdditionalColor); + g.fillPolygon(x_t, y_t, 3); + } + + } +} diff --git a/Drawnings/DrawningTruck.java b/Drawnings/DrawningTruck.java new file mode 100644 index 0000000..86fcbaa --- /dev/null +++ b/Drawnings/DrawningTruck.java @@ -0,0 +1,217 @@ +package Drawnings; + +import java.awt.*; +import java.util.Random; +import DifferenceOfWheels.*; +import Entities.EntityTruck; + + +public class DrawningTruck { + public EntityTruck EntityTruck; + protected EntityTruck get_EntityTruck() { return EntityTruck; } + public IOrnaments _ornament = new DrawningWheels(); + int CountOfWheels = 0; + int ornament_type; + + // Ширина окна + private Integer _pictureWidth; + + // Высота окна + private Integer _pictureHeight; + + // Левая координата прорисовки автомобиля + protected Integer _startPosX; + + // Верхняя кооридната прорисовки автомобиля + protected Integer _startPosY; + + // Ширина прорисовки самосвала + private int _drawningTruckWidth = 110; //100 + + // Высота прорисовки самосвала + private int _drawningTruckHeight = 110; //92 + + public Integer GetPosX() { return _startPosX;} + public Integer GetPosY() { return _startPosY;} + public Integer GetWidth() { return _drawningTruckWidth;} + public Integer GetHeight() { return _drawningTruckHeight;} + Random random = new Random(); + + private DrawningTruck() + { + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + + } + + public DrawningTruck(int speed, double weight, Color bodyColor) + { + EntityTruck = new EntityTruck(speed, weight, bodyColor); + CountOfWheels = random.nextInt(2, 5); + ornament_type = random.nextInt(0, 3); + } + + protected DrawningTruck(int drawningTruckWidth, int drawningTruckHeight) + { + _drawningTruckWidth = drawningTruckWidth; + _drawningTruckHeight = drawningTruckHeight; + } + + public DrawningTruck(EntityTruck _truck, IOrnaments ornament){ + EntityTruck = _truck; + _ornament = ornament; + if (_ornament instanceof DrawningOrnamentStar) ornament_type = 0; + else if (_ornament instanceof DrawningOrnamentTriangle) ornament_type = 1; + else if (_ornament instanceof DrawningOrnamentRectangle) ornament_type = 2; + CountOfWheels = _ornament.get_count_weels(); + } + + protected void SetOrnamentWheel(){ + switch (ornament_type){ + case 0: + _ornament = new DrawningOrnamentStar(); + _ornament.SetCount(CountOfWheels); + break; + case 1: + _ornament = new DrawningOrnamentTriangle(); + _ornament.SetCount(CountOfWheels); + break; + case 2: + _ornament = new DrawningOrnamentRectangle(); + _ornament.SetCount(CountOfWheels); + break; + } + } + + public Boolean SetPictureSize(int width, int height) + { + if (_drawningTruckWidth > width || _drawningTruckHeight > height) + { + return false; + } + + if (_pictureWidth != null && width != _pictureWidth || _pictureHeight != null && height != _pictureHeight) + { + if (_startPosX + _drawningTruckWidth > width) + { + _startPosX -= _drawningTruckWidth; + } + + if (_startPosY + _drawningTruckHeight > height) + { + _startPosY -= _drawningTruckHeight; + } + } + + _pictureWidth = width; + _pictureHeight = height; + return true; + } + + public void SetPosition(int x, int y) + { + if (_pictureHeight == null|| _pictureWidth == null) + { + return; + } + + if (x < 0) + { + x = 0; + } + if (x + _drawningTruckWidth > _pictureWidth) + { + x = (int)_pictureWidth - _drawningTruckWidth; + } + if (y < 0) + { + y = 0; + } + if (y + _drawningTruckHeight > _pictureHeight) + { + y = (int)_pictureHeight - _drawningTruckHeight; + + } + + _startPosX = x; + _startPosY = y; + + } + + public Boolean MoveTransport(DirectionType direction) + { + if (EntityTruck == null || _startPosX == null || _startPosY == null) + { + return false; + } + + switch (direction) + { + //влево + case DirectionType.Left: + if (_startPosX - _drawningTruckWidth > 0) + { + _startPosX -= (int)EntityTruck.Step; + } + return true; + //вверх + case DirectionType.Up: + if (_startPosY - _drawningTruckHeight > 0) + { + _startPosY -= (int)EntityTruck.Step; + } + return true; + // вправо + case DirectionType.Right: + if (_startPosX + _drawningTruckWidth + (int)EntityTruck.Step < _pictureWidth) + { + _startPosX += (int)EntityTruck.Step; + } + return true; + //вниз + case DirectionType.Down: + if (_startPosY + _drawningTruckHeight + (int)EntityTruck.Step < _pictureHeight) + { + _startPosY += (int)EntityTruck.Step; + } + return true; + default: + return false; + } + } + + public void DrawTransport(Graphics2D g) + { + if (EntityTruck == null || _startPosX == null || _startPosY == null) + { + return; + } + + g.setPaint(Color.BLACK); + + // Колёса + g.setPaint(Color.BLACK); + SetOrnamentWheel(); + if (_ornament != null){ + _ornament.DrawWeels(g, _startPosX - 5, _startPosY); + } + + //Границы самосвала + g.drawRect( _startPosX + 5, _startPosY + 53, 100, 13); + g.drawRect( _startPosX + 75, _startPosY + 15, 30, 36); + g.drawRect(_startPosX + 80, _startPosY + 20, 20, 20); + + // Кузов + g.setPaint(EntityTruck.BodyColor); + g.fillRect(_startPosX + 5, _startPosY + 53, 100, 13); + g.fillRect(_startPosX + 75, _startPosY + 15, 30, 36); + + //Окно + g.setPaint(Color.CYAN); + g.fillRect(_startPosX + 80, _startPosY + 20, 20, 20); + + } + +} diff --git a/Entities/EntityDumpTruck.java b/Entities/EntityDumpTruck.java new file mode 100644 index 0000000..80afee1 --- /dev/null +++ b/Entities/EntityDumpTruck.java @@ -0,0 +1,26 @@ +package Entities; + +import java.awt.*; + +public class EntityDumpTruck extends EntityTruck { + + public Color AdditionalColor; + private Color get_AdditionalColor(){ return AdditionalColor; } + + public Boolean Body; + private Boolean get_Body() { return Body; } + + public Boolean Tent; + private Boolean get_Tent() { return Tent; } + + public double Step; + + public EntityDumpTruck(int speed, double weight, Color bodyColor, Color additionalColor, Boolean body, Boolean tent) + { + super (speed, weight, bodyColor); + AdditionalColor = additionalColor; + Body = body; + Tent = tent; + } + +} diff --git a/Entities/EntityTruck.java b/Entities/EntityTruck.java new file mode 100644 index 0000000..1c43562 --- /dev/null +++ b/Entities/EntityTruck.java @@ -0,0 +1,24 @@ +package Entities; + +import java.awt.*; + +public class EntityTruck { + public int Speed; + private int get_Speed(){ return Speed; } + + public double Weight; + private double get_Weight(){return Weight; } + + public Color BodyColor; + public Color get_BodyColor(){ return BodyColor; } + + public double Step; + + public EntityTruck(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + Step = (speed * 100) / weight; + } +} diff --git a/EntityDumpTruck.java b/EntityDumpTruck.java deleted file mode 100644 index 0edb60f..0000000 --- a/EntityDumpTruck.java +++ /dev/null @@ -1,35 +0,0 @@ -import java.awt.*; - -public class EntityDumpTruck { - public int Speed; - private int get_Speed(){ return Speed; } - - public double Weight; - private double get_Weight(){return Weight; } - - public Color BodyColor; - private Color get_BodyColor(){ return BodyColor; } - - public Color AdditionalColor; - private Color get_AdditionalColor(){ return AdditionalColor; } - - public Boolean Body; - private Boolean get_Body() { return Body; } - - public Boolean Tent; - private Boolean get_Tent() { return Tent; } - - public double Step; - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, Boolean body, Boolean tent) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Body = body; - Tent = tent; - Step = speed * 100 / weight; - } - -} diff --git a/FormAdditionalCollection.java b/FormAdditionalCollection.java new file mode 100644 index 0000000..de71ba5 --- /dev/null +++ b/FormAdditionalCollection.java @@ -0,0 +1,147 @@ +import CollectionGenericObjects.AbstractCompany; +import CollectionGenericObjects.AdditionalCollection; +import DifferenceOfWheels.DrawningOrnamentRectangle; +import DifferenceOfWheels.DrawningOrnamentStar; +import DifferenceOfWheels.DrawningOrnamentTriangle; +import DifferenceOfWheels.IOrnaments; +import Drawnings.CanvasDumpTruck; +import Drawnings.DrawningDumpTruck; +import Drawnings.DrawningTruck; +import Entities.EntityDumpTruck; +import Entities.EntityTruck; + +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{ + DrawningTruck _drawningTruck; + private AbstractCompany _company = null; + private CanvasDumpTruck _canvasTruck = new CanvasDumpTruck(); + private AdditionalCollection _additionalCollection = null; + private Random random = new Random(); + private JButton buttonGenerate = new JButton("Создать"); + private JButton buttonAdd = new JButton("Добавить"); + private JList listEntity = new JList(); + private JList listOrnament = new JList(); + public FormAdditionalCollection() { + setTitle("Случайный грузовик"); + setMinimumSize(new Dimension(650,310)); + _additionalCollection = new AdditionalCollection(3, + (Class) EntityTruck.class, (Class) IOrnaments.class); + AddEntities(); + AddOrnaments(); + buttonGenerate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + _drawningTruck = _additionalCollection.CreateDrawningTruckClass(); + _drawningTruck.SetPictureSize(getWidth(), getHeight()); + _drawningTruck.SetPosition(50,50); + _canvasTruck.repaint(); + _canvasTruck._drawningTruck = _drawningTruck; + DrawningTruck _copyTruck; + if (_drawningTruck instanceof DrawningDumpTruck) + _copyTruck = new DrawningDumpTruck((EntityDumpTruck) _drawningTruck.EntityTruck, _drawningTruck._ornament); + else + _copyTruck = new DrawningTruck(_drawningTruck.EntityTruck, _drawningTruck._ornament); + _company._collection.Insert(_copyTruck); + + String[] data1 = new String[_additionalCollection.Len_entity]; + for (int i = 0; i < _additionalCollection.Len_entity; i++) { + EntityTruck _entity = _additionalCollection._collectionEntityTruck[i]; + data1[i] = ToString(_entity); + } + String[] data2 = new String[_additionalCollection.Len_ornament]; + for (int i = 0; i < _additionalCollection.Len_ornament; i++) { + IOrnaments ornament = _additionalCollection._collectionIOrnaments[i]; + data2[i] = ToString(ornament); + } + listEntity.setListData(data1); + listOrnament.setListData(data2); + } + }); + + buttonAdd.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + FormDumpTruckCollection.canvasShow(); + } + }); + + buttonGenerate.setBounds(450, 10, 100, 50); + buttonAdd.setBounds(450, 70, 100, 50); + add(buttonGenerate); + add(buttonAdd); + listEntity.setBounds(10,200,300,60); + listOrnament.setBounds(320,200,300,60); + add(listEntity); + add(listOrnament); + add(_canvasTruck); + setVisible(true); + } + private String ToString(EntityTruck entity) { + String str = ""; + if (entity instanceof EntityDumpTruck) str += "Самосвал "; + else str += "Грузовик "; + str += entity.get_BodyColor().toString(); + return str; + } + private String ToString(IOrnaments ornament) { + if (ornament == null || ornament.get_count_weels() == null) + return "Нет орнамента"; + String str = "Орнамент "; + if (ornament instanceof DrawningOrnamentRectangle) str += "Квадрат "; + else if (ornament instanceof DrawningOrnamentStar) str += "Звезда "; + else if (ornament instanceof DrawningOrnamentTriangle) str += "Треугольник "; + str += ornament.get_count_weels().toString(); + return str; + } + public void AddEntities() { + for (int i = 0; i < _additionalCollection.Len_entity; i++) { + random = new Random(); + int speed = random.nextInt(100, 300); + double weight = random.nextInt(1000, 3000); + Color bodycolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)); + EntityTruck entity; + if (random.nextBoolean()) { + entity = new EntityTruck(speed, weight, bodycolor); + } + else { + Color additionalcolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)); + boolean body = random.nextBoolean(); + boolean tent = random.nextBoolean(); + entity = new EntityDumpTruck(speed, weight, bodycolor, + additionalcolor, body, tent); + } + _additionalCollection.Insert(entity); + } + } + public void AddOrnaments() { + for (int i = 0; i < _additionalCollection.Len_ornament; i++) { + random = new Random(); + Integer count_of_wheels = random.nextInt(2, 5); + IOrnaments _ornament = null; + switch (random.nextInt(0,3)) { + case 0: + _ornament = new DrawningOrnamentStar(); + break; + case 1: + _ornament = new DrawningOrnamentTriangle(); + break; + case 2: + _ornament = new DrawningOrnamentRectangle(); + break; + default: + count_of_wheels = null; + break; + } + if (_ornament != null) _ornament.SetCount(count_of_wheels); + _additionalCollection.Insert(_ornament); + } + } + void setCompany(AbstractCompany company) { + this._company = company; + } +} diff --git a/FormDumpTruck.java b/FormDumpTruck.java index 3028df0..e81f00f 100644 --- a/FormDumpTruck.java +++ b/FormDumpTruck.java @@ -1,3 +1,9 @@ +import Drawnings.CanvasDumpTruck; +import Drawnings.DirectionType; +import Drawnings.DrawningDumpTruck; +import Drawnings.DrawningTruck; +import MovementStrategy.*; + import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -10,27 +16,63 @@ public class FormDumpTruck extends JFrame{ private CanvasDumpTruck _canvasDumpTruck = new CanvasDumpTruck(); + private AbstractStrategy _strategy; + // Размер формы private Dimension dimension; // Название формы private String title; - private JButton CreateButton = new JButton("Создать самосвал"); + private JButton CreateDumpTruckButton = new JButton("Создать грузовик"); + private JButton CreateTruckButton = new JButton("Создать самосвал"); private JButton UpButton = new JButton(); private JButton DownButton = new JButton();; private JButton LeftButton = new JButton();; private JButton RightButton = new JButton(); + private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"}); + private JButton ButtonStrategy = new JButton("Шаг"); public FormDumpTruck(String title, Dimension dimension) { this.title = title; this.dimension = dimension; } - public void Form() { - CreateButton.setName("CREATE"); + private void CreateObject(String type){ + Random random = new Random(); + Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + switch(type){ + case "DrawningTruck": + _canvasDumpTruck._drawningTruck = new Drawnings.DrawningTruck(random.nextInt(100, 300), random.nextInt(1000, 3000), + bodyColor); + _canvasDumpTruck._drawningTruck.SetPictureSize(getWidth(), getHeight()); + _canvasDumpTruck._drawningTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + _canvasDumpTruck.repaint(); + break; + case "DrawningDumpTruck": + Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + _canvasDumpTruck._drawningTruck = new DrawningDumpTruck(); + _canvasDumpTruck._drawningTruck = new DrawningDumpTruck(random.nextInt(100, 300), random.nextInt(1000, 3000), + bodyColor, additionalColor, + random.nextBoolean(), random.nextBoolean()); + _canvasDumpTruck._drawningTruck.SetPictureSize(getWidth(), getHeight()); + _canvasDumpTruck._drawningTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + _canvasDumpTruck.repaint(); + break; + default: + return; + } + _strategy = null; + ComboBoxStrategy.setEnabled(true); + } + + public void Form(DrawningTruck _truck) { + CreateDumpTruckButton.setName("Создать самосвал"); + CreateTruckButton.setName("Создать грузовик"); setMinimumSize(dimension); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + _canvasDumpTruck._drawningTruck = _truck; + UpButton.setName("UP"); Icon iconUp = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowUp.png"); UpButton.setIcon(iconUp); @@ -47,40 +89,76 @@ public class FormDumpTruck extends JFrame{ Icon iconRight = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowRight.png"); RightButton.setIcon(iconRight); - CreateButton.addActionListener(new ActionListener() { + CreateTruckButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - Random random = new Random(); - _canvasDumpTruck._drawningDumpTruck = new DrawningDumpTruck(); - Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); - Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); - _canvasDumpTruck._drawningDumpTruck.Init(random.nextInt(1000, 3000), random.nextInt(1000, 3000), - bodyColor, additionalColor, - random.nextBoolean(), random.nextBoolean()); + CreateObject("DrawningTruck"); + } + }); - _canvasDumpTruck._drawningDumpTruck.SetPictureSize(getWidth(), getHeight()); - _canvasDumpTruck._drawningDumpTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); - _canvasDumpTruck.repaint(); + CreateDumpTruckButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawningDumpTruck"); + } + }); + + ButtonStrategy.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_canvasDumpTruck._drawningTruck == null) return; + if (ComboBoxStrategy.isEnabled()) + { + int index = ComboBoxStrategy.getSelectedIndex(); + switch(index) + { + case 0: + _strategy = new MoveToCenter(); + break; + case 1: + _strategy = new MoveToBorder(); + break; + default: + _strategy = null; + break; + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableTruck(_canvasDumpTruck._drawningTruck), getWidth() - 23, getHeight()-23); + } + if (_strategy == null) + { + return; + } + ComboBoxStrategy.setEnabled(false); + _strategy.MakeStep(); + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + ComboBoxStrategy.setEnabled(true); + _strategy = null; + } } }); ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { - if (_canvasDumpTruck._drawningDumpTruck == null) return; + if (_canvasDumpTruck._drawningTruck == null) return; boolean result = false; switch ((((JButton) (event.getSource())).getName())) { case "UP": - result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Up); + result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Up); break; case "DOWN": - result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Down); + result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Down); break; case "LEFT": - result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Left); + result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Left); break; case "RIGHT": - result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Right); + result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Right); break; } if (result) { @@ -97,17 +175,27 @@ public class FormDumpTruck extends JFrame{ setSize(dimension.width, dimension.height); setLayout(null); _canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 160, 40); + // кнопки создаиния самосвала и грузовика + CreateDumpTruckButton.setBounds(10, getHeight() - 90, 160, 40); + CreateTruckButton.setBounds(160, getHeight() - 90, 160, 40); + // кнопки направлений UpButton.setBounds(getWidth() - 150, getHeight() - 170, 60, 60); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 60, 60); RightButton.setBounds(getWidth() - 85, getHeight() - 100, 60, 60); LeftButton.setBounds(getWidth() - 215, getHeight() - 100, 60, 60); - add(CreateButton); + // кнокпи для стратегий + ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35); + ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25); + // добавление кнопок + add(CreateDumpTruckButton); + add(CreateTruckButton); add(UpButton); add(DownButton); add(RightButton); add(LeftButton); add(_canvasDumpTruck); + add(ComboBoxStrategy); + add(ButtonStrategy); pack(); setVisible(true); @@ -115,14 +203,17 @@ public class FormDumpTruck extends JFrame{ public void componentResized(ComponentEvent e) { int Width = getWidth(); int Height = getHeight(); - if (_canvasDumpTruck._drawningDumpTruck != null) - _canvasDumpTruck._drawningDumpTruck.SetPictureSize(Width, Height); + if (_canvasDumpTruck._drawningTruck != null) + _canvasDumpTruck._drawningTruck.SetPictureSize(Width, Height); _canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 160, 40); + CreateDumpTruckButton.setBounds(10, getHeight() - 90, 160, 40); + CreateTruckButton.setBounds(180, getHeight() - 90, 160, 40); UpButton.setBounds(getWidth() - 150, getHeight() - 170, 60, 60); DownButton.setBounds(getWidth() - 150, getHeight() - 100, 60, 60); RightButton.setBounds(getWidth() - 85, getHeight() - 100, 60, 60); LeftButton.setBounds(getWidth() - 215, getHeight() - 100, 60, 60); + ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35); + ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25); } }); } diff --git a/FormDumpTruckCollection.java b/FormDumpTruckCollection.java new file mode 100644 index 0000000..ef363bc --- /dev/null +++ b/FormDumpTruckCollection.java @@ -0,0 +1,225 @@ +import CollectionGenericObjects.AbstractCompany; +import CollectionGenericObjects.MassiveGenericObjects; +import CollectionGenericObjects.TruckShearingService; +import Drawnings.CanvasFormDumpTruckCollection; +import Drawnings.DrawningDumpTruck; +import Drawnings.DrawningTruck; + +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 FormDumpTruckCollection extends JFrame { + private String title; + private Dimension dimension; + public static CanvasFormDumpTruckCollection _canvasDumpTruck = new CanvasFormDumpTruckCollection(); + private static AbstractCompany _company = null; + private JButton CreateDumpTruckButton = new JButton("Создать самосвал");; + private JButton CreateTruckButton = 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 MaskedTextField; + private Random random = new Random(); + + public FormDumpTruckCollection(String title, Dimension dimension) { + this.title = title; + this.dimension = dimension; + } + + public static void canvasShow() { + _company.SetPosition(); + _canvasDumpTruck.SetCollectionToCanvas(_company); + _canvasDumpTruck.repaint(); + } + + private void CreateObject(String type){ + DrawningTruck _drawningTruck; + Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + switch(type){ + case "DrawningTruck": + _drawningTruck = new DrawningTruck(random.nextInt(100, 300), random.nextInt(1000, 3000), + bodyColor); + break; + case "DrawningDumpTruck": + Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + _drawningTruck = new DrawningDumpTruck(random.nextInt(100, 300), random.nextInt(1000, 3000), + bodyColor, additionalColor, + random.nextBoolean(), random.nextBoolean()); + break; + default: + return; + } + if (_company._collection.Insert(_drawningTruck) >= 0){ + JOptionPane.showMessageDialog(null, "Объект добавлен"); + canvasShow(); + } + else{ + JOptionPane.showMessageDialog(null, "Объект не удалось добавить"); + } + } + + public Color getColor() { + Color initializator = new Color(random.nextInt(0, 256),random.nextInt(0, 256),random.nextInt(0, 256)); + Color _color = JColorChooser.showDialog(this, "Выберите цвет", initializator); + return _color; + } + + public void Form(){ + 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 TruckShearingService(getWidth()-200, getHeight()-70, new MassiveGenericObjects()); + break; + } + } + }); + + CreateTruckButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawningTruck"); + } + }); + CreateDumpTruckButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawningDumpTruck"); + } + }); + + 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) { + JOptionPane.showMessageDialog(null, "Объект удален"); + canvasShow(); + } + else { + JOptionPane.showMessageDialog(null, "Объект не удалось удалить"); + } + } + }); + + GoToCheckButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_company == null) + { + return; + } + DrawningTruck _truck = null; + int counter = 100; + while (_truck == null) + { + _truck = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + if (_truck == null) + { + return; + } + FormDumpTruck form = new FormDumpTruck("Самосвал", new Dimension(900,565)); + form.Form(_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(); + } + }); + + _canvasDumpTruck.setBounds(0, 0, getWidth()-200, getHeight()); + ComboBoxCollections.setBounds(getWidth()-170, 10, 150, 20); + CreateTruckButton.setBounds(getWidth()-170, 60, 160, 30); + CreateDumpTruckButton.setBounds(getWidth()-170, 100, 160, 30); + MaskedTextField.setBounds(getWidth()-170,200,150,30); + RemoveButton.setBounds(getWidth()-170, 240, 150, 30); + GoToCheckButton.setBounds(getWidth()-170, 280, 150, 30); + RandomButton.setBounds(getWidth()-170, 320, 150, 30); + RefreshButton.setBounds(getWidth()-170, getHeight()-90, 150, 30); + + setSize(dimension.width,dimension.height); + setLayout(null); + add(_canvasDumpTruck); + add(ComboBoxCollections); + add(CreateTruckButton); + add(CreateDumpTruckButton); + add(MaskedTextField); + add(RemoveButton); + add(GoToCheckButton); + add(RandomButton); + add(RefreshButton); + setVisible(true); + + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent e) { + _canvasDumpTruck.setBounds(0, 0, getWidth()-200, getHeight()-70); + ComboBoxCollections.setBounds(getWidth()-170, 10, 150, 20); + CreateTruckButton.setBounds(getWidth()-170, 60, 160, 30); + CreateDumpTruckButton.setBounds(getWidth()-170, 100, 160, 30); + MaskedTextField.setBounds(getWidth()-170,200,150,30); + RemoveButton.setBounds(getWidth()-170, 240, 150, 30); + GoToCheckButton.setBounds(getWidth()-170, 280, 150, 30); + RandomButton.setBounds(getWidth()-170, 320, 150, 30); + RefreshButton.setBounds(getWidth()-170, getHeight()-90, 150, 30); + } + }); + } +} diff --git a/Main.java b/Main.java index 99983af..eefba9d 100644 --- a/Main.java +++ b/Main.java @@ -2,7 +2,7 @@ import java.awt.*; public class Main { public static void main(String[] args) { - FormDumpTruck formDumpTruck = new FormDumpTruck("Самосвал", new Dimension(900, 700)); + FormDumpTruckCollection formDumpTruck = new FormDumpTruckCollection("Самосвал", new Dimension(950, 700)); formDumpTruck.Form(); } } diff --git a/MovementStrategy/AbstractStrategy.java b/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..08703d0 --- /dev/null +++ b/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,60 @@ +package MovementStrategy; + +public abstract class AbstractStrategy { + private IMoveableObject _moveableObject; + private StrategyStatus _state = StrategyStatus.NotInit; + protected int FieldWidth; + protected int FieldHeight; + public StrategyStatus GetStatus() { return _state; } + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = StrategyStatus.Finish; + return; + } + MoveToTarget(); + } + protected boolean MoveLeft() { return MoveTo(MovementDirection.Left);} + protected boolean MoveRight() { return MoveTo(MovementDirection.Right);} + protected boolean MoveUp() { return MoveTo(MovementDirection.Up);} + protected boolean MoveDown() { return MoveTo(MovementDirection.Down);} + protected ObjectParameters GetObjectParameters(){ return _moveableObject.GetObjectPosition();} + protected Integer GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return null; + } + return _moveableObject.GetStep(); + } + protected abstract void MoveToTarget(); + protected abstract boolean IsTargetDestinaion(); + private boolean MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + if (_moveableObject.TryMoveObject(movementDirection)) + return true; + else + return false; + } +} diff --git a/MovementStrategy/IMoveableObject.java b/MovementStrategy/IMoveableObject.java new file mode 100644 index 0000000..a067173 --- /dev/null +++ b/MovementStrategy/IMoveableObject.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + int GetStep(); + Boolean TryMoveObject(MovementDirection direction); +} diff --git a/MovementStrategy/MoveToBorder.java b/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..b716856 --- /dev/null +++ b/MovementStrategy/MoveToBorder.java @@ -0,0 +1,36 @@ +package MovementStrategy; + +public class MoveToBorder extends AbstractStrategy{ + @Override + protected boolean IsTargetDestinaion() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.RightBorder + GetStep() >= FieldWidth - GetStep() && + objParams.DownBorder + GetStep() >= FieldHeight - GetStep(); + } + + @Override + protected void MoveToTarget() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + + int diffX = FieldWidth - objParams.RightBorder; + if (Math.abs(diffX) > GetStep()) + { + MoveRight(); + } + + int diffY = FieldHeight - objParams.DownBorder; + if (Math.abs(diffY) > GetStep()) + { + MoveDown(); + } + } + +} diff --git a/MovementStrategy/MoveToCenter.java b/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..3f2b69f --- /dev/null +++ b/MovementStrategy/MoveToCenter.java @@ -0,0 +1,38 @@ +package MovementStrategy; + +public class MoveToCenter extends AbstractStrategy{ + @Override + protected boolean IsTargetDestinaion() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) { + return false; + } + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 - GetStep() && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 - GetStep() && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 - GetStep() && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2 - GetStep() ; + } + @Override + protected void MoveToTarget() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) { + return; + } + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.abs(diffX) > GetStep()) { + if (diffX > 0) { + MoveLeft(); + } else { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.abs(diffY) > GetStep()) { + if (diffY > 0) { + MoveUp(); + } else { + MoveDown(); + } + } + } +} diff --git a/MovementStrategy/MoveableTruck.java b/MovementStrategy/MoveableTruck.java new file mode 100644 index 0000000..c4f627a --- /dev/null +++ b/MovementStrategy/MoveableTruck.java @@ -0,0 +1,48 @@ +package MovementStrategy; + +import Drawnings.CanvasDumpTruck; +import Drawnings.DirectionType; +import Drawnings.DrawningTruck; + +public class MoveableTruck implements IMoveableObject { + private CanvasDumpTruck canvas = new CanvasDumpTruck(); + public MoveableTruck(DrawningTruck drawningTruck) + { + canvas._drawningTruck = drawningTruck; + } + @Override + public ObjectParameters GetObjectPosition() { + if (canvas._drawningTruck == null || canvas._drawningTruck.EntityTruck == null || + canvas._drawningTruck.GetPosX() == null || canvas._drawningTruck.GetPosY() == null) + { + return null; + } + return new ObjectParameters(canvas._drawningTruck.GetPosX(), canvas._drawningTruck.GetPosY(), + canvas._drawningTruck.GetWidth(), canvas._drawningTruck.GetHeight()); + } + + @Override + public int GetStep() { + return (int)(canvas._drawningTruck.EntityTruck.Step); + } + + @Override + public Boolean TryMoveObject(MovementDirection direction) { + if (canvas._drawningTruck == null || canvas._drawningTruck.EntityTruck == null) + { + return false; + } + return canvas._drawningTruck.MoveTransport(GetDirectionType(direction)); + } + + private static DirectionType GetDirectionType(MovementDirection direction) + { + switch (direction) { + case MovementDirection.Left: return DirectionType.Left; + case MovementDirection.Right: return DirectionType.Right; + case MovementDirection.Up: return DirectionType.Up; + case MovementDirection.Down: return DirectionType.Down; + default: return DirectionType.Unknow; + } + } +} diff --git a/MovementStrategy/MovementDirection.java b/MovementStrategy/MovementDirection.java new file mode 100644 index 0000000..c52f124 --- /dev/null +++ b/MovementStrategy/MovementDirection.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public enum MovementDirection { + Up, + Down, + Left, + Right +} diff --git a/MovementStrategy/ObjectParameters.java b/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..3ff2428 --- /dev/null +++ b/MovementStrategy/ObjectParameters.java @@ -0,0 +1,27 @@ +package MovementStrategy; + +public class ObjectParameters { + private int _x; + private int _y; + private int _width; + private int _height; + public int LeftBorder = _x; + public int TopBorder = _y; + public int RightBorder = _x + _width; + public int DownBorder = _y + _height; + public int ObjectMiddleHorizontal = _x + _width / 2; + public int ObjectMiddleVertical = _y + _height / 2; + + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + RightBorder = _x + _width; + DownBorder = _y + _height; + ObjectMiddleHorizontal = _x + _width / 2; + ObjectMiddleVertical = _y + _height / 2; + } + +} diff --git a/MovementStrategy/StrategyStatus.java b/MovementStrategy/StrategyStatus.java new file mode 100644 index 0000000..4f087ea --- /dev/null +++ b/MovementStrategy/StrategyStatus.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public enum StrategyStatus { + NotInit, + InProgress, + Finish +}