diff --git a/OOP/src/DirectionType.java b/OOP/src/DirectionType.java deleted file mode 100644 index d3a2058..0000000 --- a/OOP/src/DirectionType.java +++ /dev/null @@ -1,6 +0,0 @@ -public enum DirectionType { - Up, - Down, - Left, - Right -} \ No newline at end of file diff --git a/OOP/src/DrawningElectricLocomotive.java b/OOP/src/DrawningElectricLocomotive.java deleted file mode 100644 index e560f86..0000000 --- a/OOP/src/DrawningElectricLocomotive.java +++ /dev/null @@ -1,193 +0,0 @@ -import java.awt.*; -import java.util.Random; - -public class DrawningElectricLocomotive { - private EntityElectricLocomotive entityElectricLocomotive; - public EntityElectricLocomotive getEntityElectricLocomotive() { - return entityElectricLocomotive; - } - private Integer _pictureWidth; - private Integer _pictureHeight; - private Integer _startPosX; - private Integer _startPosY; - private final int _drawingElectricLocomotiveWidth = 100; - private final int _drawingElectricLocomotiveHight = 100; - - public DrawningElectricLocomotiveWheels _drawningElectricLocomotiveWheels; - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean pantograph, boolean batteryStorage) { - entityElectricLocomotive = new EntityElectricLocomotive(); - entityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, pantograph, batteryStorage); - _startPosY = null; - _startPosX = null; - _pictureWidth = null; - _pictureHeight = null; - - _drawningElectricLocomotiveWheels = new DrawningElectricLocomotiveWheels(); - Random random = new Random(); - int[] countwheels = {2, 3, 4}; - int wheelCount = countwheels[random.nextInt(countwheels.length)]; - _drawningElectricLocomotiveWheels.setEnumNumber(wheelCount); - - - } - - public void setPosition(int x, int y) { - if (_pictureHeight == null || _pictureWidth == null) - return; - _startPosX = x; - _startPosY = y; - - if (_drawingElectricLocomotiveWidth + x > _pictureWidth || x < 0) { - _startPosX = 0; - } - if (_drawingElectricLocomotiveHight + y > _pictureHeight || y < 0) { - _startPosY = 0; - } - } - - public boolean setPictureSize(int width, int height) { - - if (_drawingElectricLocomotiveHight > height || _drawingElectricLocomotiveWidth > width) - return false; - _pictureHeight = height; - _pictureWidth = width; - - if (_startPosX != null && _startPosY != null) { - if (_startPosX + _drawingElectricLocomotiveWidth > width) - _startPosX = width - _drawingElectricLocomotiveWidth; - if (_startPosY + _drawingElectricLocomotiveHight > height) - _startPosY = height - _drawingElectricLocomotiveHight; - } - return true; - } - - public boolean moveTransport(DirectionType direction) { - if (entityElectricLocomotive == null || _pictureWidth == null || _pictureHeight == null) - return false; - switch (direction) { - case Left: - if (_startPosX - entityElectricLocomotive.Step() > 0) - _startPosX -= (int) entityElectricLocomotive.Step(); - return true; - case Up: - if (_startPosY - entityElectricLocomotive.Step() > 0) - _startPosY -= (int) entityElectricLocomotive.Step(); - return true; - case Right: - if (_startPosX + entityElectricLocomotive.Step() < _pictureWidth - _drawingElectricLocomotiveWidth) - _startPosX += (int) entityElectricLocomotive.Step(); - return true; - case Down: - if (_startPosY + entityElectricLocomotive.Step() < _pictureHeight - _drawingElectricLocomotiveHight) - _startPosY += (int) entityElectricLocomotive.Step(); - return true; - default: - return false; - } - } - - public void DrawTransport(Graphics g){ - if (entityElectricLocomotive == null || _startPosX == null || _startPosY == null) { - return; - } - - Graphics2D g2d = (Graphics2D) g; - _drawningElectricLocomotiveWheels.drawElectricLocomotiveWheels(g, Color.BLACK, _startPosX, _startPosY); - Point[] Body = new Point[]{ - new Point(_startPosX + 20,_startPosY + 20), - new Point(_startPosX, _startPosY + 25 + 20), - new Point(_startPosX , _startPosY + 50 + 20), - new Point(_startPosX + 100 , _startPosY + 50 + 20), - new Point(_startPosX + 100, _startPosY + 25 + 20), - new Point(_startPosX + 105, _startPosY + 20), - }; - - Polygon BodyPolygon = new Polygon(); - for(Point point:Body){ - BodyPolygon.addPoint(point.x, point.y); - } - g2d.setColor(entityElectricLocomotive.getBodyColor()); - g2d.fillPolygon(BodyPolygon); - - - Point[] line = new Point[]{ - new Point(_startPosX, _startPosY + 25 + 20), - new Point(_startPosX, _startPosY + 30 + 20), - new Point(_startPosX + 100, _startPosY + 30 + 20), - new Point(_startPosX + 100, _startPosY + 25 + 20), - }; - Polygon linePolugon = new Polygon(); - for(Point point: line){ - linePolugon.addPoint(point.x, point.y); - } - g2d.setColor(entityElectricLocomotive.getAdditionalColor()); - g2d.fillPolygon(linePolugon); - - - Point[] glass = new Point[]{ - new Point(_startPosX + 20, _startPosY + 2 + 20), - new Point(_startPosX + 6 , _startPosY + 20 + 20), - new Point(_startPosX + 20, _startPosY + 20 + 20), - new Point(_startPosX + 20, _startPosY + 2 + 20) - }; - Polygon glassPolygon = new Polygon(); - for(Point point: glass){ - glassPolygon.addPoint(point.x, point.y); - } - g2d.setColor(Color.WHITE); - g2d.fillPolygon(glassPolygon); - - - if(entityElectricLocomotive.getBatteryStorage()){ - int mod = 50; - Point[] battery = new Point[]{ - new Point(_startPosX + 40/2 + mod, _startPosY + 2+ 20), - new Point(_startPosX + 40/2 + mod, _startPosY + 4+ 20), - new Point(_startPosX + 35/2 + mod , _startPosY + 4+ 20), - new Point(_startPosX + 35/2 + mod, _startPosY + 16+ 20), - new Point(_startPosX + 60/2 + mod, _startPosY + 16+ 20), - new Point(_startPosX + 60/2 + mod, _startPosY + 4+ 20), - new Point(_startPosX + 55/2 + mod, _startPosY + 4+ 20), - new Point(_startPosX + 55/2 + mod, _startPosY + 2+ 20), - }; - mod += 20; - Point[] battery2 = new Point[]{ - new Point(_startPosX + 40/2 + mod, _startPosY + 2+ 20), - new Point(_startPosX + 40/2 + mod, _startPosY + 4+ 20), - new Point(_startPosX + 35/2 + mod , _startPosY + 4+ 20), - new Point(_startPosX + 35/2 + mod, _startPosY + 16+ 20), - new Point(_startPosX + 60/2 + mod, _startPosY + 16+ 20), - new Point(_startPosX + 60/2 + mod, _startPosY + 4+ 20), - new Point(_startPosX + 55/2 + mod, _startPosY + 4+ 20), - new Point(_startPosX + 55/2 + mod, _startPosY + 2+ 20), - }; - - Polygon batteryPolygon = new Polygon(); - for(Point points : battery){ - batteryPolygon.addPoint(points.x, points.y); - } - Polygon battery2Polygon = new Polygon(); - for(Point points : battery2){ - battery2Polygon.addPoint(points.x, points.y); - } - g2d.setColor(Color.YELLOW); - g2d.fillPolygon(batteryPolygon); - g2d.fillPolygon(battery2Polygon); - } - if(entityElectricLocomotive.getPantograph()){ - g2d.setColor(Color.BLACK); - g.drawRect(_startPosX + 40,_startPosY,10,1); - g.drawRect(_startPosX + 5 + 40, _startPosY ,1,20); - - g.drawRect(_startPosX + 40 + 30,_startPosY,10,1); - g.drawRect(_startPosX + 5 + 40 + 30, _startPosY ,1,20); - - } - - - - } - - -} diff --git a/OOP/src/Drawnings/DrawningElectricLocomotive.java b/OOP/src/Drawnings/DrawningElectricLocomotive.java new file mode 100644 index 0000000..88302f3 --- /dev/null +++ b/OOP/src/Drawnings/DrawningElectricLocomotive.java @@ -0,0 +1,85 @@ +package Drawnings; + +import Entities.EntityElectricLocomotive; +import java.awt.*; + + +public class DrawningElectricLocomotive extends DrawningLocomotive { + private EntityElectricLocomotive entityElectricLocomotive; + + public DrawningElectricLocomotive(int speed, float weight, Color bodyColor,int wheelsType, Color additionalColor, boolean batteryStorage, boolean pantograph){ + super(speed, weight ,bodyColor,wheelsType, 100,100); + entityElectricLocomotive = new EntityElectricLocomotive(speed,weight,bodyColor,additionalColor,batteryStorage,pantograph); + } + + public void DrawTransport(Graphics g){ + if(entityElectricLocomotive == null || _startPosX == null || _startPosY == null){return;} + super.DrawTransport(g); + Graphics2D g2d = (Graphics2D)g; + + Point[] line = new Point[]{ + new Point(_startPosX, _startPosY + 25 + 20), + new Point(_startPosX, _startPosY + 30 + 20), + new Point(_startPosX + 100, _startPosY + 30 + 20), + new Point(_startPosX + 100, _startPosY + 25 + 20), + }; + Polygon linePolugon = new Polygon(); + for(Point point: line){ + linePolugon.addPoint(point.x, point.y); + } + g2d.setColor(entityElectricLocomotive.getAdditionalColor()); + g2d.fillPolygon(linePolugon); + + + if(entityElectricLocomotive.getBatteryStorage()){ + int mod = 50; + Point[] battery = new Point[]{ + new Point(_startPosX + 40/2 + mod, _startPosY + 2+ 20), + new Point(_startPosX + 40/2 + mod, _startPosY + 4+ 20), + new Point(_startPosX + 35/2 + mod , _startPosY + 4+ 20), + new Point(_startPosX + 35/2 + mod, _startPosY + 16+ 20), + new Point(_startPosX + 60/2 + mod, _startPosY + 16+ 20), + new Point(_startPosX + 60/2 + mod, _startPosY + 4+ 20), + new Point(_startPosX + 55/2 + mod, _startPosY + 4+ 20), + new Point(_startPosX + 55/2 + mod, _startPosY + 2+ 20), + }; + mod += 20; + Point[] battery2 = new Point[]{ + new Point(_startPosX + 40/2 + mod, _startPosY + 2+ 20), + new Point(_startPosX + 40/2 + mod, _startPosY + 4+ 20), + new Point(_startPosX + 35/2 + mod , _startPosY + 4+ 20), + new Point(_startPosX + 35/2 + mod, _startPosY + 16+ 20), + new Point(_startPosX + 60/2 + mod, _startPosY + 16+ 20), + new Point(_startPosX + 60/2 + mod, _startPosY + 4+ 20), + new Point(_startPosX + 55/2 + mod, _startPosY + 4+ 20), + new Point(_startPosX + 55/2 + mod, _startPosY + 2+ 20), + }; + + Polygon batteryPolygon = new Polygon(); + for(Point points : battery){ + batteryPolygon.addPoint(points.x, points.y); + } + Polygon battery2Polygon = new Polygon(); + for(Point points : battery2){ + battery2Polygon.addPoint(points.x, points.y); + } + g2d.setColor(Color.YELLOW); + g2d.fillPolygon(batteryPolygon); + g2d.fillPolygon(battery2Polygon); + } + if(entityElectricLocomotive.getPantograph()){ + g2d.setColor(Color.BLACK); + g.drawRect(_startPosX + 40,_startPosY,10,1); + g.drawRect(_startPosX + 5 + 40, _startPosY ,1,20); + + g.drawRect(_startPosX + 40 + 30,_startPosY,10,1); + g.drawRect(_startPosX + 5 + 40 + 30, _startPosY ,1,20); + + } + + + + } + + +} diff --git a/OOP/src/DrawningElectricLocomotiveWheels.java b/OOP/src/Drawnings/DrawningElectricLocomotiveWheels.java similarity index 87% rename from OOP/src/DrawningElectricLocomotiveWheels.java rename to OOP/src/Drawnings/DrawningElectricLocomotiveWheels.java index a403e62..fa1d2c5 100644 --- a/OOP/src/DrawningElectricLocomotiveWheels.java +++ b/OOP/src/Drawnings/DrawningElectricLocomotiveWheels.java @@ -1,8 +1,12 @@ +package Drawnings; + import java.awt.*; -public class DrawningElectricLocomotiveWheels { +public class DrawningElectricLocomotiveWheels implements IDrawWheels{ private WheelsCount _wheelCount; - public void setEnumNumber(int wheelCount) { + + @Override + public void setNumber(int wheelCount) { for (WheelsCount value : WheelsCount.values()) { if (value.getEnumNumber() == wheelCount) { _wheelCount = value; @@ -10,8 +14,11 @@ public class DrawningElectricLocomotiveWheels { } } } - public void drawElectricLocomotiveWheels(Graphics g, Color color, int startPosX, int startPosY) { - Graphics2D g2d = (Graphics2D) g; + + + + @Override + public void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) { g2d.setColor(color); g2d.setStroke(new BasicStroke(4)); diff --git a/OOP/src/Drawnings/DrawningFancyWheels.java b/OOP/src/Drawnings/DrawningFancyWheels.java new file mode 100644 index 0000000..d0a4a0a --- /dev/null +++ b/OOP/src/Drawnings/DrawningFancyWheels.java @@ -0,0 +1,78 @@ +package Drawnings; + +import java.awt.*; + +public class DrawningFancyWheels implements IDrawWheels{ + private WheelsCount _wheelCount; + + @Override + public void setNumber(int wheelCount) { + for (WheelsCount value : WheelsCount.values()) { + if (value.getEnumNumber() == wheelCount) { + _wheelCount = value; + return; + } + } + } + + + + @Override + public void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) { + g2d.setColor(color); + g2d.setStroke(new BasicStroke(4)); + + if (_wheelCount.getEnumNumber() >= 2){ + g2d.drawOval(startPosX, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1, startPosY + 70 + 1, 8, 8); + g2d.drawOval(startPosX+ 10, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 10, startPosY + 70 + 1, 8, 8); + + + g2d.drawOval(startPosX + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 50, startPosY + 70 + 1, 8, 8); + g2d.drawOval(startPosX+ 10 + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 10 + 50, startPosY + 70 + 1, 8, 8); + + } + + if (_wheelCount.getEnumNumber() >= 3){ + g2d.drawOval(startPosX, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1, startPosY + 70 + 1, 8, 8); + g2d.drawOval(startPosX+ 10, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 10, startPosY + 70 + 1, 8, 8); + g2d.drawOval(startPosX+ 10 +10, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 10 + 10, startPosY + 70 + 1, 8, 8); + + g2d.drawOval(startPosX + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 50, startPosY + 70 + 1, 8, 8); + g2d.drawOval(startPosX+ 10 + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 10 + 50, startPosY + 70 + 1, 8, 8); + g2d.drawOval(startPosX + 10 + 10 + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 10 + 10 + 50, startPosY + 70 + 1, 8, 8); + } + + if (_wheelCount.getEnumNumber() >= 4){ + g2d.drawOval(startPosX, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1, startPosY + 70 + 1, 8, 8); + g2d.drawOval(startPosX+ 10, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 10, startPosY + 70 + 1, 8, 8); + g2d.drawOval(startPosX+ 10 +10, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 10 + 10, startPosY + 70 + 1, 8, 8); + g2d.drawOval(startPosX+ 10 +10 + 10, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 10 + 10 + 10, startPosY + 70 + 1, 8, 8); + + g2d.drawOval(startPosX + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 50, startPosY + 70 + 1, 8, 8); + g2d.drawOval(startPosX+ 10 + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 10 + 50, startPosY + 70 + 1, 8, 8); + g2d.drawOval(startPosX + 10 + 10 + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 10 + 10 + 50, startPosY + 70 + 1, 8, 8); + g2d.drawOval(startPosX + 10 + 10 + 10 + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 1 + 10 + 10 + 10 + 50, startPosY + 70 + 1, 8, 8); + } + + } +} + + diff --git a/OOP/src/Drawnings/DrawningLocomotive.java b/OOP/src/Drawnings/DrawningLocomotive.java new file mode 100644 index 0000000..55b3c21 --- /dev/null +++ b/OOP/src/Drawnings/DrawningLocomotive.java @@ -0,0 +1,185 @@ +package Drawnings; + + +import Entities.EntityLocomotive; +import MovementStrategy.MovementDirection; + +import java.awt.*; +import java.util.Random; + +public class DrawningLocomotive { + private final EntityLocomotive entityLocomotive; + + public EntityLocomotive getEntityLocomotive(){ + return entityLocomotive; + } + + private Integer _pictureWidth; + private Integer _pictureHeight; + protected Integer _startPosX; + protected Integer _startPosY; + private int _drawingLocomotiveWidth = 100; + private int _drawingLocomotiveHight = 100; + + public int GetPosX() { + return _startPosX; + } + + public int GetPosY() { + return _startPosY; + } + + public int GetWidth() { + return _drawingLocomotiveWidth; + } + + public int GetHeight() { + return _drawingLocomotiveHight; + } + + private IDrawWheels drawWheels; + + + public DrawningElectricLocomotiveWheels _drawningElectricLocomotiveWheels; + + public DrawningLocomotive(int speed, double weight, Color bodyColor, int wheelsType) { + entityLocomotive = new EntityLocomotive(speed, weight, bodyColor); + _startPosY = null; + _startPosX = null; + _pictureWidth = null; + _pictureHeight = null; + switch (wheelsType){ + case 0: + drawWheels = new DrawningElectricLocomotiveWheels(); + break; + case 1: + drawWheels = new DrawningFancyWheels(); + break; + case 2: + drawWheels = new DrawningUltraFancyWheels(); + + } + + Random random = new Random(); + int[] countwheels = {2, 3, 4}; + int wheelCount = countwheels[random.nextInt(countwheels.length)]; + drawWheels.setNumber(wheelCount); + + + } + + protected DrawningLocomotive(int speed, float weight, Color bodyColor,int wheelsType, int locomotiveWidth, int locomotiveHight) { + this(speed, weight, bodyColor, wheelsType); + _drawingLocomotiveHight = locomotiveHight; + _drawingLocomotiveWidth = locomotiveWidth; + } + + public void setPosition(int x, int y) { + if (_pictureHeight == null || _pictureWidth == null) + return; + _startPosX = x; + _startPosY = y; + + if (_drawingLocomotiveWidth + x > _pictureWidth || x < 0) { + _startPosX = 0; + } + if (_drawingLocomotiveHight + y > _pictureHeight || y < 0) { + _startPosY = 0; + } + } + + public boolean setPictureSize(int width, int height) { + + if (_drawingLocomotiveHight > height || _drawingLocomotiveWidth > width) + return false; + _pictureHeight = height; + _pictureWidth = width; + + if (_startPosX != null && _startPosY != null) { + if (_startPosX + _drawingLocomotiveWidth > width) + _startPosX = width - _drawingLocomotiveWidth; + if (_startPosY + _drawingLocomotiveHight > height) + _startPosY = height - _drawingLocomotiveHight; + } + return true; + } + + public boolean moveTransport(MovementDirection direction) { + if (entityLocomotive == null || _pictureWidth == null || _pictureHeight == null) + return false; + switch (direction) { + case Left: + if (_startPosX - entityLocomotive.getStep() > 0) + _startPosX -= (int) entityLocomotive.getStep(); + return true; + case Up: + if (_startPosY - entityLocomotive.getStep() > 0) + _startPosY -= (int) entityLocomotive.getStep(); + return true; + case Right: + if (_startPosX + entityLocomotive.getStep() < _pictureWidth - _drawingLocomotiveWidth) + _startPosX += (int) entityLocomotive.getStep(); + return true; + case Down: + if (_startPosY + entityLocomotive.getStep() < _pictureHeight - _drawingLocomotiveHight) + _startPosY += (int) entityLocomotive.getStep(); + return true; + default: + return false; + } + } + + public void DrawTransport(Graphics g){ + if (entityLocomotive == null || _startPosX == null || _startPosY == null) { + return; + } + + Graphics2D g2d = (Graphics2D)g; + drawWheels.drawWheels(g2d, Color.BLACK, _startPosX, _startPosY); + Point[] Body = new Point[]{ + new Point(_startPosX + 20,_startPosY + 20), + new Point(_startPosX, _startPosY + 25 + 20), + new Point(_startPosX , _startPosY + 50 + 20), + new Point(_startPosX + 100 , _startPosY + 50 + 20), + new Point(_startPosX + 100, _startPosY + 25 + 20), + new Point(_startPosX + 105, _startPosY + 20), + }; + + Polygon BodyPolygon = new Polygon(); + for(Point point:Body){ + BodyPolygon.addPoint(point.x, point.y); + } + g2d.setColor(entityLocomotive.getBodyColor()); + g2d.fillPolygon(BodyPolygon); + + + Point[] line = new Point[]{ + new Point(_startPosX, _startPosY + 25 + 20), + new Point(_startPosX, _startPosY + 30 + 20), + new Point(_startPosX + 100, _startPosY + 30 + 20), + new Point(_startPosX + 100, _startPosY + 25 + 20), + }; + + Polygon linePolugon = new Polygon(); + for(Point point: line){ + linePolugon.addPoint(point.x, point.y); + } + g2d.setColor(Color.black); + g2d.fillPolygon(linePolugon); + + Point[] glass = new Point[]{ + new Point(_startPosX + 20, _startPosY + 2 + 20), + new Point(_startPosX + 6 , _startPosY + 20 + 20), + new Point(_startPosX + 20, _startPosY + 20 + 20), + new Point(_startPosX + 20, _startPosY + 2 + 20) + }; + Polygon glassPolygon = new Polygon(); + for(Point point: glass){ + glassPolygon.addPoint(point.x, point.y); + } + g2d.setColor(Color.WHITE); + g2d.fillPolygon(glassPolygon); + } + + +} diff --git a/OOP/src/Drawnings/DrawningUltraFancyWheels.java b/OOP/src/Drawnings/DrawningUltraFancyWheels.java new file mode 100644 index 0000000..cc50ffe --- /dev/null +++ b/OOP/src/Drawnings/DrawningUltraFancyWheels.java @@ -0,0 +1,63 @@ +package Drawnings; + +import java.awt.*; + +public class DrawningUltraFancyWheels implements IDrawWheels{ + private WheelsCount _wheelCount; + + @Override + public void setNumber(int wheelCount) { + for (WheelsCount value : WheelsCount.values()) { + if (value.getEnumNumber() == wheelCount) { + _wheelCount = value; + return; + } + } + } + + @Override + public void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) { + g2d.setColor(color); + g2d.setStroke(new BasicStroke(4)); + + if (_wheelCount.getEnumNumber() >= 2){ + g2d.drawOval(startPosX, startPosY + 70,10,10); + g2d.drawLine(startPosX + 1, startPosY + 70 + 1, startPosX + 8, startPosY + 70 + 8); + g2d.drawOval(startPosX+ 10, startPosY + 70,10,10); + g2d.drawLine(startPosX + 1 + 10, startPosY + 70 + 1, startPosX + 8 + 10, startPosY + 70 + 8); + + g2d.drawOval(startPosX + 50, startPosY + 70,10,10); + g2d.drawLine(startPosX + 1 + 50, startPosY + 70 + 1, startPosX + 8 + 50, startPosY + 70 + 8); + g2d.drawOval(startPosX+ 10 + 50, startPosY + 70,10,10); + g2d.drawLine(startPosX + 1 + 10 + 50, startPosY + 70 + 1, startPosX + 8 + 10 + 50, startPosY + 70 + 8); + + } + + if (_wheelCount.getEnumNumber() >= 3){ + g2d.drawOval(startPosX, startPosY + 70,10,10); + g2d.drawOval(startPosX+ 10, startPosY + 70,10,10); + g2d.drawOval(startPosX+ 10 +10, startPosY + 70,10,10); + g2d.drawLine(startPosX + 1 + 10 + 10, startPosY + 70 + 1, startPosX + 8 + 10 + 10, startPosY + 70 + 8); + + g2d.drawOval(startPosX + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 10 + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 10 + 10 + 50, startPosY + 70,10,10); + g2d.drawLine(startPosX + 1 + 10 + 10 + 50, startPosY + 70 + 1, startPosX + 8 + 10 + 10 + 50, startPosY + 70 + 8); + } + + if (_wheelCount.getEnumNumber() >= 4){ + g2d.drawOval(startPosX, startPosY + 70,10,10); + g2d.drawOval(startPosX+ 10, startPosY + 70,10,10); + g2d.drawOval(startPosX+ 10 +10, startPosY + 70,10,10); + g2d.drawOval(startPosX+ 10 +10 + 10, startPosY + 70,10,10); + g2d.drawLine(startPosX + 1 + 10 + 10 + 10, startPosY + 70 + 1, startPosX + 8 + 10 + 10 + 10, startPosY + 70 + 8); + + g2d.drawOval(startPosX + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 10 + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 10 + 10 + 50, startPosY + 70,10,10); + g2d.drawOval(startPosX + 10 + 10 + 10 + 50, startPosY + 70,10,10); + g2d.drawLine(startPosX + 1 + 10 + 10 + 10 + 50, startPosY + 70 + 1, startPosX + 8 + 10 + 10 + 10 + 50, startPosY + 70 + 8); + } + + } +} diff --git a/OOP/src/Drawnings/IDrawWheels.java b/OOP/src/Drawnings/IDrawWheels.java new file mode 100644 index 0000000..02a8066 --- /dev/null +++ b/OOP/src/Drawnings/IDrawWheels.java @@ -0,0 +1,7 @@ +package Drawnings; + +import java.awt.*; + public interface IDrawWheels { + void setNumber(int x); + void drawWheels(Graphics2D graphics2D, Color color, int _startX, int _startY); + } diff --git a/OOP/src/WheelsCount.java b/OOP/src/Drawnings/WheelsCount.java similarity index 92% rename from OOP/src/WheelsCount.java rename to OOP/src/Drawnings/WheelsCount.java index f86dd18..20108b3 100644 --- a/OOP/src/WheelsCount.java +++ b/OOP/src/Drawnings/WheelsCount.java @@ -1,3 +1,5 @@ +package Drawnings; + public enum WheelsCount { Two(2), Three(3), diff --git a/OOP/src/Entities/EntityElectricLocomotive.java b/OOP/src/Entities/EntityElectricLocomotive.java new file mode 100644 index 0000000..a2fef70 --- /dev/null +++ b/OOP/src/Entities/EntityElectricLocomotive.java @@ -0,0 +1,26 @@ +package Entities; + +import java.awt.*; + +public class EntityElectricLocomotive extends EntityLocomotive{ + + private Color AdditionalColor; + public Color getAdditionalColor() { + return AdditionalColor; + } + private boolean BatteryStorage; + public boolean getBatteryStorage() { + return BatteryStorage; + } + private boolean Pantograph; + public boolean getPantograph() { + return Pantograph; + } + + public EntityElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, boolean pantograph, boolean batteryStorage) { + super(speed, weight, bodyColor); + AdditionalColor = additionalColor; + Pantograph = pantograph; + BatteryStorage = batteryStorage; + } +} diff --git a/OOP/src/Entities/EntityLocomotive.java b/OOP/src/Entities/EntityLocomotive.java new file mode 100644 index 0000000..e9c3ac5 --- /dev/null +++ b/OOP/src/Entities/EntityLocomotive.java @@ -0,0 +1,36 @@ +package Entities; + +import java.awt.*; + +public class EntityLocomotive { + private int Speed; + + public int getSpeed() { + return Speed; + } + + private double Weight; + + public double getWeight() { + return Weight; + } + + private Color BodyColor; + + public Color getBodyColor() { + return BodyColor; + } + + private double Step; + + public double getStep() { + return Speed * 100 / Weight; + } + + public EntityLocomotive(int speed, double weight, Color bodyColor) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + +} diff --git a/OOP/src/EntityElectricLocomotive.java b/OOP/src/EntityElectricLocomotive.java deleted file mode 100644 index c8f4f4b..0000000 --- a/OOP/src/EntityElectricLocomotive.java +++ /dev/null @@ -1,39 +0,0 @@ -import java.awt.*; - -public class EntityElectricLocomotive { - private int Speed; - public int getSpeed() { - return Speed; - } - private double Weight; - public double getWeight() { - return Weight; - } - private Color BodyColor; - public Color getBodyColor() { - return BodyColor; - } - private Color AdditionalColor; - public Color getAdditionalColor() { - return AdditionalColor; - } - public double Step() { - return Speed*100/Weight; - } - private boolean BatteryStorage; - public boolean getBatteryStorage() { - return BatteryStorage; - } - private boolean Pantograph; - public boolean getPantograph() { - return Pantograph; - } - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean pantograph, boolean batteryStorage) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Pantograph = pantograph; - BatteryStorage = batteryStorage; - } -} diff --git a/OOP/src/FormElectricLocomotive.form b/OOP/src/FormElectricLocomotive.form index 5b13c84..137ff13 100644 --- a/OOP/src/FormElectricLocomotive.form +++ b/OOP/src/FormElectricLocomotive.form @@ -8,61 +8,55 @@ - + - + + + - - - - - - - - - + + + - + - - - - - - - - - - - - - - + - + - - - - + + + + - - + + + + + + + + + + + + + - + @@ -73,9 +67,17 @@ + + + + + + + + - + @@ -86,6 +88,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/OOP/src/FormElectricLocomotive.java b/OOP/src/FormElectricLocomotive.java index 2a90784..1dd2a7d 100644 --- a/OOP/src/FormElectricLocomotive.java +++ b/OOP/src/FormElectricLocomotive.java @@ -1,22 +1,56 @@ + +import Drawnings.DrawningElectricLocomotive; +import Drawnings.DrawningLocomotive; +import MovementStrategy.*; + import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.LinkedList; -import java.util.List; import java.util.Random; +import java.util.List; public class FormElectricLocomotive extends JFrame { - protected DrawningElectricLocomotive _drawningElectricLocomotive = new DrawningElectricLocomotive(); + protected DrawningLocomotive _drawningLocomotive; JPanel PanelWrapper; private JPanel PictureBox; - private JButton buttonCreate; + private JButton buttonCreateElectricLocomotive; + private JButton buttonCreateLocomotive; private JButton buttonRight; private JButton buttonDown; private JButton buttonLeft; private JButton buttonUp; - + private JComboBox comboBoxStrategy; + private JButton buttonStrategyStep; + private AbstractStrategy _strategy; private List controls; + + private void createObject(String type) { + Random random = new Random(); + switch (type) { + case "Drawnings.DrawningLocomotive": + _drawningLocomotive = new DrawningLocomotive(50, 100, + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3)); + break; + case "Drawnings.DrawningElectricLocomotive": + _drawningLocomotive = new DrawningElectricLocomotive(50, 100, + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),random.nextInt(3), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + random.nextBoolean(), random.nextBoolean()); + break; + default: + return; + } + _drawningLocomotive.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight()); + _drawningLocomotive.setPosition(random.nextInt(100), + random.nextInt(100)); + _strategy = null; + comboBoxStrategy.setEnabled(true); + + Draw(); + } + public FormElectricLocomotive() { buttonUp.setName("buttonUp"); buttonDown.setName("buttonDown"); @@ -25,25 +59,19 @@ public class FormElectricLocomotive extends JFrame { InitializeControlsRepaintList(); - buttonCreate.addActionListener(new ActionListener() { + buttonCreateElectricLocomotive.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - _drawningElectricLocomotive = new DrawningElectricLocomotive(); - Random random = new Random(); - - _drawningElectricLocomotive.Init(25, - 200, - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - random.nextBoolean(), random.nextBoolean() ); - _drawningElectricLocomotive.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight()); - _drawningElectricLocomotive.setPosition(random.nextInt(100), - random.nextInt(100)); - - Draw(); + createObject("Drawnings.DrawningElectricLocomotive"); } }); + buttonCreateLocomotive.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + createObject("Drawnings.DrawningLocomotive"); + } + }); ActionListener buttonMoveClickedListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -52,36 +80,72 @@ public class FormElectricLocomotive extends JFrame { switch (buttonName) { case "buttonUp": { - result = _drawningElectricLocomotive.moveTransport(DirectionType.Up); + result = _drawningLocomotive.moveTransport(MovementDirection.Up); } break; case "buttonDown": { - result = _drawningElectricLocomotive.moveTransport(DirectionType.Down); + result = _drawningLocomotive.moveTransport(MovementDirection.Down); } break; case "buttonLeft": { - result = _drawningElectricLocomotive.moveTransport(DirectionType.Left); + result = _drawningLocomotive.moveTransport(MovementDirection.Left); } break; case "buttonRight": { - result = _drawningElectricLocomotive.moveTransport(DirectionType.Right); + result = _drawningLocomotive.moveTransport(MovementDirection.Right); } break; - } if (result) Draw(); - } }; buttonRight.addActionListener(buttonMoveClickedListener); buttonDown.addActionListener(buttonMoveClickedListener); buttonLeft.addActionListener(buttonMoveClickedListener); buttonUp.addActionListener(buttonMoveClickedListener); + comboBoxStrategy.addItem("К центру"); + comboBoxStrategy.addItem("К краю"); + buttonStrategyStep.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_drawningLocomotive == null) + return; + if (comboBoxStrategy.isEnabled()) { + switch (comboBoxStrategy.getSelectedIndex()) { + case 0: + _strategy = new MoveToCenter(); + break; + case 1: + _strategy = new MoveToBorder(); + break; + default: + _strategy = null; + break; + + } + if (_strategy == null) { + return; + } + _strategy.SetData(new MoveableLocomotive(_drawningLocomotive), PictureBox.getWidth(), PictureBox.getHeight()); + } + if (_strategy == null) { + return; + } + _strategy.MakeStep(); + Draw(); + comboBoxStrategy.setEnabled(false); + + if (_strategy.GetStatus() == StrategyStatus.Finish) { + comboBoxStrategy.setEnabled(true); + _strategy = null; + } + } + }); } private void Draw() { - if (_drawningElectricLocomotive.getEntityElectricLocomotive() == null) + if (_drawningLocomotive.getEntityLocomotive() == null) return; if (PictureBox.getWidth() == 0 || PictureBox.getHeight() == 0) { return; @@ -89,10 +153,9 @@ public class FormElectricLocomotive extends JFrame { Graphics g = PictureBox.getGraphics(); g.setColor(PictureBox.getBackground()); g.fillRect(0,0, PictureBox.getWidth(), PictureBox.getHeight()); - _drawningElectricLocomotive.DrawTransport(g); + _drawningLocomotive.DrawTransport(g); RepaintControls(); - } private void RepaintControls() { for (JComponent control : controls) { @@ -100,15 +163,15 @@ public class FormElectricLocomotive extends JFrame { } } - private void InitializeControlsRepaintList() { controls = new LinkedList<>(); - controls.add(buttonCreate); + controls.add(buttonCreateElectricLocomotive); + controls.add(buttonCreateLocomotive); controls.add(buttonUp); controls.add(buttonDown); controls.add(buttonLeft); controls.add(buttonRight); + controls.add(comboBoxStrategy); + controls.add(buttonStrategyStep); } - - -} +} \ No newline at end of file diff --git a/OOP/src/Main.java b/OOP/src/Main.java index c8dc57b..ca8fdc0 100644 --- a/OOP/src/Main.java +++ b/OOP/src/Main.java @@ -1,4 +1,7 @@ import javax.swing.*; + +//TIP To Run code, press or +// click the icon in the gutter. public class Main { public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(false); diff --git a/OOP/src/MovementStrategy/AbstractStrategy.java b/OOP/src/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..333abfd --- /dev/null +++ b/OOP/src/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,66 @@ +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 (IsTargetDestination()) + { + _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 int GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return 0; + } + return _moveableObject.GetStep(); + } + protected abstract void MoveToTarget(); + protected abstract boolean IsTargetDestination(); + private boolean MoveTo(MovementDirection directionType) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + if (_moveableObject.TryMoveObject(directionType)) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } +} \ No newline at end of file diff --git a/OOP/src/MovementStrategy/IMoveableObject.java b/OOP/src/MovementStrategy/IMoveableObject.java new file mode 100644 index 0000000..0cb1a83 --- /dev/null +++ b/OOP/src/MovementStrategy/IMoveableObject.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean TryMoveObject(MovementDirection direction); + void MoveObject(MovementDirection direction); +} \ No newline at end of file diff --git a/OOP/src/MovementStrategy/MoveToBorder.java b/OOP/src/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..505f3d1 --- /dev/null +++ b/OOP/src/MovementStrategy/MoveToBorder.java @@ -0,0 +1,47 @@ +package MovementStrategy; + +public class MoveToBorder extends AbstractStrategy { + protected boolean IsTargetDestination() + { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) { + return false; + } + return objParams.RightBorder() <= FieldWidth && + objParams.RightBorder() + GetStep() >= FieldWidth && + objParams.DownBorder() <= FieldHeight && + objParams.DownBorder() + GetStep() >= FieldHeight; + } + + protected void MoveToTarget() + { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) { + return; + } + int diffX = objParams.RightBorder() - FieldWidth; + if (Math.abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.DownBorder() - FieldHeight; + if (Math.abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} \ No newline at end of file diff --git a/OOP/src/MovementStrategy/MoveToCenter.java b/OOP/src/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..0250903 --- /dev/null +++ b/OOP/src/MovementStrategy/MoveToCenter.java @@ -0,0 +1,47 @@ +package MovementStrategy; + + +public class MoveToCenter extends AbstractStrategy { + protected boolean IsTargetDestination() + { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) { + return false; + } + return (objParams.ObjectMiddleHorizontal() - GetStep() <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical() - GetStep() <= FieldHeight / 2 && + objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2); + } + 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(); + } + } + } +} \ No newline at end of file diff --git a/OOP/src/MovementStrategy/MoveableLocomotive.java b/OOP/src/MovementStrategy/MoveableLocomotive.java new file mode 100644 index 0000000..86250d7 --- /dev/null +++ b/OOP/src/MovementStrategy/MoveableLocomotive.java @@ -0,0 +1,24 @@ +package MovementStrategy; + +import Drawnings.DrawningLocomotive; + +public class MoveableLocomotive implements IMoveableObject { + private DrawningLocomotive _locomotive = null; + public MoveableLocomotive(DrawningLocomotive drawningLocomotive) + { + _locomotive = drawningLocomotive; + } + + public ObjectParameters GetObjectPosition() + { + if (_locomotive == null || _locomotive.getEntityLocomotive() == null) + { + return null; + } + return new ObjectParameters(_locomotive.GetPosX(), _locomotive.GetPosY(), _locomotive.GetWidth(), _locomotive.GetHeight()); + } + + public int GetStep() { return (int) _locomotive.getEntityLocomotive().getStep(); } + public boolean TryMoveObject(MovementDirection direction) { return _locomotive.moveTransport(direction); } + public void MoveObject(MovementDirection direction) { _locomotive.moveTransport(direction); } +} \ No newline at end of file diff --git a/OOP/src/MovementStrategy/MovementDirection.java b/OOP/src/MovementStrategy/MovementDirection.java new file mode 100644 index 0000000..91ab5f0 --- /dev/null +++ b/OOP/src/MovementStrategy/MovementDirection.java @@ -0,0 +1,9 @@ + +package MovementStrategy; + +public enum MovementDirection { + Up, + Down, + Left, + Right; +} diff --git a/OOP/src/MovementStrategy/ObjectParameters.java b/OOP/src/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..36d1273 --- /dev/null +++ b/OOP/src/MovementStrategy/ObjectParameters.java @@ -0,0 +1,25 @@ +package MovementStrategy; + +public class ObjectParameters { + private int _x; + private int _y; + private int _width; + private int _height; + + public int LeftBorder() { return _x; } + public int TopBorder() { return _y; } + public int RightBorder() { return _x + _width; } + public int DownBorder() { return _y + _height; } + + + public int ObjectMiddleHorizontal () { return _x + _width / 2; } + public int ObjectMiddleVertical () { return _y + _height / 2; } + + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} \ No newline at end of file diff --git a/OOP/src/MovementStrategy/StrategyStatus.java b/OOP/src/MovementStrategy/StrategyStatus.java new file mode 100644 index 0000000..c3c3950 --- /dev/null +++ b/OOP/src/MovementStrategy/StrategyStatus.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public enum StrategyStatus { + NotInit, + InProgress, + Finish +} \ No newline at end of file